Choose build configuration according to branch name & parameter

3k views Asked by At

I am using an Azure build pipeline to build a DotNet application.

Until now, this build pipeline was configured to build in Release mode.

What I want is to select the build configuration depending on the branch a commit has been pushed on:

  • develop branch → Release mode
  • feature branch → Debug mode

I also want to override this behaviour thanks to a parameter I can choose before triggering a manual build.

Here is a diagram to show what I would like:

enter image description here

And here is the relevant YAML of my build pipeline and my attempt:

parameters:
  - name: buildConfiguration
    displayName: Build Configuration
    default: Default
    values:
      - Default
      - Release
      - Debug

variables:
  - name: runsOnDevelop
    value: ${{ eq( variables['Build.SourceBranch'], 'refs/heads/develop') }}
  - name: defaultConfigurationSelected
    value: ${{ eq(parameters['buildConfiguration'], 'Default') }}
  - name: releaseConfigurationSelected
    value: ${{ eq(parameters['buildConfiguration'], 'Release') }}
  - name: buildForRelease
    value: ${{ or(and( variables.runsOnDevelop, variables.defaultConfigurationSelected), variables.releaseConfigurationSelected )}}
    #value: $[ or(and( variables.runsOnDevelop, variables.defaultConfigurationSelected), variables.releaseConfigurationSelected )] # same result
  - name: buildConfiguration
    ${{ if variables.buildForRelease }}: # Conditional insertion: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#conditional-insertion
      value: 'Release'
    ${{ if not(variables.buildForRelease) }}:
      value: 'Debug'


steps:
  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: 'env | sort'

  - task: DotNetCoreCLI@2
    displayName: 'Build (${{ variables.buildConfiguration }})'
    condition: 
    inputs:
      command: build
      arguments: '--configuration ${{ variables.buildConfiguration }} -p:Version=$(Version)'

  - task: DotNetCoreCLI@2
    displayName: 'Run Tests'
    inputs:
      command: test
      arguments: '--configuration ${{ variables.buildConfiguration }}'

The output of the env | inline shows that buildConfiguration is always Release, even if I replace the long expression assigned to buildForRelease by ${{ false }} or false.

I suspect the variable to be evaluated as a string rather than a boolean, making and( variables.runsOnDevelop, variables.defaultConfigurationSelected) return true.

Thanks in advance.

EDIT: I tried with the following syntax, no luck:

  - name: buildForRelease
    value: ${{ or(
        and(
          eq(variables.runsOnDevelop, True),
          eq(variables.defaultConfigurationSelected, True)
        ),
        eq(variables.debugConfigurationSelected, True)
      ) }}
  - name: buildConfiguration
    ${{ if eq(variables.buildForRelease, True) }}:
      value: 'Release'
    ${{ if ne(variables.buildForRelease, True) }}:
      value: 'Debug'
1

There are 1 answers

0
Levi Lu-MSFT On BEST ANSWER

Your updated syntax is the correct one.

It was just I found a little mistake in your updated syntax. You mistpyed the expression for the value of buildForRelease. The second variables.debugConfigurationSelected should be variables.releaseConfigurationSelected

After i changed it to variables.releaseConfigurationSelected . It worked perfectly.

 - name: buildForRelease
    value: ${{ or(
        and(
          eq(variables.runsOnDevelop, True),
          eq(variables.defaultConfigurationSelected, True)
        ),
        eq(variables.releaseConfigurationSelected, True)
      ) }}
  - name: buildConfiguration
    ${{ if eq(variables.buildForRelease, True) }}:
      value: 'Release'
    ${{ if ne(variables.buildForRelease, True) }}:
      value: 'Debug'