Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting YAML variables depending on trigger branch

Tags:

azure-devops

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'

Thanks for your help :-)

like image 235
Damien Sawyer Avatar asked Dec 17 '22 16:12

Damien Sawyer


2 Answers

If anyone's interested, I ended up with this.


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

[truncated] 

 #https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#set-a-job-scoped-variable-from-a-script    
 - pwsh: |
    If ("$(Build.SourceBranch)" -eq "refs/heads/master") {
      Write-Host "##vso[task.setvariable variable=buildConfiguration;]Release"
    }
    If ("$(Build.SourceBranch)" -eq "refs/heads/develop") {
      Write-Host "##vso[task.setvariable variable=buildConfiguration;]Debug"
    }
 - script: | 
    echo building configuration $(buildConfiguration)

 - task: VSBuild@1
   inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    clean: true
    vsVersion: '15.0'


like image 51
Damien Sawyer Avatar answered May 21 '23 17:05

Damien Sawyer


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'
like image 30
4c74356b41 Avatar answered May 21 '23 15:05

4c74356b41