While debugging the CI builds, sometimes it becomes necessary to take a peek at the values of the variables that are being passed to the environment used. It helps in understanding what is going on and why some steps are not working as desired in the build pipeline. There are different commands to check the environment variables in different types of agents, mostly based on the underlying Operating System. However, if you happen to use the Microsoft Hosted agents for your build pipelines, we can use one single line of code to print all environmental variables across all agents.
For this, we have to use the bash task in the list of available tasks in the Azure DevOps:
We now need to go the inline type and replace the content with below one line of code:
|# this is inline code|
|env | sort|
Alternatively, we can use below code in yaml pipeline under the job section:
|# code trimmed for brevity from azure-pipelines.yml|
|steps: # 'Steps' section is to be used inside 'job' section.|
|– task: Bash@3|
|script: 'env | sort'|
Now, we just need to commit the code with a suitable message and run the CI build. We should now be able to see all the environmental variables and their values from bash task output:
If you have defined any custom variables, it would list them too:
Do note that you can not print secretes stored inside variables using this method. It is also not a good idea to print secrets in build output as then they can be clearly viewed by anyone having access to build logs.
A full copy of the source code used in this blog post can be found at GitHub repository under branch blog/8378 and master branch