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

pipeline

Hope this helps!

Comments are closed.

%d bloggers like this: