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 :-)
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'
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With