azure-pipelines.yml, how to override variables for pipeline

Viewed 6732

I'm trying to learn how to use the new yaml configured pipeline system for Azure Devops, and I'm having a bit of trouble getting my head around the way the variables are supposed to work.

When I setup the pipeline, it created a file azure-pipelines.yml and committed this to the master branch.

By default, this file looks like so...

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

My project is setup with the following build configurations... "prod", "staging", "develop".

What I'm confused about, is where am I supposed to override these default variables for the actual pipelines?

I can modify the values directly in this file, but that's not really going to work. When I merge the changes back from "master" to "staging" etc, then presumably the pipelines for these lower environments will then be trying to build with "prod" configuration.

Surely there must be some way to configure variables independent of the source code.

There are 2 places where I can see an option to add Variables...

When I choose "Edit" for the pipeline, up in the top right, there is a "Variables" button next to run.

I can add variables there, but they don't appear to do anything. They are not applied when I run the pipeline.

Also, to make things more confusing, when I choose to "Run pipeline", there is also an option to define variables, but likewise, these don't seem to do anything. The build still just runs with the pre-defined values from the yaml file.

2 Answers

Agree with Shayki Abramczyk. This method could manually override the variable value on the UI interface.

I would like to share the method of automatically appending values to variables.

You could use Expressions to judge different situations(e.g. build branch). Then you could set the value for different situations.

Here is an example:

trigger:
- '*'

pool:
  vmImage: 'windows-latest'

variables:
  ${{ if eq(variables['Build.SourceBranchName'], 'master') }}:
    buildConfiguration: Prod
  ${{ if eq(variables['Build.SourceBranchName'], 'staging') }}:
    buildConfiguration: Staging  

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.

      Write-Host $(buildConfiguration)

This sample code can select the corresponding value according to different trigger branch names. (master: Prod , staging: staging)

Hope this helps.

You can define the variables with Let users override this value when running this pipeline:

enter image description here

Use the variable in the build step with $(BuildConfiguration ).

When you run the build you can override the value:

enter image description here

enter image description here

enter image description here

Related