Dave F
Dave F

Reputation: 71

Providing arrays as parameters to devops pipelines

I'm just getting started with azure pipelines. We're wanting to move away from manually deploying our Azure resources with arm templates and parameter files to do doing so with a devops pipeline.

While it's straightforward to do this with a single template+parameter's json file, I cannot see a sensible way to do with with multiple files.

I've seen discussions concerning using an array as a parameter to a pipeline template - this would work, but I cannot see how to create the parameter array other than setting it manually in the pipeline yaml file.

A more useful solution would be either by reading its contents in from a text file- which we could generate before the file is read, or - even more ideally- by referencing a directory listing of the folder within the repo containing the json parameter files for the template that's common to them all.

A code fragment within the yaml pipeline is capable of passing back a single string variable E.g.

script: |
      # Get list of files in folder
      $fl =  get-childitem ./templates | select-object -expand name
      Write-output  "##vso[task.setvariable variable=fl]$fl"
      

But all this produces is a single string which I cannot then split.

Similarly, I can use a fragment of code to dynamically create the file listing - but again, cannot see how to read that within the pipeline.

Is there an approach that lets my dynamically set the content of the parameter array within an devops pipeline, or read the array in from a file?

Or should i just give up and write the entire thing in a powershell script and use a task to run that instead of trying to use the azure deployment tasks in a devops?

Thanks Dave

Upvotes: 7

Views: 26383

Answers (2)

Rachel
Rachel

Reputation: 1284

You can use extends to create a pipeline template that deploys your resources. This can have a parameter for the resources you want to deploy, including arm template + param files array, and then loop through them using foreach. Then you can hard-code your resources into params of multiple pipelines, with the trigger filtered by branch/path/your other criteria, which pass these params into the template.

Upvotes: 1

Vito Liu
Vito Liu

Reputation: 8298

1.Read the String file and split the string into an array on the yaml

Create a yaml build and add the task power shell. We also can use the extension Replace Tokens to update the value of file

$ourfiles = Get-Content "file path"
Write-Output $ourfiles

$CharArray =$ourfiles.Split(" ")
$CharArray

2.We also can use each in the yaml, please refer to this doc for more details.

Template file: test.yml

parameters:
- name: InstanceArgs 
  type: object
  default: [] 
stages:
  - stage: deploy
    displayName: deploy
    jobs:
      - ${{ each arg in parameters.InstanceArgs }}:
        - deployment: ${{ arg.value }}
          displayName: ${{ arg.value }}
          environment: Development
          strategy:
            runOnce:
              deploy:
                steps:
                - script: echo Hello, world!

YAML build

extends:
  template: test.yml
  parameters:
    InstanceArgs:  
      In1 : _1
      In2 : _2

I hope it can help you.

Upvotes: 6

Related Questions