Search code examples
azure-devopsazure-pipelines

Unable to pass Variable Group value to a template parameter


I am trying to pass a value to a template parameter using a variable defined in Azure Variable Groups.

Variable group:

Variable group

Here the pipeline

---
trigger: none
jobs:
  - job: PipelineTasks
    displayName: 'Pipeline Tasks'
    pool:
      vmImage: 'ubuntu-latest'
    variables:
    - group: Testing
    steps:
      - script: |
          echo "$(variable1)"
        displayName: 'Display Value'

      - template: "/template.yml"
        parameters:
          isEnabled: $(variable1)

Here's the template.yml

---
parameters:
  - name: isEnabled
    displayName: "Whether a scan is enabled or disabled"
    type: string
    default: true

steps:
    - ${{ if eq(parameters.isEnabled, true) }}:
        - script: |
            echo "${{ parmeters.isEnabled }}"
          displayName: 'Task 1'

I expect Task 1 to run, to my surprise it isn't.


Solution

  • The template expression is evaluated at compile-time before runtime, and the variables from variable group are evaluated at runtime. So, when evaluating the conditional set in the template expression, the value of the variable is still empty that cause the conditional always return False result.

    You can use condition key on the script task to directly check the value of the variable. It is evaluated at runtime.

    1. The pipeline main YAML.

      # azure-pipelines.yml
      
      jobs:
      - job: PipelineTasks
        displayName: 'Pipeline Tasks'
        variables:
        - group: Testing
        steps:
        - script: |
            echo "$(variable1)"
          displayName: 'Display Value'
      
        - template: /template.yml
      
    2. The template YAML.

      # template.yml
      
      steps:
      - script: |
          echo "variable1 = $(variable1)"
        displayName: 'Task 1'
        condition: eq(variables.variable1, 'true')
      
    3. Result.

    • When the value of "variable1" is "true", the step 'Task 1' gets executed.

      enter image description here

    • When the value of "variable1" is NOT "true", the step 'Task 1' gets skipped.

      enter image description here