3

I use Azure Pipelines to Build my solution. When building manually the user can decide which Build Configuration to use. Now I want the default value (and so the value when triggered automatically) to be different according to the branch. This is my (non-working) approach:

name:   PR-$(Build.SourceBranchName)-$(Date:yyyyMMdd)$(Rev:.r)
trigger:
  - develop
  - test 
  - master
pool:
  vmImage: 'windows-latest'
parameters:
- name: BuildConfiguration
  displayName: Build Configuration
  ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/develop') }}:
  default: Debug
  ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/test') }}:
  default: Release
  ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/master') }}:
  default: Release
  values:
  - Debug
  - Release

The conditional part (${{...}) simply does not work. (*"A template expression is not allowed in this context")

Is there any other approach to make this happen?

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
Ole Albers
  • 8,715
  • 10
  • 73
  • 166

1 Answers1

9

This is not posssible to use expressins to select default value for paramataers.

Paramaters alco can't be optionla:

Parameters must contain a name and data type. Parameters cannot be optional. A default value needs to be assigned in your YAML file or when you run your pipeline. If you do not assign a default value or set default to false, the first available value will be used.

What you can do is use parameters and variables:

name:   PR-$(Build.SourceBranchName)-$(Date:yyyyMMdd)$(Rev:.r)
trigger:
  - develop
  - test 
  - master
pool:
  vmImage: 'windows-latest'
parameters:
- name: BuildConfiguration
  displayName: Build Configuration
  default: Default
  values:
  - Debug
  - Release
  - Default

variables:
- name: buildConfiguration
  ${{ if and(eq(variables['Build.SourceBranch'], 'refs/heads/develop'), eq(parameters.BuildConfiguration, 'Default')) }}:
    value: Debug
  ${{ elseif and(eq(variables['Build.SourceBranch'], 'refs/heads/test'), eq(parameters.BuildConfiguration, 'Default')) }}:
    value: Release
  ${{ elseif and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(parameters.BuildConfiguration, 'Default')) }}:
    value: Release
  ${{ else }}:
    value: ${{parameters.BuildConfiguration}}


steps:
- script: |
    echo $(buildConfiguration)
    echo $(Build.SourceBranch)
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107