Azure DevOps YAML pipelines provide a well-established, code-based approach to defining CI/CD workflows as code. When working with YAML pipelines, various types of parameters can be used to pass input values into the pipeline. These parameters can be referenced in later stages for multiple purposes, such as structuring the pipeline, adding conditions, or controlling task behavior.
In this blog, we’ll learn how to use StringList parameters in a PowerShell task within a YAML pipeline.
Pre-requites: Experience with Azure DevOps YAML pipelines
Below is a sample pipeline where readyComponents is defined as a StringList parameter, and we loop through the list within a PowerShell task. We’ll look at each section in detail in the next few steps of this blog.
parameters: - name: readyComponents type: stringList displayName: ReadyComponents values: - Container App - Front End - Kubernetes stages: - stage: Loop_StringList displayName: "Loop through StringList" jobs: - job: Loop_StringList_Job displayName: "Iterate Ready Components" steps: - task: PowerShell@2 displayName: "Process Ready Components" env: COMPONENT_NAMES: ${{ join(';', parameters.readyComponents) }} inputs: targetType: 'inline' script: | $list = $env:COMPONENT_NAMES -split ";" foreach ($component in $list) { Write-Host "Processing region: $component" }
- We are defining an environment variable named COMPONENT_NAMES and it is populated from a pipeline parameter called readyComponents. The join function joins all items in that list into a single string, separated by semicolons.
env: COMPONENT_NAMES: ${{ join(';', parameters.readyComponents) }}
- Then COMPONENT_NAMES will become as below,
- Then the PowerShell script reads the environment variable COMPONENT_NAMES that was passed into the task. Splits it into an array based on semicolon.
$list = $env:COMPONENT_NAMES -split ";"- Then List value becomes as below
$list = @("Container App","Front End","Kubernetes")
No comments:
Post a Comment