13

i'm trying to set the BuildConfiguration based on the triggered branch using powershell

anyone know how this could be done?

switch($env:Build.SourceBranchName) {
   'master' {$env:BuildConfiguration = Release; break;} 
   'staging' {$env:BuildConfiguration = Staging; break;} 
   'develop' {$env:BuildConfiguration = Dev; break;} 
}

Manhao Chen
  • 399
  • 2
  • 8

2 Answers2

26

you can set variables at the top of your yaml pipeline and use them at will.

variables:
  ${{ if eq(variables['Build.SourceBranchName'], 'main') }}: 
    deployTarget: prod
  ${{ if eq(variables['Build.SourceBranchName'], 'develop') }}: 
    deployTarget: dev

and to use:

- task: CmdLine@2
  displayName: Display deployment
  inputs:
  script: |
    echo '${{ variables.deployTarget }}'
MaxThom
  • 1,165
  • 1
  • 13
  • 20
15

finally got this working with

switch(${env:BUILD_SOURCEBRANCH}) {
   'refs/heads/master' {Write-Host "##vso[task.setvariable variable=BuildConfiguration]Release"; } 
   'refs/heads/staging' {Write-Host "##vso[task.setvariable variable=BuildConfiguration]Staging"; } 
   'refs/heads/develop' {Write-Host "##vso[task.setvariable variable=BuildConfiguration]Dev"; } 
}
Manhao Chen
  • 399
  • 2
  • 8