Azure Pipeline中的变量在传递到不同阶段时,在第二次访问时会丢失其值。

huangapple go评论96阅读模式
英文:

Azure Pipeline Value of variable gets lost when accessing second time during the propagation between the stages

问题

我正在尝试实现一个管道,其中第一阶段设置了一个变量的值,所有后续阶段都访问这个值。

以下是我的YAML配置。在这里,阶段ST1设置了变量VAR1的值。我能够在ST2中访问VAR1的值。然而,在ST2之后,当ST3运行时,这个值会丢失,在ST3中我得到空值。

是否有任何方法可以在各个阶段中保留这个值?

谢谢,

stages:
  - stage: ST1
    jobs:
      - job: 
        steps:
        - task: 
          name: TASK1
          inputs:
            script: |
                            echo "##vso[task.setvariable variable=VAR1;isoutput=true]Hello"
  - stage: ST2
    variables:
    - name: VAR1
      value: $[stageDependencies.Build.Build_Artifact_Job.outputs['TASK1.VAR1']]   
    jobs:
      - job: 
        steps:
        - task: 
          inputs:
            script: |
                            echo "$(VAR1)" # Prints "Hello"
  - stage: ST3
    variables:
    - name: VAR1
      value: $[stageDependencies.Build.Build_Artifact_Job.outputs['TASK1.VAR1']]   
    jobs:
      - job: 
        steps:
        - task: 
          inputs:
            script: |
                            echo "$(VAR1)" # Prints Empty String

【注意】:这是您提供的YAML配置的翻译部分。

英文:

I am trying to implement a pipeline where the first stage sets the value of a variable and all the consecutive stages access this value.

Following is my YAML. Here Stage ST1 is setting value of Variable VAR1. I am able to access the value of the VAR1 in ST2. However, after ST2, when ST3 runs, this value gets lost and in ST3 I get the empty value.

Is there any way to preserve this value throughout the stages?

Thank you,

stages:
  - stage: ST1
    jobs:
      - job: 
        steps:
        - task: 
          name: TASK1
          inputs:
            script: |
              echo "##vso[task.setvariable variable=VAR1;isoutput=true]Hello"

  - stage: ST2
    variables:
    - name: VAR1
      value: $[stageDependencies.Build.Build_Artifact_Job.outputs['TASK1.VAR1']]   
    jobs:
      - job: 
        steps:
        - task: 
          inputs:
            script: |
              echo "$(VAR1)" # Prints "Hello"

  - stage: ST3
    variables:
    - name: VAR1
      value: $[stageDependencies.Build.Build_Artifact_Job.outputs['TASK1.VAR1']]   
    jobs:
      - job: 
        steps:
        - task: 
          inputs:
            script: |
              echo "$(VAR1)" # Prints Empty String

答案1

得分: 3

请查看文档,其中有以下信息:输出变量仅在下游阶段中才可用。如果多个阶段使用相同的输出变量,请使用dependsOn条件

以下是一个可工作的示例。

trigger: none

pool:
  vmImage: ubuntu-latest

stages:
  - stage: ST1
    jobs:
      - job: Build_Artifact_Job
        steps:
        - task: CmdLine@2
          name: TASK1
          inputs:
            script: ''echo "##vso[task.setvariable variable=VAR1;isoutput=true]Hello"''

  - stage: ST2
    dependsOn: # 使用dependsOn来访问来自阶段的输出变量
    - ST1
    variables:
    - name: VAR1
      value: $[stageDependencies.ST1.Build_Artifact_Job.outputs['TASK1.VAR1']]
    jobs:
      - job:
        steps:
        - script: echo '"$(VAR1)"' # 打印 "Hello"

  - stage: ST3
    dependsOn: # 使用dependsOn来访问来自阶段的输出变量
    - ST1
    - ST2
    variables:
    - name: VAR1
      value: $[stageDependencies.ST1.Build_Artifact_Job.outputs['TASK1.VAR1']]
    jobs:
      - job:
        steps:
          - script: echo '"$(VAR1)"' # 打印 "Hello"
英文:

Check the documentation there is information that: Output variables are only available in the next downstream stage. If multiple stages consume the same output variable, use the dependsOn condition.

Below is a working example.

trigger: none

pool:
  vmImage: ubuntu-latest

stages:
  - stage: ST1
    jobs:
      - job: Build_Artifact_Job
        steps:
        - task: CmdLine@2
          name: TASK1
          inputs:
            script: 'echo "##vso[task.setvariable variable=VAR1;isoutput=true]Hello"'
         
        
  - stage: ST2
    dependsOn: # Use dependsOn to get access to output variable from the stage
    - ST1
    variables:
    - name: VAR1
      value: $[stageDependencies.ST1.Build_Artifact_Job.outputs['TASK1.VAR1']]   
    jobs:
      - job: 
        steps:
        - script: echo "$(VAR1)" # Prints "Hello"

  - stage: ST3
    dependsOn: # Use dependsOn to get access to output variable from the stage
    - ST1
    - ST2
    variables:
    - name: VAR1
      value: $[stageDependencies.ST1.Build_Artifact_Job.outputs['TASK1.VAR1']]   
    jobs:
      - job: 
        steps:
          - script: echo "$(VAR1)" # Prints "Hello"

huangapple
  • 本文由 发表于 2023年8月4日 23:12:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定