Alamakanambra
Alamakanambra

Reputation: 7826

azure-pipelines - print variable to output

I am struggling with this simple task:

variables:
  myVariable: 'ValueFromVar'

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: ${myVariable} # prints empty

- task: PowerShell@2
  displayName: Display version of app
  inputs:
    targetType: 'inline'
    script: 'Write-Host "Version of app: ${myVariable}"' # prints empty

- task: Bash@3
  inputs:
    targetType: 'inline'
    script: echo '${myVariable}' # prints ${myVariable} 

What is the correct way how to print a variable to output within azure pipeline ??

Upvotes: 18

Views: 41486

Answers (2)

noontz
noontz

Reputation: 1987

A clean one line approach is to use the CmdLine task shortcut:

- script: echo $(myVariable)

Add display name etc. if needed e.g.

- script: echo $(myVariable)
  displayname: My variable value

Upvotes: 4

Yang Shen - MSFT
Yang Shen - MSFT

Reputation: 1214

About how to use custom variables, please check the doc Set variables in pipeline.

In your case, when you want to use the variable, it should be $(myVariable) instead of ${myVariable}.

Please refer to below demo:

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  myVariable: 'ValueFromVar'

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: '"$(myVariable)"'

- task: PowerShell@2
  displayName: Display version of app
  inputs:
    targetType: 'inline'
    script: 'Write-Host "Version of app: $(myVariable)"' 

- task: Bash@3
  inputs:
    targetType: 'inline'
    script: echo '$(myVariable)'

enter image description here

Upvotes: 21

Related Questions