Azure DevOps Azure PowerShell 任务输出变量 - 无法将值传递到第三阶段

标签 azure powershell yaml devops

我正在尝试在阶段之间传递变量。我成功地将值传递到下一阶段,但之后变量为空。我尝试了所有的方法来得到这个,但失败了

stages:
- stage: InitialStage
  jobs:
  - job: StandAlone
    displayName: Required Stand Alone Job
    steps:
    - bash: |
        echo "##vso[task.setvariable variable=doThing;isOutput=true]Yes"
      name: bash_test

- stage: SecondStage
  jobs:
  - job:
    displayName: SecondStage
    variables:
      variable1: $[ stageDependencies.InitialStage.StandAlone.outputs['bash_test.doThing'] ]
      
    steps:
    - task: CmdLine@2
      name: test
      inputs:
        script: |
          echo "$(variable1)"

- stage: ThirdStage
  jobs:
  - job:
    displayName: ThirdStage
    variables:
      variable1: $[ stageDependencies.InitialStage.StandAlone.outputs['bash_test.doThing'] ]
      
    steps:
    - task: CmdLine@2
      name: test
      inputs:
        script: |
          echo "$(variable1)"

SecondStage 阶段的回显将给出值,但 ThirdStage 是空白的,我怀疑我们只能在立即阶段访问变量,否则我做错了什么,对此的任何帮助将不胜感激。

最佳答案

选项 1:

为了将变量传递到另一个阶段,您需要添加 isOutput=true

参见如下所示的示例;

- task: Bash@3
  name:  setprojectName
  inputs:
    targetType: 'inline' 
    script: |
      echo "##vso[task.setvariable variable=projectNameGlobal;isOutput=true]$(echo $(Build.Repository.Name)| sed 's|.*/||')"
      echo "##vso[task.setvariable variable=projectName]$(echo $(Build.Repository.Name)| sed 's|.*/||')"

projectName 仅在变量填充的阶段可用,但 projectNameGlobal 在下一阶段也可用。

另请参阅documentation

选项 2:

## azure-pipelines.yml
stages:

- stage: one
  jobs:
  - job: A
    steps:
    - task: Bash@3
      inputs:
          filePath: 'script-a.sh'
      name: setvar
    - bash: |
       echo "##vso[task.setvariable variable=skipsubsequent;isOutput=true]true"
      name: skipstep

- stage: two
  jobs:
  - job: B
    variables:
      - name: StageSauce
        value: $[ stageDependencies.one.A.outputs['setvar.sauce'] ]
      - name: skipMe
        value: $[ stageDependencies.one.A.outputs['skipstep.skipsubsequent'] ]
    steps:
    - task: Bash@3
      inputs:
        filePath: 'script-b.sh'
      name: fileversion
      env:
        StageSauce: $(StageSauce) # predefined in variables section
        skipMe: $(skipMe) # predefined in variables section
    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: |
          echo 'Hello inline version'
          echo $(skipMe) 
          echo $(StageSauce)

在上面的选项中,您可以看到如何使用由任何先前阶段填充的一个变量

关于Azure DevOps Azure PowerShell 任务输出变量 - 无法将值传递到第三阶段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71807117/

相关文章:

azure - 使用 Azure 机器学习的时间/预测系列

来自 Eventhub 的 Azure 捕获事件未按窗口大小捕获

c# - Power BI Embedded - 嵌入图 block

Powershell DSC : Can someone please provide me an example on how to use a resource?

powershell - 比较两个CSV并仅导出两个都不存在的名称列表

json - Perl YAML 到 JSON

azure - 如何与 New-AzureRmResourceGroupDeployment cmdlet 一起传递额外参数

python - Ansible 将变量写入 YAML 文件

ruby-on-rails - rails : yml merging

arrays - powershell 中的字节类型转换