Azure Pipeline use template expression with queued variables

前端 未结 2 1313
日久生厌
日久生厌 2021-01-26 04:01

I defined a YAML build-pipeline in azure:

variables:
  test: \'${{ variables.Environment }}\'

pool:
  vmImage: \'ubuntu-latest\'

steps:
- script: |
    echo $(         


        
2条回答
  •  孤城傲影
    2021-01-26 04:05

    A variation of this problem:

    TL;DR:

    Group variables do not seem to be available at template compile time, e.g. when conditionally setting a template to run, based on a variable from that group - only variables explicitly set in the pipeline seem to be available.

    Long version:

    I have a set of variable groups; 'Group-NonProd', 'Group-Prod', etc. Each contains variable 'identifier', with differing values ("dev", "prod", for the below example).

    I have a master pipeline, 'main-pipeline.yml', and in that are several stages, each corresponding to the value of 'identifier', e.g.:

    - stage: NonProd
      variables:
      - group: 'Group-NonProd' ## includes the variable 'identifier' with value "nonprod"
      jobs:
      - template: nonprod.yml
        parameters:
          identifier: $(identifier)
      - script: echo "environment is $(identifier)"
    
    - stage: Prod
      variables:
      - group: 'Group-Prod' ## includes the variable 'identifier' with value "prod"
      jobs:
      - template: prod.yml
        parameters:
          identifier: $(identifier)
      - script: echo "environment is $(identifier)"
    

    However, when I run the pipeline, the parameters 'identifier' do not expand to the value in the group variable 'identifier' - it is blank - I see that when I try using the variable in the below conditional logic (the aim being to use this logic to determine which template to call - see commented out lines - and pass that parameter down to them):

    steps:
      - ${{ if eq(parameters.identifier, 'nonprod') }}:
        # - template: nonprod.yml
        - script: echo "Using nonprod template, environment is ${{ parameters.identifier }}"
      - ${{ if not(eq(parameters.identifier, 'prod')) }}:
        # - template: prod.yml
        - script: echo "Using prod template, environment is ${{ parameters.identifier }}"
    

    The above script always resorts to the 2nd condition, because the result is always "Using prod template, environment is " (blank).

    Here's the odd thing though - if I explicitly set the variable 'identifier' at each stage, it does work!

    e.g. this works:

    - stage: NonProd
      variables:
      - group: 'Group-NonProd'
      - name: identifier
        value: nonprod
      jobs:
      - template: nonprod.yml
        parameters:
          identifier: $(identifier)
      - script: echo "environment is ${{ parameters.identifier }}" 
    

提交回复
热议问题