azure pipeline - set variable based on triggered branch azure pipeline - set variable based on triggered branch powershell powershell

azure pipeline - set variable based on triggered branch


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


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