Tuesday 26 November 2019

Define Variables Dynamically and Use Them in Subsequent Steps in Azure DevOps Pipelines

You normally define variables in Azure DevOps pipelines in the pipeline definition or using variable groups. However, there might be situations in a given step in your pipeline you might want to set a variable with a value from external source. You do not have to define such variables in your release definition or in a variable group added to the definition. You can obtain the value from external source and dynamically define the variable, so that it can be used within the agent job. Let’s look at the steps to achieve this.


Create and apply value for variable dynamically

The two variables defined in following PowerShell script var and var2 are dynamically created and assigned with values chaminda and chandrasekara respectively.


This kind of scenario can be used to obtain Variables via Octopus deploy variable sets attached to an Octopus deploy project, to reuse them in Azure DevOps pipeline. Example below and full post on this usage can be found here.
Write-Host ("##vso[task.setvariable variable=" + $octopusVariable.Name + "]" + $variableValue)


Use the dynamic variables in other tasks/steps

The variables can be used normally as you use the other variables defined in the variable groups or in the definition itself.


The variables effectively working and the values are available in the dynamically created variables.

No comments:

Popular Posts