Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

azure pipeline - set variable based on triggered branch

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;} 
}

like image 959
Manhao Chen Avatar asked Sep 26 '19 22:09

Manhao Chen


2 Answers

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 }}'
like image 157
MaxThom Avatar answered Oct 05 '22 09:10

MaxThom


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"; } 
}
like image 32
Manhao Chen Avatar answered Oct 05 '22 07:10

Manhao Chen