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

75,392

Solution 1

The easiest method is to pass the Azure DevOps(ADO) Env Variable values into your keys like this:

- task: DotNetCoreCLI@2
  displayName: 'Run tests'
  env:
    SAUCE_USERNAME: $(sauceUsername) #this will store the value from 'sauceUsername' into SAUCE_USERNAME
    SAUCE_ACCESS_KEY: $(sauceKey)

Displaying or using the value will work if you try

- bash: echo $(SAUCE_USERNAME) # will output our username stored in SAUCE_USERNAME env variable

And if you are referencing SAUCE_USERNAME in your code, the code will pick up the value from the Azure server.

This article has a good explanation

Previously, I also used Powershell, but this method is more involved and convoluted:

  1. Create your variables in your Azure DevOps pipeline and provide those variables the values.
  2. Create a Powershell script that you will run in the beginning to set your Env Variables. This is what my Posh looks like.
  3. Run this Posh in the beginning as a separate step in your CI pipeline and this will set the environment variables for the VM that's being used to run your pipeline.

This is another detailed article that could help you with this.

As per request, I'm also attaching the PowerShell code that makes this possible.

Param(
[string]$sauceUserName,
[string]$sauceAccessKey,
[string]$sauceHeadlessUserName,
[string]$sauceHeadlessAccessKey
)
Write-Output "sauce.userName that was passed in from Azure DevOps=>$sauceUserName"
Write-Output "sauce.accessKey that was passed in from Azure DevOps=>$sauceAccessKey"
Write-Output "sauce.headless.userName that was passed in from Azure DevOps=>$sauceHeadlessUserName"
Write-Output "sauce.headless.access.key that was passed in from Azure DevOps=>$sauceHeadlessAccessKey"

[Environment]::SetEnvironmentVariable("SAUCE_USERNAME", "$sauceUserName", "User")
[Environment]::SetEnvironmentVariable("SAUCE_ACCESS_KEY", "$sauceAccessKey", "User")
[Environment]::SetEnvironmentVariable("SAUCE_HEADLESS_USERNAME", "$sauceUserName", "User")
[Environment]::SetEnvironmentVariable("SAUCE_HEADLESS_ACCESS_KEY", "$sauceAccessKey", "User")

Solution 2

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)'

Solution 3

I tried using both of the following syntax as suggested in answers above, but the environment variable was always blank when trying to use it in tasks further down in the pipeline (like during the build or while running tests).

[Environment]::SetEnvironmentVariable("SAUCE_USERNAME", "$(sauceUserName)", "User")

variables:
  sauceUserName: '$(sauceUserName)'

What worked for me was to use the syntax to write Azure DevOps variables in an inline PowerShell script task:

- task: PowerShell@2
  displayName: Add the username as an environment variable so the tests can find it.
  inputs:
    targetType: 'inline'
    script: |
      Write-Host "Making the sauceUsername available as an environment variable."
      Write-Host "##vso[task.setvariable variable=SAUCE_USERNAME;]$(sauceUserName)"

My build task was then able to find the environment variable, and I could also access it in PowerShell script tasks further down in the pipeline with code like:

- task: PowerShell@2
  displayName: Display the environment variable value for debugging purposes.
  inputs:
    targetType: 'inline'
    script: |
      [string] $username= $Env:SAUCE_USERNAME
      Write-Host "The SAUCE_USERNAME environment variable value is '$username'."

Solution 4

We fought with this for hours thinking it was due to not being able to set environment variables but in the end it had nothing to do with that.

Here's some of our notes from trying to figure this out for Azure DevOps:

  • Pipeline Variables are Environment Variables and are injected into the pipeline in the first step of the process. You can confirm this by looking at the logs for the Initialize job task which lists all the environment variables.
  • In order to prove that the pipeline variables are in the system you can add a powershell task and inline:
Write-Host "Pipeline Variable: $(FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD)"
Write-Host "Environment Variable: $Env:FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD"
  • The () around the pipeline variable are critical if you want to use the pipeline variable somewhere - e.g. $(myVar)

Unrelated to the users question but perhaps helpful to those who are trying to upload to AppStore using two-factor auth (2FA). This is the documentation we were following:

  • We had to set FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD and FASTLANE_SESSION in order to get it to bypass the 2FA. We think the session one allowed us to log into AppStoreConnect but the App specific password was used for uploading. The logs sorta hint that's what's happening although you never see the session variable used.
Share:
75,392
Nikolay Advolodkin
Author by

Nikolay Advolodkin

Updated on March 13, 2021

Comments

  • Nikolay Advolodkin
    Nikolay Advolodkin over 3 years

    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)