Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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) 
like image 824
Nikolay Advolodkin Avatar asked Sep 20 '18 20:09

Nikolay Advolodkin


People also ask

How do I set my environment in Azure DevOps?

Create an environmentSign in to your organization: https://dev.azure.com/{yourorganization} and select your project. Select Pipelines > Environments > Create environment. Enter information for the environment, and then select Create.

How do I set environment variables in Azure?

To set environment variables when you start a container in the Azure portal, specify them in the Advanced page when you create the container. Under Environment variables, enter NumWords with a value of 5 for the first variable, and enter MinLength with a value of 8 for the second variable.

How do you use variables in Released pipeline Azure DevOps?

You define and manage these variables in the Variables tab in a release pipeline. In the Pipeline Variables page, open the Scope drop-down list and select "Release". By default, when you add a variable, it is set to Release scope. Share values across all of the tasks within one specific stage by using stage variables.


1 Answers

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") 
like image 133
Nikolay Advolodkin Avatar answered Sep 19 '22 07:09

Nikolay Advolodkin