When & Where to use?
We received the request that we would like to pass the variable group as a runtime parameter so that whenever I run the pipeline, it should allow me to select the variable group name as input, and based on the input value for the variable group during runtime my pipeline should proceed. In this article, we will discuss How to use the variable group at runtime in Azure YAML Pipeline.
This can be achieve by using the Runtime parameters. Runtime parameters let you have more control over what values can be passed to a pipeline. In this article
What is Runtime parameters?
You can specify parameters in templates and in the pipeline. Parameters have data types such as number and string, and they can be restricted to a subset of values. The parameters section in a YAML defines what parameters are available. These runtime parameters allow you to have more control over the parameter values you pass to your pipelines.
Parameters are only available at template parsing time. Parameters are expanded just before the pipeline runs so that values surrounded by ${{ }} are replaced with parameter values. Use variables if you need your values to be more widely available during your pipeline run.
Note: If you are going to trigger the pipeline manually then you can make use of Runtime parameters in the Azure DevOps pipeline.
Runtime parameters let you have more control over what values can be passed to a pipeline. Unlike variables, runtime parameters have data types and don’t automatically become environment variables.
Let we see How to use the variable group at runtime in Azure YAML Pipeline
Step 1: Define the parameters under the Values section
Ensure Always Set runtime parameters at the beginning of a YAML. This example pipeline accepts the value of variable and then outputs the value in the job
parameters: - name: variable_group displayName: Variable Group type: string default: app-sitecore-dev values: - app-sitecore-dev - app-sitecore-qa - app-sitecore-pprd - app-sitecore-prd - app-sitecore-pprd-hotfix trigger: none # trigger is explicitly set to none
Step 2: Assign the selected value to the variable group.
Post slection of variable group during manula build, the selected variable will be assinged by using ${{ parameters.<parameter_name> }}. once runtime parameter is assinged the sequence of stage/jobs can able to use the values
variables: - group: ${{ parameters.variable_group }}
Step 3: Use the values from the selected variable group
Based on the variable group assinged from run time parameter, the remaining stage can fetch the value from the variable group like agentPool…
stages: - stage: Build_Artifacts jobs: - template: Prepare_Artifacts.yml parameters: agentPool: '$(agentPool)' TargetFolder: '$(Build.ArtifactStagingDirectory)'
Full YAML Code
parameters: - name: variable_group displayName: Variable Group type: string default: app-sitecore-dev values: - app-sitecore-dev - app-sitecore-qa - app-sitecore-pprd - app-sitecore-prd - app-sitecore-pprd-hotfix trigger: none # trigger is explicitly set to none variables: - group: ${{ parameters.variable_group }} stages: - stage: Build_Artifacts jobs: - template: Prepare_Artifacts.yml parameters: agentPool: '$(agentPool)' TargetFolder: '$(Build.ArtifactStagingDirectory)'