Home > database >  Yaml Pipeline - Loop over dynamic variable and repeat task
Yaml Pipeline - Loop over dynamic variable and repeat task

Time:10-13

I have a UI pipeline variable (called 'HTML_Files') that passes in a string of filenames i.e. login.html password.html

In my pipeline i want to loop over and repeat a task based on the number of filenames passed in (in this case 2)

My yaml looks something like this:

steps:
        - powershell: |
            $f = "$(HTML_Files)".Split(" ");
            echo $f;
            echo '##vso[task.setvariable variable=htmlFiles; isOutput=true]$f'
          displayName: 'Create array of html files to loop over'
        - bash: |
            echo "variable value: $(htmlFiles)"
        - ${{ each file in variables.htmlFiles }}:
          - task: CmdLine@2
            displayName: run language injection tool
            inputs:
              script: .\DrupalLanguageInjector.exe $(file) $(Language)
              workingDirectory: $(System.DefaultWorkingDirectory)/terraform/language-injection

This does not work and the bash task above is outputting:

Generating script.
Script contents:
echo "variable value: $f"
"C:\Program Files\Git\bin\bash.exe" -c pwd
/d/a/_temp
========================== Starting Command Output ===========================
"C:\Program Files\Git\bin\bash.exe" /d/a/_temp/6326a530-a71b-4961-b314-cff1e694b97b.sh
variable value: 
Finishing: Bash

and the each loop does not get entered at all. Could anyone help with this?

Thanks

CodePudding user response:

First of all, you have to understand the limitations of the use of variables (Concept in DevOps pipeline) and conditional insertions.

1, Variables in DevOps Pipeline concept is always string. String can't be iterated in your situation.

2, the name of section - ${{ each file in variables.htmlFiles }} that you are using is 'conditional insertion'. Conditional insertions section needs compile time value, runtime value will not be got in this part.

So, if you want run multiple tasks expanded via 'conditional insertion', then you need to set object type parameter and iterate it to expand YAML definition of your pipeline.

Or one possible way is, you can give up expand multiple tasks. You can simply stitch the sequence data into strings with spaces and then output the variable via the logging command you're already using. After that, the string can be split by code to get the 'items', do logic based on those items. I think only one task should be enough.

By the way, please consider whether you need conditional insertion, this thing can help you expand the yaml file before the pipeline runs. But it also brings many limitations (such as not being able to use runtime values)

  • Related