Print all environment variables in Azure DevOps across Microsoft Hosted Agents

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:

use bash task from list of available tasks

We now need to go the inline type and replace the content with below one line of code:

Alternatively, we can use below code in yaml pipeline under the job section:

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:

build output from the bash task

If you have defined any custom variables, it would list them too:

build output from the bash task-2

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s