Parameterizing a Variable in an Azure DevOps Build Pipeline

Antoine NAFFETAT
Published by Antoine NAFFETAT
Category : DevOps
22/11/2023

Introduction

Some build pipelines serve multiple branches of the same project. As such, it may be necessary to have different parameters depending on the branch. In this article, we’ll take as a need the fact of changing the build configuration (debug or release) according to the branch.

Since it is not possible to choose the scope of a variable based on the branch used in a pipeline, we will modify the pipeline variables according to the branch with an inline PowerShell task.

 

Set Up

Once the build pipeline is created, the first step is to create a variable in the pipeline. We name this variable “system.BuildConfig”, but you can choose any name:

 

pipeline variables

 

Next, we will add a “Run Inline Powershell” task to modify this variable based on the branch name. In our example, we want a debug configuration if the branch name is “dev”, otherwise a release configuration.

Param( 
[string]$branchName 
) 

If($branchName -eq 'dev') 
{ 
Write-Host "##vso[task.setvariable variable=system.buildConfig]build" 
} 
Else 
{ 
Write-Host "##vso[task.setvariable variable=system.buildConfig]release" 
}

 

PowerShell pipeline tasks

 

Finally, in the “Visual Studio build” task, we will use our variable for the build configuration.

 

Visual Studio pipeline build task

 

It’s possible to modify any pipeline variable this way depending on the different needs of the projects !