8

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?

Matthias Herrmann
  • 2,650
  • 5
  • 32
  • 66

3 Answers3

11

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

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • oh nice, I'll check tomorrow. I guess in my attempts I used single quotes instead of double enquotation – Matthias Herrmann Jul 14 '20 at 16:03
  • It does only work with a `variables` declaration, but when I try to overwrite the value (for example "SomeValue" to "AnotherValue") the old value gets displayed – Matthias Herrmann Jul 15 '20 at 06:39
  • How do you overwrite the value? – Shayki Abramczyk Jul 15 '20 at 06:50
  • I have 2 jobs in my pipeline. In the first job I use powershell to overwrite the variable - see the code included in my question. Then I try to use this overwritten value as displayname in the second job. – Matthias Herrmann Jul 15 '20 at 06:52
  • 3
    @MatthiasHerrmann it because the steps name evaluated when the pipeline starts to run, so even you change the variable value and couldn't affect the step name. – Shayki Abramczyk Jul 15 '20 at 07:00
  • not working when referencing a variable in the `displayName` from a template file . any ideas on that situation? – spottedmahn Feb 01 '22 at 13:26
  • This also works for template task parameters. e.g. `displayName: 'Building ${{parameters.published_zip_file_name}}'` The parameter is defined inside the template file – adeel41 Sep 21 '22 at 16:09
5

It doesn't appear that this is currently possible. In both issues I found, they described this as a feature request.

https://github.com/MicrosoftDocs/azure-devops-docs/issues/2327

https://github.com/microsoft/azure-pipelines-yaml/issues/45

Matt
  • 3,658
  • 3
  • 14
  • 27
  • Based on the example in the other answer, this might have been completed. Might not be available in the on-premise version I was looking at. Whenever you change the "answer" I'll delete this one. – Matt Jul 14 '20 at 18:54
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 }}
Mike
  • 843
  • 7
  • 13