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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this is inline code | |
env | sort |
Alternatively, we can use below code in yaml pipeline under the job section:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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:
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
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. 🙂
LikeLike
Thanks, this helped a lot! Works with powershell, too.
LikeLike
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.
LikeLike