How to configure environment variables in Azure DevOps pipeline?

azure devops pipeline parameters
azure devops output variables
azure devops conditional variables
azure devops release pipeline variables
azure devops nested variables
azure devops variable substitution
azure pipeline parameters
azure devops bash script variables

I have an Azure Function (.NET Core) that is configured to read application settings from both a JSON file and environment variables:

var configurationBuilder = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.json", optional: true)

BuildAgentMonitorConfiguration configuration = configurationBuilder.Get<BuildAgentMonitorConfiguration>();

appsettings.json has the following structure:

  "ProjectBaseUrl": "",
  "ProjectName": "my-project",
  "AzureDevOpsPac": ".....",
  "SubscriptionId": "...",
  "AgentPool": {
    "PoolId": 38,
    "PoolName": "MyPool",
    "MinimumAgentCount": 2,
    "MaximumAgentCount": 10
  "ContainerRegistry": {
    "Username": "mycontainer",
    "LoginServer": "",
    "Password": "..."
  "ActiveDirectory": {
    "ClientId": "...",
    "TenantId": "...",
    "ClientSecret": "..."

Some of these settings are configured as environment variables in the Azure Function. Everything works as expected:

The problem now is to configure some of these variables in a build pipeline, which are used in unit and integration tests. I've tried adding a variable group as follows and linking it to the pipeline:

But the environment variables are not being set and the tests are failing. What am I missing here?

I also have the same use case in which I want some environment variable to be set up using the azure build pipeline so that the test cases can access that environment variable to get the test passed. Directly setting the env variable using the EXPORT,ENV command does not work for the subsequent task so to have the environment variable set up for subsequent task follow the syntax as mentioned on ie the task.set variable with the script tag

Correct way of setting ENV variable using build pipeline

- script: |
    echo '##vso[task.setvariable variable=LD_LIBRARY_PATH]$(Build.SourcesDirectory)/src/Projectname/bin/Release/netcoreapp2.0/x64'
  displayName: set environment variable for subsequent steps

Please be careful of the spaces as its is yaml. The above script tags set up the variable LD_LIBRARY_PATH (used in Linux to define path for .so files) to the directory defined.

This style of setting the environment variable works for subsequent task also , but if we set the env variable like mentioned below the enviroment variable will be set for the specefic shell instance and will not be applicable for subsequent tasks

Wrong way of setting env variable :

- script: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(Build.SourcesDirectory)/src/CorrectionLoop.HttpApi/bin/Release/netcoreapp2.0/x64
  displayName: Set environment variable

You can use the similar syntax for the setting up your environment variable.

Build a Jenkins Pipeline - DevOps CI/CD, Use popular tools to demonstrate Continuous Integration & Continuous Delivery. The question is - how do you setup environment variables in a DevOps pipeline? The answer is easy - when a pipeline executes, Azure will place all pipeline variables into environment variables, so any tools, scripts, tasks, or processes you run as part of the build can access parameters through the environment.

If you are using bash then their example does not work, as they are referring incorrectly to the variables in the documentation. Instead it should be:

Store secret
echo "##vso[task.setvariable variable=sauce]crushed tomatoes"
echo "##vso[task.setvariable variable=secret.Sauce;issecret=true]crushed tomatoes with garlic"
Retrieve secret
Wrong: Their example
echo "No problem reading $1 or $SAUCE"
echo "But I cannot read $SECRET_SAUCE"
echo "But I can read $2 (but the log is redacted so I do not spoil the secret)"
echo "No problem reading $(sauce)"
echo "But I cannot read $(secret.Sauce)"

Define variables, Azure Pipelines | Azure DevOps Server 2019 | TFS 2018 | TFS 2017 | TFS These variables are automatically set by the system and read-only. For example, a variable named MySecret can be input using the environment variable AZURE_DEVOPS_EXT_PIPELINE_VAR_MySecret. Example. The following command updates the Configuration variable with the new value config.debug in the pipeline with ID 12. It specifies that the variable is not a secret and shows the result in table format.

I ran into this as well when generating EF SQL scripts from a build task. According to the docs, the variables you define in the "Variables" tab are also provided to the process as environment variables.

Notice that variables are also made available to scripts through environment variables. The syntax for using these environment variables depends on the scripting language. Name is upper-cased, . replaced with _, and automatically inserted into the process environment

For my instance, I just had to load a connection string, but deal with the case difference of the key between the json file and the environment:

var config = new ConfigurationBuilder()
              .AddJsonFile("appsettings.json", true, true)

var connectionString = config["connectionString"] ?? config["CONNECTIONSTRING"];

Predefined variables, The question is - how do you setup environment variables in a DevOps pipeline? The answer is easy - when a pipeline executes, Azure will  Create your variables in your Azure DevOps pipeline and provide those variables the values. Create a Powershell script that you will run in the beginning to set your Env Variables. This is what my Posh looks like.

Using Environment Variables in Azure DevOps Pipelines, I also have the same use case in which I want some environment variable to be set up using the azure build pipeline so that the test cases can  Within the script environment, when a pipeline variable is made available, it's done so by creating an environment variable. These environment variables can then be accessed via the language of choice's typical methods. Pipeline variables exposed as environment variables will always be upper-cased and any dots replaced with underscores.

How to configure environment variables in Azure DevOps pipeline , Before we dive into the specifics of variables, what are they and how do In a pipeline, you can set and read variables almost and pass their values to other parts of the system. According to this, using outputs in a different job is not supported in Classic UI Format.. As workarounds in this scenario, you can share variables via Pipeline Variables(share variables across jobs in same pipeline) or Variable Groups(share variables across pipelines that use same Variable Group, it also works across jobs).

Understanding Azure DevOps Variables [Complete Guide], A key piece of info about setting variables in Azure DevOps pipelines with a script​. This ended up being a bit time consuming for me, because  The environment variable should be referenced as AGENT_JOBSTATUS. The older agent.jobstatus is available for backwards compatibility. Agent.MachineName: The name of the machine on which the agent is installed. Agent.Name: The name of the agent that is registered with the pool. If you are using a self-hosted agent, then this name is specified by

  • are those secret values? if they are, i think you need to explicitly map them to the task
  • @4c74356b41 yes these are secrets. I'm not sure if I understand what do you mean by "explicitly map them to the task", could you please elaborate?
  • check this answer:… second code block shows how to map secret variables to env variables for a given task
  • Thanks @4c74356b41 - if I understood correctly, these secret variables are not set as environment variables, I need to set them using bash or powershell?
  • i dont know how to access them using bash or powershell, but you can assign second code block to any task and it would work, that is using yaml, i dont know off the top of my head how to use those without yaml, there should be a way, but I'm only using yaml for the last year at least, dont remember regular pipelines, feel free to upvote that answer ;)
  • Thanks for sharing. Unfortunately I'm not using yaml, but hopefully this will be useful to other people with the same problem.
  • Ok but I wonder if you are not using yaml how you are setting up the build pipeline in azure. Even the GUI of azure build pipeline is mapped to azure-pipeline.yml file.
  • @RuiJarimba you can mark this as the answer so that it could be helpful for others.It seems that people have preferred it over other answers.
  • I'm glad it is helpful to others, but I can't mark it as an answer to my question - as I mentioned in my first comment, I was looking for a solution that doesn't use yaml.
  • Thanks for sharing, I'll give it a try sooner or later.