How to set and read user environment variable in Azure DevOps Pipeline?

Nikolay Advolodkin picture Nikolay Advolodkin · Sep 20, 2018 · Viewed 45.3k times · Source

I have some test automation code that reads some values from an environment variable stored on my local machine, like this:

Environment.GetEnvironmentVariable("SAUCE_USERNAME", EnvironmentVariableTarget.User);

I'm trying to use Azure Pipelines to create this variable during pipeline execution and then read it in my test automation code. Using a YAML file.

Im reading this variable in the VS Test step of the Azure Pipeline. So if I set the variable, it has to be for the life of the Azure Pipeline.

I've tried to use the documentation here but have been unsuccessful.

Tried this code below as well but it fails with this error:

azure-pipelines.yml (Line: 39, Col: 1, Idx: 1252) - (Line: 39, Col: 1, Idx: 1252): While scanning a simple key, could not find expected ':'.

# Create a secret variable
- powershell: |
Write-Host '##vso[task.setvariable variable=sauce.userName;issecret=true]abc'

# Attempt to output the value in various ways
- powershell: |
# Using an input-macro:
Write-Host "This works: $(sauce.userName)"

# Using the env var directly:
Write-Host "This does not work: $env:SAUCE_USERNAME"

# Using the mapped env var:
Write-Host "This works: $env:SAUCE_USERNAME"
env:
SAUCE_USERNAME: $(sauce.userName)

Answer

Eliezer Cazares picture Eliezer Cazares · Oct 25, 2018

set up pipeline variables and then try this mapping in your yaml file:

# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

pool:
  vmImage: 'VS2017-Win2016'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  yourEnvVar: '$(yourPipelineVariable)'
  yourOtherEnvVar: '$(yourOtherPipelineVariable)'