Azure Pipeline use template expression with queued variables

前端 未结 2 1311
日久生厌
日久生厌 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:06

    I expect it to echo abc but instead abc is replaced with nothing - variables.Environment seems to be undefined.

    According to this document:

    Runtime happens after template expansion. Template variables are processed at compile time, and are replaced before runtime starts. Template variables silently coalesce to empty strings when a replacement value isn't found.

    So in your case echo $(test) print out nothing but empty string. Cause the queue variables are used for runtime. For this, you can consider using macro or runtime expression which is for runtime. Both test: $(Environment) and test: $[variables.Environment] work well on my side.

    Later on I want to load a different Variable Group depending on the Environment variable, which is why I do not echo $(Environment) directly in the script. It's just a simplified example.

    As I know, linking different variable groups depending on the dynamic Environment variable is not supported yet, here's one discussion about that topic. And this is one good workaround in that scenario.

    Nowadays Azure Devops Service is rolling out the new feature runtime parameters, I think it can meet most of your requirements. It could be a better choice for you, use runtime parameters instead of not supported dynamic Environment variable.

    My simple test about this option:

    1.Content in yaml:

    parameters:
    - name: group
      displayName: Group Name
      type: string
      default: TestGroup
      values:
      - TestGroup
      - Group2
      - Group3
      - Group4
    
    variables:
    - group: ${{ parameters.group }}
    
    steps:
    - script: |
        echo $(Name)
      displayName: 'Show group name'
    

    2.My variable group TestGroup:

    3.Click run pipeline:

    4.The pipeline runs well and the displays the variable defined in variable group:

    Hope it helps :)

提交回复
热议问题