azure-devops

Setting YAML variables depending on trigger branch


I'm trying to get my head around the yaml syntax for defining build pipelines in devops.

I'd like to set variables in the file dependent on which branch triggered the build.

# trigger:
 batch: true
 branches:
   include:
    - master
    - develop
    - staging

 variables:
    buildConfiguration: 'Release' # Can I set this according to the branch which triggered the build?

I've tried the following but can't seem to define variables twice.

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

 variables:
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/develop')
  buildConfiguration: 'Develop'

 variables:
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/release')
  buildConfiguration: 'Release'

Solution

  • I'd probably add a script step to calculate those. So create some sort of script that will check the value of $(Build.SourceBranch) and set the value of buildConfiguration like you normally would:

    echo '##vso[task.setvariable variable=buildConfiguration]something'
    

    For more information, look at the variables article for azure devops, it has info about those. basically all you have to do is echo out a string that looks like that.