Select Page

In this article we tackle all of the common challenges encountered when trying to do continuous integration with Azure DevOps aka VSTS aka TFS. Some critical topics that are tackled here are:

• How to work with Azure DevOps environment variables
• How to create a build pipeline

By the way, if you aren’t aware, TFS, VSTS and Azure DevOps are all technically the same solution. Over several years, Microsoft did a lot of rebranding that created the confusion between all of these products. Hence it went from TFS to VSTS to Azure DevOps relatively quickly. Causing lots of confusion. The latest version is called Azure DevOps. I hope that it stays with this name for the foreseeable future 🙏

## Environment

### How to set Sauce Labs environment variables in Azure DevOps with .NET Core?

These are instructions if you are working with .NET Core

1. Create environment variables in Azure DevOps

Create the environment variables in ADO and assign values to those variables.

Here’s an example

My recommendation is that you name the variables to something similar that I have above. Do not name your ADO variables the same as your Environment variables as that will cause you issues when you are trying to read them. So don’t name your ADO variable SAUCE_USER_NAME for example

2. Make sure you are reading values from environment variables in your code

For this to work, you need to be reading values into environment variables exactly like this:

var sauceUserName = Environment.GetEnvironmentVariable("SAUCE_USERNAME");
var sauceAccessKey = Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY");

3. Configure your YAML to read values from VSTS aka Aure DevOps and set them into system environment variables

Your YAML needs this piece of code to be able to set environment variables of the Azure DevOps box

#need to use the vso tasks so that the env variables persist trhough tasks in ADO
- powershell: |
Write-Host "Our Sauce Username in ADO is=> $($env:SAUCE_USER)";
Write-Host "Our Sauce Access Key in ADO is=> $($env:SAUCE_KEY)";
Write-Host ("##vso[task.setvariable variable=SAUCE_USERNAME]$($env:SAUCE_USER)")
Write-Host ("##vso[task.setvariable variable=SAUCE_ACCESS_KEY]$($env:SAUCE_KEY)")

Here is the full YAML file that works.

### How to set Sauce Labs environment variables in Azure DevOps with .NET Framework?

There might be a few ways to do this, but I found that using a Powershell script works fine for me.

### Using a Powershell script

1. First you need to create some environment variables in your Azure DevOps UI that you want to use for values. This is an example of a variable that I would like to set on the test agent for my automation scripts. For example sauce.userName. I will use the value of this variable(sauce.userName) and have a Powershell script set it in my System Environment Variables of the test agent when my automation is running. That way, the value of this variable isn’t exposed to the public.

2. Next, you will want to create a Powershell script that you attach to your solution. Here’s my solution layout.

Don’t forget to make sure to copy your Powershell script to output directory on build in your .csproj

Here is what you want in your Powershell script.

Param(
[string]$sauceUserName, [string]$sauceAccessKey,
[string]$rdcVodQaNativeAppApiKey, [string]$rdcSauceDemoIosRdcApiKey
)
Write-Output "sauce.userName value from ADO was passed as a Argument in the ADO Task called $env:SAUCE_USERNAME " + "to sauceUserName variable in the Posh. This is the value found=>$sauceUserName"
Write-Output "sauce.accessKey that was passed in from Azure DevOps=>$sauceAccessKey" Write-Output "sauce.rdc.VodQaNativeAppApiKey stored in Azure DevOps=>$rdcVodQaNativeAppApiKey"
Write-Output "sauce.rdc.SauceDemoIosRdcApiKey stored in Azure DevOps=>$rdcSauceDemoIosRdcApiKey" [Environment]::SetEnvironmentVariable("SAUCE_USERNAME", "$sauceUserName", "User")
[Environment]::SetEnvironmentVariable("SAUCE_ACCESS_KEY", "$sauceAccessKey", "User") [Environment]::SetEnvironmentVariable("VODQC_RDC_API_KEY", "$rdcVodQaNativeAppApiKey", "User")
"C:\Source\Github\dot-net-sauce\SauceExamples\SeleniumNunit\bin\Debug\SeleniumNunit.dll"'**\$(BuildConfiguration)\**\SeleniumNunit.dll' Column A shows the local path. Column B shows how the path looks in Azure DevOps. ### How to pass parameters to test code from a build or release pipeline? A: Use a runsettings file to pass values as parameters to your test code. For example, in a release that contains several stages, you can pass the appropriate app URL to each the test tasks in each one. The runsettings file and matching parameters must be specified in the Visual Studio Test task. Source ### How to add a status badge to your Github repo The instructions on this are really good from Microsoft and you can follow them here in the Get the status badge section Finally, you want to have a powershell step in your YAML that executes this Powershell script and passes in the values from the variables that you set in the Azure DevOps UI. Below is what my YAML step looks like and I’m basically setting the SAUCE_USERNAME and SAUCE_ACCCESSKEY variables. In order for the YAML to understand where these variables come from, you need to convert sauce.userName to SAUCE_USERNAME. That’s the Azure convention. Read more about working with variables. Basically the value stored in sauce.userName is passed in as a variable called $env:SAUCE_USERNAME . It’s the same exact variable, but when you convert it from the ADO UI to YAML, that’s the convention, I know, it’s weird…

sauce.AccessKey = \$env:SAUCE_ACCESSKEY

word.a.b.c = WORD_A_B_C