0
votes

I'm kinda stuck with this achievement i would like to have available in my build pipeline.

The main goal is to iterate through every value extracted by the function {{split ',' parameters.PARAMETER_PASSED}} when I'm already in the template form.

How it should looks like:

  1. Build pass string that changes depending by one script value1,value2,value3,...,valueX
  2. Template start
  3. FOR EACH valueN, start a script with a specific displayName (in this case ${{ value}} will be ok)

Below you can find what i've achieved so far:

azure-pipelines.yaml

- stage: AutomateScriptContent
    displayName: 'Some new trick'
    jobs:
      - job: CheckFileChanges
        displayName: Prepare list
        steps:
          - checkout: self
            persistCredentials: true
          - bash: |
                #....Something..Something
                echo "##vso[task.setvariable variable=VALUE_LIST;isOutput=true;]value1,value2,value3"
              fi
            displayName: "Check"
            name: check
      - job: TestTemplate
        variables:
          LISTVAL: $[ dependencies.CheckFileChanges.outputs['check.VALUE_LIST'] ]
        displayName: "Do something with values passed from env variable"
        dependsOn: CheckFileChanges
        steps:
        - template: __templates__/test.template.yml
          parameters:
            MY_LIST: $(LISTVAL)

test.template.yml

parameters:
  MY_LIST: ""

steps:
  - variables:
    website: {{split ',' parameters.MY_LIST}}
  # - script: echo "${{ parameters.MY_LIST }}"
  - ${{ each value in var.MY_LIST }}:
    - script: 'echo ${{ value }}'
      displayName: '${{ value }}'

I know that test.template.yml is not correct but I cannot understand how to make it work!

Any suggestion? Not sure if it's possible to pass from bash/powershell a new array with `echo "##vso[task.setvariable variable=VALUE_LIST;isOutput=true;]$MY_ARRAY"

Also one accepted solution could be adding every value of the array as new parameter, output it and then pass ALL the parameters (without providing the name like below) but i'm not sure if it's possible to pass the parameters without providing each single name of each parameter (example below).

variables:
          LISTVAL: $[ dependencies.CheckFileChanges.outputs['check(GET_ALL_PARAMETERS)'] ]

Thank you in advance.

1

1 Answers

0
votes

You have a few problems here. In your test.template.yml

  1. A steps sequence cannot have a variables section
  2. The each keyword can only be used on parameters of type object.
  3. There is no 'split' function.

Also, your parameter name in your pipeline 'WEBSITE_LIST' doesn't match the name defined in your template 'MY_LIST'

If you have a finite list of outputs from your first job you could do something like the below but this will not be sustainable if the list grows.

pipeline:

stages:
  - stage: AutomateScriptContent
    displayName: 'Some new trick'
    jobs:
      - job: CheckFileChanges
        displayName: Prepare list
        steps:
          - checkout: self
            persistCredentials: true
          - bash: |
                #....Something..Something
                echo "##vso[task.setvariable variable=value1;isOutput=true;]foo"
                echo "##vso[task.setvariable variable=value2;isOutput=true;]bar"
                echo "##vso[task.setvariable variable=value3;isOutput=true;]lorem"
                echo "##vso[task.setvariable variable=value4;isOutput=true;]ipsum"
            displayName: "Check"
            name: check
            
      - job: TestTemplate
        variables:
          v1: $[ dependencies.CheckFileChanges.outputs['check.value1'] ]
          v2: $[ dependencies.CheckFileChanges.outputs['check.value2'] ]
          v3: $[ dependencies.CheckFileChanges.outputs['check.value3'] ]
          v4: $[ dependencies.CheckFileChanges.outputs['check.value4'] ]
        displayName: "Do something with values passed from env variable"
        dependsOn: CheckFileChanges
        steps:
        - template: __templates__/test.template.yml
          parameters:
            MY_LIST: [$(v1), $(v2), $(v3), $(v4)]

template

parameters:
  - name: MY_LIST
    type: object

steps:
  - ${{ each value in parameters.MY_LIST }}:
    - script: 'echo ${{ value }}'
      displayName: '${{value}}'

Edit: as you explain that you do not know the number of output values from your first job, this does not seem possible to achieve. Variables are always strings and, while you could output a csv string variable from the first job, there is no function available to split this into an array for use as the parameter of your second job.