legends2k
legends2k

Reputation: 32994

Using a matrix value as template parameter in Azure pipeline YAML

To a template.yaml I've introduced a new parameter buildTests

parameters:
  - name: isDebug
    type: string
    default: true
  - name: buildLibs
    type: string
    values:
    - True
    - False
    default: False
  - name: buildTests
    type: string
    values:
    - True
    - False
    default: True

In a pool.yaml we do

parameters:
  # Values marked as True and False instead of true and false
  # to facilitate passing forward as string values to another tool.
  - name: buildLibs
    type: string
    values:
    - True
    - False

jobs:
  - job:
    strategy:
      matrix:
        x64_debug:
          isDebug: true
          buildTests: False
        x64_release:
          isDebug: false
          buildTests: True

    steps:
        - template: template.yaml
          parameters:
            isDebug: $(isDebug)
            buildLibs: ${{ parameters.buildLibs }}
            buildTests: $(buildTests)

However, when I try to run the pipeline with this YAML, I get an error:

pool.yml: The 'buildTests' parameter value '$(buildTests)' is not a valid value.

I understand that some variables are substituted at compile-time, while some at run-time. However, if isDebug works, why does buildTests not?

Upvotes: 0

Views: 2038

Answers (1)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35544

pool.yml: The 'buildTests' parameter value '$(buildTests)' is not a valid value.

Reproduce the same issue when using the same YAML template.

The cause of the issue is that you set the value list for the buildTests parameter.

  - name: buildTests
    type: string
    values:
    - True
    - False
    default: True

When you set the value list of the parameters, it requires the input value at compile time.

If you remove it, the macro format type variable($(var)) can update the value of template parameters before a task executes.

To solve this issue, you can remove the value list of the buildTests Parameter.

For example:

template.yaml

parameters:
  - name: isDebug
    type: string
    # default: true
  - name: buildLibs
    type: string
    values:
    - True
    - False
    default: False
  - name: buildTests
    type: string
    default: True

steps:
- script:  echo ${{ parameters.isDebug }}

- script:  echo ${{ parameters.buildTests }}

pool.yaml

parameters:
  # Values marked as True and False instead of true and false
  # to facilitate passing forward as string values to another tool.
  - name: buildLibs
    type: string
    values:
    - True
    - False
    default: True

jobs:
  - job:
    strategy:
      matrix:
        x64_debug:
          isDebug: true
          buildTests: False
        x64_release:
          isDebug: false
          buildTests: True

    steps:
        - template: template.yml
          parameters:
            isDebug: $(isDebug)
            buildLibs: ${{ parameters.buildLibs }}
            buildTests: $(buildTests)

Result:

enter image description here

Upvotes: 1

Related Questions