0

I would like to re-use lists of servers in multiple deployments. A simplified version of what I'm doing in some template deploy-tasks.yml:

parameters:
- name: servers
  type: object

- ${{ each server in parameters.servers }}:
  - task: CopyFiles@2
    inputs:
      sourceFolder: ${{parameters.sourceFolder}}
      targetFolder: '\\${{server}}\some-path'
      cleanTargetFolder: true

Then I can use this in for example this pipeline.yml

steps:
- checkout: self
- template: deploy-tasks.yml
  parameters: 
    servers:
    # How can I define this list somewhere else?!
    - server1
    - server2
    sourceFolder: '$(Build.SourcesDirectory)\something'

So far it works: but I would like to extract the commonly used lists of servers. So something like infra.yml:

my-production-webservers:
- production1
- production2

And then something like (but this doesn't work):

steps:
- checkout: self
- template: deploy-tasks.yml
  parameters: 
    servers:
    !!! something that includes my-production-webservers !!!
    sourceFolder: '$(Build.SourcesDirectory)\something'

Variables don't work in this case because they are string-only. What trick am I missing?

Sunib
  • 304
  • 2
  • 14
  • 1
    I don't think that is possible. Maybe you can extend the template https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#extend-from-a-template . At one point you need to assign the values directly to the parameter. – Mohamed Sahbi Mar 15 '21 at 21:03

1 Answers1

0

I am afraid that there is no such method could meet your requirements.

As you said, the variables are string type. So the variables couldn't pass the value list to object type parameter.

In the template, it is invalid to only define the parameter, because the parameters field in the main yaml cannot refer to the template.

Therefore you cannot save only parameter values in the yaml template. You need to add additional steps in the template.

Workaround:

You could try the following sample: it is not a pure parameter. It needs to reference deploy-tasks.yml file

deploy-tasks.yml

parameters:
  - name: servers
    type: object
    default: []

steps:
 - ${{ each server in parameters.servers}}:
    - script: echo ${{ server }}

infra.yml

parameters:
  - name: buildTemplate
    type: string
    default: ""

steps:
- template: ${{parameters.buildTemplate}}
  parameters:
    servers:
      - Server1
      - Server2
      - Server3

pipeline.yml

trigger:
- none

pool:
  vmImage: ubuntu-latest

steps:
- template: infra.yml
  parameters:
    buildTemplate: deploy-tasks.yml
      
Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • 1
    Thank you for the elaborate example, I'm a bit disappointed by the fact that there is no easy fix, it would have been so nice. I also played with saving it as a comma-separated string, but there is no split operation (and there is a join). – Sunib Mar 16 '21 at 14:02
  • 1
    Hmmm. I'm trying to apply it, but a disadvantage is that you can't have real parameters in the deployTasks anymore (you can't parse them through the infra.yml). – Sunib Mar 16 '21 at 14:21
  • Hi @Sunib. I could fully understand your requirement. But there is no such method could meet your requirement for the time beting. Because the parameters field doesn't support template. Please refer to this doc about the supported templates: https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema%2Cparameter-schema#template-references – Kevin Lu-MSFT Mar 17 '21 at 05:27
  • 1
    Based on the existing template, I also tested to use the variable to pass the value. But it couldn't pass the correct value to object type parameters. For your requirement: I suggest that you could submit a suggestion ticket on [UserVoice Site](https://developercommunity.visualstudio.com/report?space=21&entry=suggestion). Hope that there will be a simple method in the future – Kevin Lu-MSFT Mar 17 '21 at 05:30
  • @Sunib. Is there any update about this ticket? If the answer could give you some help, you may consider accepting it. thanks – Kevin Lu-MSFT Mar 18 '21 at 01:46