-1

I'm trying to download multiple artifacts into different servers(like web, db) using environments. Currently i have added the task DownloadPipelineArtifact@2 in a file and using template to add that task in azure-pipelines.yml. As i'm having multiple artifacts, im trying to use for loop where i'm getting issues.

#azure-pipelines.yml

- template: artifacts-download.yml
  parameters:
    pipeline:
    - pipeline1
    - pipeline2
    - pipeline3
    path:
    - path1
    - path2
    - path3

I need to write loop in yaml so that it should download the pipeline1 artifacts to path1 and so on. Can someone please help??

Kart
  • 53
  • 2
  • 8

2 Answers2

2

Object-type parameters are your friend. They are incredibly powerful. As qBasicBoy answered, you'll want to make sure that you group the multiple properties together. If you're finding that you have a high number of properties per object, though, you can do a multi-line equivalent.

The following is an equivalent parameter structure to what qBasicBoy posted:

parameters:
- name: pipelines
  type: object
  default:
  - Name: pipeline1
    Path: path1
  - Name: pipeline2
    Path: path2
  - Name: pipeline3
    Path: path3

An example where you can stack many properties to a single object is as follows:

parameters:
- name: big_honkin_object
  type: object
  default:
    config:
    - appA: this
      appB: is
      appC: a
      appD: really
      appE: long
      appF: set
      appG: of
      appH: properties
    - appA: and
      appB: here
      appC: we
      appD: go
      appE: again
      appF: making
      appG: more
      appH: properties
    settings:
      startuptype: service
      recovery: no

You can, in essence, create an entire dumping ground for everything that you want to do by sticking it in one single object structure and properly segmenting everything. Sure, you could have had "startuptype" and "recovery" as separate string parameters with defaults of "service" and "no" respectively, but this way, we can pass a single large parameter from a high level pipeline to a called template, rather than passing a huge list of parameters AND defining said parameters in the template yaml scripts (remember, that's necessary!).

If you then want to access JUST a single setting, you can do something along the lines of:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.
      
      Write-Host "Apps start as a "${{ parameters.settings.startuptype }}
      Write-Host "Do the applications recover? "${{ parameters.settings.recovery }}

This will give you the following output:

Apps start as a service
Do the applications recover? no

YAML and Azure Pipelines are incredibly powerful tools. I can't recommend enough going through the entire contents of learn.microsoft.com on the subject. You'll spend a couple hours there, but you'll come out the other end with an incredibly knowledge of how these pipelines can be tailored to do everything you could ever NOT want to do yourself!

Notable links that helped me a TON (only learned this a couple months ago):

How to work with the YAML language in Pipelines https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema%2Cparameter-schema

How to compose expressions (also contains useful functions like convertToJSON for your object parameters!) https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops

How to create variables (separate from parameters, still useful) https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

SLEEPER ALERT!!! Templates are HUGELY helpful!!! https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops

Mike0684
  • 76
  • 6
1

You could use an object with multiple properties

parameters:

  - name: pipelines
    type: object  
    default:
    - { Name: pipeline1, Path: path1 }
    - { Name: pipeline2, Path: path2 }
    - { Name: pipeline3, Path: path3 }
   
steps:
  - ${{each pipeline in parameters.pipelines}}:
      # use pipeline.Name or pipeline.Path
qBasicBoy
  • 46
  • 5