How to see the Predefined Variables in Azure Devops
J

2

14

I want to see the values that is path of predefined variable like for $(System.DefaultWorkingDirectory) i want to see value stored in it. I am unable to find this variable value so where can i find it in Azure devops.

In simple words, how could i check what was the Build.SourcesDirectory or Build.Repository.LocalPath used in that particular release pipeline?

Jezebel answered 10/4, 2020 at 6:59 Comment(4)
The easiest way is to print them out in the script tasks as @Krzysztof answered below.Adam
Any other way ? @LeviLu-MSFTJezebel
You can also task Display all variables in your pipeline.Adam
@LeviLu-MSFT I am unable to see system variables there.Jezebel
P
28

I'm not sure if you find a specific place in Azure DevOps what values are behind. Values may differ a bit depending in which OS you select for your agent. However you can alwasy print them out. Please check doc here.

steps:
  - bash: echo $(System.DefaultWorkingDirectory)

To print all variables you can use this step (since variables are also available to scripts through environment variables)

 steps: # 'Steps' section is to be used inside 'job' section.
  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: 'env | sort'

enter image description here

Another option which works for Windows and Linux would be (all credits to Joe):

- pwsh: (gci  env:* | sort-object name)

You can also use third party extension Print all variables

  - task: printAllVariables@1
    displayName: 'Print all variables via extension'

Or expression like:

  - ${{ each var in variables }}:
    - pwsh: Write-Host "${{ var.Key }} - ${{ var.Value }}"
      displayName: 'Print variables via expression in the loop'

Here is an example pipeline:

trigger: none
pr: none

name: Display pipeline variables

variables:
- group: DisplayPipelineVariables
- name: DB_HOSTNAME
  value: 10.123.56.222
- name: DB_PORTNUMBER
  value: 1521
- name: USERNAME
  value: TEST
- name: PASSWORD
  value: TEST
- name: SCHEMANAME
  value: SCHEMA  
- name: ACTIVEMQNAME
  value: 10.123.56.223
- name: ACTIVEMQPORT
  value: 8161

pool:
  vmImage: $(imageName)

jobs:
- job: AllEnvironmentVariables
  strategy:
    matrix:
      linux:
        imageName: 'ubuntu-latest'
      mac:
        imageName: 'macOS-latest'
      windows:
        imageName: 'windows-latest'
  steps:
  - script: env | sort
    displayName: Display all environment variables

- job: PipelineVariablesViaExtension
  strategy:
    matrix:
      linux:
        imageName: 'ubuntu-latest'
      mac:
        imageName: 'macOS-latest'
      windows:
        imageName: 'windows-latest'
  steps:
  - task: printAllVariables@1
    displayName: 'Print all variables via extension'

- job: PipelineVariablesViaExpression
  strategy:
    matrix:
      linux:
        imageName: 'ubuntu-latest'
      mac:
        imageName: 'macOS-latest'
      windows:
        imageName: 'windows-latest'
  steps:
  - pwsh: Write-Host "${{ convertToJson(variables) }}"
    displayName: 'Print all variables via expression'

- job: PipelineVariablesViaExpressionInLoop
  strategy:
    matrix:
      linux:
        imageName: 'ubuntu-latest'
      mac:
        imageName: 'macOS-latest'
      windows:
        imageName: 'windows-latest'
  steps:
  - ${{ each var in variables }}:
    - pwsh: Write-Host "${{ var.Key }} - ${{ var.Value }}"
      displayName: 'Print variables via expression in the loop'
Prospero answered 10/4, 2020 at 8:10 Comment(4)
No worries. All things above will work for you. I just mentioned this that value may differ depending OS.Prospero
This works on the cloud but not when I use my local machine as a build agent, even though I have bash installed on it (Windows 10). It just says, "'c:\WINDOWS\system32\bash.exe' failed with exit code 4294967295". Is there some sort of powershell equivalent of this?Palgrave
Answering my own question in a comment: This powershell script works for both: - pwsh: (gci env:* | sort-object name)Palgrave
An even better option is: - pwsh: (gci env:* | sort-object name | Format-Table -AutoSize). This ensure the whole key of a variable name is shown.Impermanent
F
-1

Simpler answer:

  - bash: |
      env | sort
    displayName: 'Debug: Show Env Vars'
Facient answered 30/3, 2023 at 20:37 Comment(1)
The existing answer already says this.Ghyll

© 2022 - 2025 — McMap. All rights reserved.