Creating dynamic variables during a pipeline run in Azure DevOps

In Azure DevOps Pipelines – you can create variables “on the fly” during your pipeline run! Wanting to create a variable such as resource ID, location of a resource in Azure etc? This blog post is for you! I will show how you can dynamically create variables during a pipeline run that can be referenced at a later stage.

Dynamic Variables

Dynamic variables are variables that are created during runtime; in this case, during part of the run of the Azure DevOps pipeline.

Super useful for non-static values that may change over time: IP addresses, route table configuration or some examples that could be used. Even concatenating values are a great use-case for this!

Azure DevOps Pipeline

The pipeline will consist of two stages:

  • create_variables_stage – Will create some variables from the output of some Azure CLI
  • variables_stage_output – Will echo the output of the variables created in the above stage

create_variables_stage Stage

In this stage, I will create two variables (two simple outputs) for the outputs of:

  • A VNET resource ID
  • A VNET location

The stage breakdown

  • Using AzureCLI@2 task
  • Creating two bash variables for VNET_RESOURCE_ID & VNET_LOCATION (highlighted below)
  • ADO creates created using task.setvariable – with isOutput set to true; allowing the variables to be used at a later step output of the initial stage
- stage: create_variables_stage
  jobs:
  - job: create_variables_job
    steps:
      - task: AzureCLI@2
        displayName: 'Create ADO Variables'
        name: 'ADO_variables'
        inputs:
          azureSubscription: 'thomasthorntoncloud'
          scriptType: bash
          scriptLocation: inlineScript
          addSpnToEnvironment: true
          inlineScript: |
            #!/bin/bash
            VNET_RESOURCE_ID=$(az network vnet show --resource-group "thomasthorntoncloud-ado-agent" --name "thomasthorntoncloud-ado-agent-vnet" --query "id" -o tsv)
            VNET_LOCATION=$(az network vnet show --resource-group "thomasthorntoncloud-ado-agent" --name "thomasthorntoncloud-ado-agent-vnet" --query "location" -o tsv)

            echo "Setting variable VNET_RESOURCE_ID_SET"
            echo "##vso[task.setvariable variable=VNET_RESOURCE_ID_SET;isOutput=true]$VNET_RESOURCE_ID"

            echo "Setting variable VNET_LOCATION_SET"
            echo "##vso[task.setvariable variable=VNET_LOCATION_SET;isOutput=true]$VNET_LOCATION"

Now that two new ADO Variables VNET_RESOURCE_ID_SET & VNET_LOCATION_SET are set, lets look at the next stage on how these can be referenced

variables_stage_output

The second stage , that will echo the output of the variables created in the above stage

With the task.setvariable references used above, two ADO variables have been created. Lets look at how they can be used in an additional stage.

Within the stages job, two variables are created (highlighted below), referencing the outputs as mentioned in the first stage.

- stage: variables_stage_output
  jobs:
  - job: variables_stage_output_job
    variables:
      VNET_RESOURCE_ID_SET: $[stageDependencies.create_variables_stage.create_variables_job.outputs['ADO_variables.VNET_RESOURCE_ID_SET']]
      VNET_LOCATION_SET: $[stageDependencies.create_variables_stage.create_variables_job.outputs['ADO_variables.VNET_LOCATION_SET']]
    steps:
      - task: Bash@3
        displayName: 'Output new ADO Variables'
        inputs:
          targetType: inline
          script: |
            echo "VNET Resource ID is: $VNET_RESOURCE_ID_SET"

            echo "VNET Location is: $VNET_LOCATION_SET"

Looking the ADO pipeline, you can see a successful output of both the VNET resource ID & VNET Location

Great! Creating variables “on the fly” during your pipeline run are super useful! Have a go 🙂

Full pipeline below

name: $(BuildDefinitionName)_$(date:yyyyMMdd)$(rev:.r)

trigger:
  batch: true
  branches:
    include:
    - main

stages:
- stage: create_variables_stage
  jobs:
  - job: create_variables_job
    steps:
      - task: AzureCLI@2
        displayName: 'Create ADO Variables'
        name: 'ADO_variables'
        inputs:
          azureSubscription: 'thomasthorntoncloud'
          scriptType: bash
          scriptLocation: inlineScript
          addSpnToEnvironment: true
          inlineScript: |
            #!/bin/bash
            VNET_RESOURCE_ID=$(az network vnet show --resource-group "thomasthorntoncloud-ado-agent" --name "thomasthorntoncloud-ado-agent-vnet" --query "id" -o tsv)
            VNET_LOCATION=$(az network vnet show --resource-group "thomasthorntoncloud-ado-agent" --name "thomasthorntoncloud-ado-agent-vnet" --query "location" -o tsv)

            echo "Setting variable VNET_RESOURCE_ID_SET"
            echo "##vso[task.setvariable variable=VNET_RESOURCE_ID_SET;isOutput=true]$VNET_RESOURCE_ID"

            echo "Setting variable VNET_LOCATION_SET"
            echo "##vso[task.setvariable variable=VNET_LOCATION_SET;isOutput=true]$VNET_LOCATION"

- stage: variables_stage_output
  jobs:
  - job: variables_stage_output_job
    variables:
      VNET_RESOURCE_ID_SET: $[stageDependencies.create_variables_stage.create_variables_job.outputs['ADO_variables.VNET_RESOURCE_ID_SET']]
      VNET_LOCATION_SET: $[stageDependencies.create_variables_stage.create_variables_job.outputs['ADO_variables.VNET_LOCATION_SET']]
    steps:
      - task: Bash@3
        displayName: 'Output new ADO Variables'
        inputs:
          targetType: inline
          script: |
            echo "VNET Resource ID is: $VNET_RESOURCE_ID_SET"

            echo "VNET Location is: $VNET_LOCATION_SET"

Thanks for viewing this post 🙂 – Have shown how you can dynamically create variables during a pipeline run that can be referenced at a later stage.

GitHub Repository with example pipeline

1 comment

Leave a Reply