Azure Pipelines coalesce not being evaluated

Viewed 1762

I've got template where I want to evaluate coalesce(${{ parameters.pipeline }}, $(System.DefinitionId)).

As far as I know, this is the proper syntax:

  - task: DownloadBuildArtifacts@0
    inputs:
      buildType: 'specific'
      project: '$(System.TeamProjectId)'
      pipeline: $[ coalesce(${{ parameters.pipeline }}, $(System.DefinitionId)) ]
      buildVersionToDownload: latestFromBranch
      branchName: $[ coalesce(${{ parameters.branchName }}, $(Build.SourceBranch)) ]
      allowPartiallySucceededBuilds: true
      downloadType: 'single'
      downloadPath: '$(Pipeline.Workspace)'
      artifactName: ${{ parameters.artifact }}

However, when I run this, I get this error:

Definition name $[ coalesce(141, 342) ] didn't correspond to a valid definition

I think this likely means that it's not evaluating the expression and using the literal string '$[ coalesce(141, 342) ]'.

It seems that Build.DefinitionId is only available at runtime, else I'd set the parameter default to ${{ Build.DefinitionId }}, and just set pipeline: ${{ parameters.pipeline }}.

I've tried a dozen different variants using $[ ], ${{ }}, and $( ) syntaxes both inside and outside the coalesce(). None of them work, but this one is the closest since it actually substitutes the variables in correctly.

Any ideas?

1 Answers

It turned out that I needed to place the expression in the job's variables, as well as use variables['System.DefinitionId'] instead of the $( ) syntax.

- job: #...
  variables:
    pipeline: $[ coalesce(${{ parameters.pipeline }}, variables['System.DefinitionId']) ]
    branchName: $[ coalesce(${{ parameters.branchName }}, variables['Build.SourceBranch']) ]
  steps:
  # ...
  - task: DownlaodBuildArtifact@0
    inputs:
      # ...
      pipeline: $(pipeline)
      branchName: $(branchName)
Related