Different variables for different branches in your Azure DevOps Pipeline

Yesterday I needed to create a pipeline to publish a zip file to a storage account on Azure. The zip file is used to publish some content to a website. I use the Azure DevOps wiki to write the content. A development branch, that contains the working version, and the master branch that contains the production version.

Every time a change is made to the development branch of the wiki, I want to publish a new version of the zip file to the storage account. In order to do this I created a YAML pipeline. However, every time when I want to publish to production, I merge the changes from the development wiki and trigger the same pipeline.

Azure DevOps has a great mechanism to change variables in the different stages. You can use variable groups or stage variables, but for different branches there is no such thing. After some googling and puzzling I ended up with a simple inline Powershell script.

switch(${env:BUILD_SOURCEBRANCH}) {
'refs/heads/master' {Write-Host "##vso[task.setvariable variable=publishUrl]https://admiral-PROD.azurefd.net"; }
'refs/heads/development' {Write-Host "##vso[task.setvariable variable=publishUrl]https://admiral-d.azurefd.net"; }
default {Write-Host "##vso[task.setvariable variable=publishUrl]NONE"; }

The ##vso[task.setvariable variable=publishUrl]NONE syntax sets a variable in the pipeline that can be used later in the normal way. To read more on this please check this link

Eventually my YAML pipeline looked like this


Hope this helps!

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: