Some days ago talking with Sergio Navarro (if you don’t follow him you should) he raised a question about setting environment variables in an Azure Pipeline so a container can use it in the build. The answer was easy, the great news are: all variables declared in an Azure Pipeline are “executed” (I couldn’t find a better word) as environment variables in the agent, and not only that, all environment variables in an agent can be retrieved as simple variables in the pipeline.
This is easier to understand with a very simple example, I’m using a Windows agent and just plain Powershell, but this can be adapted to Linux/Mac agents using bash, just one important thing to take care: Linux/MAc environment variables are case sensitive, Windows are not case sensitive.
We have this YAML build (hmmm the coloring on the second line is not correct):
It is just a simple build with one Powershell task, printing out the usual PATH environment variable, using the common syntax of Pipelines variables $(variable-name) and then printing out. We also configured the variables in the properties of the Azure Pipeline with a Demo variable:
When we run the build, we obtain the following output:
Check it out:
- The first Write-Host, has printed out the PATH environment variable without any problem, and using it as it was a simple Pipeline variable.
- The second Write-Host has printed out the content of the demo Pipeline variable but using it as a computer environment variable on the agent.
With this simple demo I hope it gets more clear, and you are ready to use this feature in your Azure Pipelines.