Search code examples
azure-devopsnugetazure-pipelinesazure-pipelines-yamlazure-devops-pipelines

How to create conditional build properties in DevOps build pipeline


I have the following build pipeline task:

  - task: NuGetCommand@2
    displayName: 'Pack CodingStyles.nuspec'
    inputs:
      command: 'pack'
      packagesToPack: 'src\CodingStyles\CodingStyles.nuspec'
      packDestination: 'src\CodingStyles\bin'
      versioningScheme: 'off'
      buildProperties: '-NoDefaultExcludes -Version $(Build_Major).$(Build_Minor).$(Build_Patch)'

However, if the variable $IsPreRelease is true, I want to add another build property as well:

buildProperties: '-NoDefaultExcludes -Version $(Build_Major).$(Build_Minor).$(Build_Patch) -Suffix beta'

A few thoughts on how to do this:

  1. I could have two different versions of this task, each with a condition: based on the variable. This would probably work, but it's quite redundant since everything else is the same.
  2. Run a Powershell task instead of the NuGetCommand task, and build my command line dynamically.

Any options I'm missing?


Solution

  • You are missing one option:

      - task: NuGetCommand@2
        displayName: 'Pack CodingStyles.nuspec'
        inputs:
          command: 'pack'
          packagesToPack: 'src\CodingStyles\CodingStyles.nuspec'
          packDestination: 'src\CodingStyles\bin'
          versioningScheme: 'off'
          ${{ if ne(variables['IsPreRelease'], 'true') }}:
            buildProperties: '-NoDefaultExcludes -Version $(Build_Major).$(Build_Minor).$(Build_Patch)'
          ${{ if eq(variables['IsPreRelease'], 'true') }}:
            buildProperties: '-NoDefaultExcludes -Version $(Build_Major).$(Build_Minor).$(Build_Patch) -Suffix beta'
    

    enter image description here