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:


# this is inline code
env | sort

view raw

print-variables

hosted with ❤ by GitHub

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
inputs:
targetType: 'inline'
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:

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

3 thoughts on “Print all environment variables in Azure DevOps across Microsoft Hosted Agents

  1. Excellent.. Thanks a lot for this post.. Nowhere I am able to find this on the internet. Tried different keywords too. But finally found your page. 🙂

    Like

  2. What about scope on variables?
    I want to output the key and the value, of an specific scope.
    With this variables :
    key —— value———- scope
    KEY1 — devvalue —– DEV
    KEY1 — provalue —– PRO
    Im only able to print DEV value, but i need to oupput an specific scoped value.

    Like

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 )

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