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)
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.
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.
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.
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:
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With