'Azure pipeline set displayname of task based on condition

In the build pipeline I have a job with a powershell script setting the applicatiuon name based on a variable like this:

$applicationName = If ('$(configuration)' -eq 'Release') { 'Appname' } Else { 'Appname-Test' }
Write-Host "##vso[task.setvariable variable=applicationName]$applicationName"

I try to set the display name of the PublishBuildArtifacts@1 variable to the variable like this:

  - task: PublishBuildArtifacts@1        
    displayName: $[variables.applicationName] #  runtime variable

But this literally displays $[variables.applicationName] instead of the variable value. How can I change the displayname of a task based on a variable?



Solution 1:[1]

You can just use the variable in this way: $(variableName). for example:

pool:
  vmImage: 'windows-latest'

variables:
  test: "SomeValue"

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Write-Host "Hello World"'
  displayName: "The variable $(test)"

The result is:

enter image description here

Solution 2:[2]

I assume ${{..}} is the way to go. You know the configuration at compile time, so you can create the app name at compile time.

variables:
  ${{ if eq(variables.configuration, 'Release') }}:
    applicationName: 'Appname'
  ${{ if ne(variables.configuration, 'Release') }}:
    applicationName: 'Appname-Test'
  - task: PublishBuildArtifacts@1        
    displayName: ${{ variables.applicationName }}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shayki Abramczyk
Solution 2 Mike