Search code examples
azure-devopsazure-pipelines

Azure devops pipelines conditional name


I am using azure-devops pipelines but I am having problems to set the name of the build.

Here is a normal build definition.

pool:
  vmImage: 'VS2017-Win2016'

name: myBuildName

steps:
- task: NuGetToolInstaller@0

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

What I would like to do is to set the name with a conditional check. If (something) then X otherwise Y

I have checked the conditional documents, but no luck.

Here is what I would like to do, but obviously does not work

# if ReleaseNumber var exists
if ($(ReleaseNumber))
  name: $(ReleaseNumber).$(Build.BuildId)
else
  name: $(date:yyyyMMdd)$(rev:.r)

Solution

  • Azure DevOps YAML doesn't support conditions in the values like you tried to do.

    The conditional documents you looked is for jobs/tasks execution, you can specify when the task will be executed with a custom condition.

    At workaround, you can add a PowerShell task that will update the build name according to your condition.

    For example, keep the $(date:yyyyMMdd)$(rev:.r) in the name and run this script during the build:

    if ($env:ReleaseNumber){
      Write-Host "##vso[build.updatebuildnumber]$env:ReleaseNumber.$env:Build_BuildId"
      }
    else{
      Write-Host "Release Number not exist, build name not changed"
      }