Azure-devops-docs: System Variables do not seem to be available in the template condition despite what this documentation page claims

Created on 17 Apr 2020  Â·  7Comments  Â·  Source: MicrosoftDocs/azure-devops-docs

Hey,

This page claims that:

Template expressions
...
Additionally, you have access to the variables context that contains all the variables specified in the YAML file plus the system variables.

I have a hard time trying to make it work. Consider the following pipeline definition. It uses template expression where it checks a value from System.PullRequest.PullRequestId system variable:

variables:
  systemVariableCondition: $[lt(0, variables['System.PullRequest.PullRequestId'])]
  ${{ if lt(0, variables['System.PullRequest.PullRequestId']) }}:
    dependentVariable: "some value"

phases:
- phase: Build
  displayName: Build
  steps:
  - powershell: |  
      Write-Host "dependentVariable: $(dependentVariable)"

Run-time variable systemVariableCondition is in this definition just to confirm that system variable in question has expected value.

Here is the output from the "Prepare job" step, please note that expected variable dependentVariable was not introduced:

##[section]Starting: Prepare job Build
Variables:
  systemVariableCondition:
    Parsing expression: <lt(0, variables['System.PullRequest.PullRequestId'])>
    Evaluating: lt(0, variables['System.PullRequest.PullRequestId'])
    Expanded: lt(0, '335')
    Result: 'True'
ContinueOnError: False
TimeoutInMinutes: 60
CancelTimeoutInMinutes: 5
Execution:
##[section]Finishing: Prepare job Build

So the step fails:

+ Write-Host "dependentVariable: $(dependentVariable)"
+                                  ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (dependentVariable:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CommandNotFoundException

##[error]PowerShell exited with code '1'.

Please elaborate what exactly this page means by stating that one has access to system variables in a template expression.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Pri1 devops-cicd-procestech devopprod doc-enhancement

Most helpful comment

@ferrata -- Alexander, I may have misunderstood your initial request.

@juliakm -- Julia, please look into this issue.

All 7 comments

@ferrata -- Alexander, thank you for your feedback. Please look here as well:

I am closing this issue now. You are welcome to @ mention me for any followup. We hope to hear from you again.

cc @juliakm for awareness

@WilliamAntonRohm,

Hey, can you please explain why you have closed the issue and what is the answer to my question?

Thanks

@ferrata -- Alexander, I may have misunderstood your initial request.

@juliakm -- Julia, please look into this issue.

I have the same issue.

variables:
  ${{ if eq( variables['Build.Reason'], 'PullRequest' ) }}:
    branchName: ${{ variables['System.PullRequest.SourceBranch'] }}
  ${{ if ne( variables['Build.Reason'], 'PullRequest' ) }}:
    branchName: ${{ variables['Build.SourceBranchName'] }}

stages:
- stage: Build
  displayName: Build image
  jobs:
  - job: Debug_vars
    steps:
    - script: echo $(Build.Reason), $(Build.SourceBranchName), $(branchName)

Result:

Generating script.
Script contents:
echo PullRequest, merge,

Hi @ferrata,

I think you faced the same problem as me currently. Could it be that you are currently not on an pull request build but an automated CI build?
It seems, that if your are not on a PR you do not have access to the variable in this context and you will get the aforementioned error. Just add a condition as shown below to make sure you are on a PR:
- job: Database condition: eq(variables['Build.Reason'], 'PullRequest')

System variables do not seem to expand. I have this issue

parameters:
  CheckoutDirectory: $(System.DefaultWorkingDirectory)

  - ${{ if eq(parameters.Type, 'terraform') }}:
    - template: ${{ parameters.CheckoutDirectory}}/builds/${{ parameters.Type }}/build.yml

it error complaining it's unable to find /$(System.DefaultWorkingDirectory/builds/terraform/build.yml

This si extremely frustrating, I've tried so many hacks to get this to work but it appears if any system variables are used it will not expand but anything else is fine.

In fact it appears ANY variable will not work, not just system.

@abelal83 Not all system variables are not available in templates. On the Predefined Variables page, there is a column titled Available in Templates? that says whether you can use the variable in a template.

Was this page helpful?
0 / 5 - 0 ratings