Azure管道变量的值在阶段之间的传播期间第二次访问时丢失

szqfcxe2  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(111)

我正在尝试实现一个管道,其中第一阶段设置变量的值,所有连续阶段都访问此值。
下面是我的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

字符串

moiiocjp

moiiocjp1#

检查文档中有以下信息:输出变量仅在下一个下游阶段可用。如果多个stage消耗相同的输出变量,则使用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: # 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"

字符串

相关问题