Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass pipeline variable from Designer to YAML job template

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.

like image 548
selimb Avatar asked Mar 18 '19 19:03

selimb


People also ask

How do you pass variables in Azure pipelines Yml tasks?

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.

How do I add a variable group to YAML pipeline?

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.

How do you convert classic pipeline to YAML?

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.


Video Answer


1 Answers

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:

Defaults

that you can override:

Override parameters

which results in:

Overridden job result

like image 186
WaitingForGuacamole Avatar answered Oct 01 '22 06:10

WaitingForGuacamole