Saturday 29 October 2022

Use Template Parameter to Create Array in Bash Task in Azure Pipelines

 Converting Azure pipeline parameter type of object, which contain an array of strings, to an array object in bash script task is not clearly documented. We can use the join expression for Azure pipeline tasks to achive this requirement. Let's look at how to do with an example.

Imagine we have app list in a yaml template parameter as shown below.

parameters:
    - namemyapps
      typeobject
      default:
        - my-app1
        - my-app2
        - my-app5

If we want to get the parameter array converted to a bash array we can use  join expression for Azure pipeline tasks as following.

myapps=(echo ${{ join(' ',parameters.myapps) }})

The full template with the bash task to print the values from parameter would be as shown below.

parameters:
    - namemyapps
      typeobject
      default:
        - my-app1
        - my-app2
        - my-app5

steps:
    - taskBash@3
      inputs:
        targetType'inline'
        script: |
            myapps=(echo ${{ join(' ',parameters.myapps) }})
            
            for t in ${myapps[@]}; do
              echo $t
            done

A pipeline can be implemented to use the template as follows.

trigger:
main

pool:
  vmImageubuntu-latest

steps:
scriptecho Hello, world!
  displayName'Run a one-line script'

templatetemplatesample.yml

When the pipeline is running the template aprameter values are converted to a bash array and can be printed in the logs.



No comments:

Popular Posts