I initially wanted to define pipeline variables in my azure-pipelines.yml that I can optionally set at queue time, but it seems that this is not supported at the moment: variables that can be set at queue time can only be defined in the Designer. This variable (comma-separated) is named nx_versions and will be used to build a matrix strategy. Here's a minimal example:
# azure-pipelines.yml
jobs:
- template: job-template.yml
  parameters:
    nx_versions: $(nx_versions)
and
# job-template.yml
parameters:
  nx_versions: 
    - 1
jobs:
  - job: build
    strategy:
      matrix:
        ${{ each nxver in parameters.nx_versions }}:
          NX_${{ nxver }}:
            NXVersion: ${{ nxver }}
    steps:
      - powershell: echo $(NXVersion)
Queuing the build with nx_versions = 2,3 (value doesn't actually matter) results in an error: 
/job-template.yml (Line: 9, Col: 9): Expected a sequence or mapping. Actual value '$(nx_versions)'
Is this even possible? I also tried using ${{ nx_versions }} and ${{ variables.nx_versions }} to no avail.
This is possible with a full Designer solution.
Passing variables between tasks in the same jobSet the value with the command echo "##vso[task. setvariable variable=FOO]some value" In subsequent tasks, you can use the $(FOO) syntax to have Azure Pipelines replace the variable with some value.
Create a variable group You can't create variable groups in YAML, but they can be used as described in Use a variable group. Sign in to your organization ( https://dev.azure.com/{yourorganization} ) and select your project. Select Pipelines > Library > + Variable group. Enter a name and description for the group.
Export your Classic pipelineOpen your Classic pipeline. Select the ellipses (...), and then select Export to YAML. Open the downloaded YAML file in your code editor. If your YAML pipeline includes variables defined in the Classic UI, define the variables again in your pipeline settings UI or in your YAML file.
The trivial pipeline (not referencing templates, but could be extended easily to do so)
parameters:
- name: nx_versions
  type: object
  default: 
  - 1
  - 4
jobs:
  - job: build
    strategy:
      matrix:
        ${{ each nxver in parameters.nx_versions }}:
          NX_${{ nxver }}:
            NXVersion: ${{ nxver }}
    steps:
      - powershell: echo $(NXVersion)
Expands to
parameters:
- name: nx_versions
  type: object
  default:
  - 1
  - 4
stages:
- stage: __default
  jobs:
  - job: build
    strategy:
      matrix:
        NX_1:
          NXVersion: 1
        NX_4:
          NXVersion: 4
    steps:
    - task: PowerShell@2
      inputs:
        targetType: inline
        script: echo $(NXVersion)
If you go to queue a build for that, you get a parameters page with the defaults:

that you can override:

which results in:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With