Azure DevOps Pipeline在作业之间传递变量。

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

Azure DevOps Pipeline passing variables between jobs

问题

我一直不知疲倦地尝试使我的流水线按照以下链接的说明工作:

他们声称,如果我按照以下步骤操作,我应该能够在多个作业中访问Name

pool:
  name: ubuntu

parameters:
- name: sourceArtifactPath
  type: string

jobs:
- job: Setup
  steps:
  - script: echo "##vso[task.setvariable variable=Name;isOutput=true]$(basename ${{ parameters.sourceArtifactPath }})"
    name: SetVariables
    displayName: "Set Pipeline Variables"
  - script: echo $(SetVariables.Name)

- job: DoStuff
  dependsOn: Setup
  steps:
  - bash: echo "Important work has beed done"

- job: Upload
  dependsOn: DoStuff
  variables:
    artifactName: $[ dependencies.Setup.outputs['SetVariables.Name'] ]
  steps:
  - script: echo $(artifactName)
    displayName: "Print Filename"

在相同作业内部引用时它可以工作,但是当我尝试从不同的作业中访问Setup.SetVariables.Name时,我得到的只是:

==============================================================================
Generating script.
Script contents:
echo 
========================== Starting Command Output ===========================

请帮忙。

英文:

I've been tirelessly trying to make my pipeline work according to the

They claim that if I do the following step I should be able to access Name across multiple jobs:

pool:
  name: ubuntu

parameters:
- name: sourceArtifactPath
  type: string

jobs:
- job: Setup
  steps:
  - script: echo "##vso[task.setvariable variable=Name;isOutput=true]$(basename ${{ parameters.sourceArtifactPath }})"
    name: SetVariables
    displayName: "Set Pipeline Variables"
  - script: echo $(SetVariables.Name)

- job: DoStuff
  dependsOn: Setup
  steps:
  - bash: echo "Important work has beed done"

- job: Upload
  dependsOn: DoStuff
  variables:
    artifactName:  $[ dependencies.Setup.outputs['SetVariables.Name'] ]
  steps:
  - script: echo $(artifactName)
    displayName: "Print Filename"

and it works for the reference inside same job, however when I try to access Setup.SetVariables.Name from different job all im getting is:

==============================================================================
Generating script.
Script contents:
echo 
========================== Starting Command Output ===========================

please help.

some questions look similar but are not exactly the same like:
1
2

答案1

得分: 1

这是对我有效的方法。重要的部分是 isOutput=true。在定义变量时添加此部分,否则您将无法查看/打印/输出该变量。

触发器:
  分支:
    包括:
      - '主分支'
资源池:
  mypool

作业:
  # 从作业A设置一个输出变量
  - 作业: A
    步骤:
    - 脚本: |
        #!/bin/bash        
        echo "##vso[task.setvariable variable=myvar1;isOutput=true]True"
        echo "##vso[task.setvariable variable=myvar2;isOutput=true]hello"        
      名称: 设置变量步骤
  # 将变量映射到作业B
  - 作业: B
    依赖于: A
    变量:
      myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myvar1'] ]  
      myVarFromJobA1: $[ dependencies.A.outputs['setvarStep.myvar2'] ]  
    步骤:
    - 脚本: echo $(myVarFromJobA)
      名称: 输出变量
    - 脚本: echo $(myVarFromJobA1)
      名称: 输出变量1
英文:

Here is what worked for me. Important part is isOutput=true. Add this while defining variable otherwise you wont be able to view/print/echo the variable

trigger:
  branches:
    include:
      - 'master'
pool:
  mypool

jobs:
  # Set an output variable from job A
  - job: A
    steps:
    - script: |
        #!/bin/bash        
        echo "##vso[task.setvariable variable=myvar1;isOutput=true]True"
        echo "##vso[task.setvariable variable=myvar2;isOutput=true]hello"
      name: setvarStep
  # Map the variable into job B
  - job: B
    dependsOn: A
    variables:
      myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myvar1'] ]  
      myVarFromJobA1: $[ dependencies.A.outputs['setvarStep.myvar2'] ]  
    steps:
    - script: echo $(myVarFromJobA)
      name: echovar
    - script: echo $(myVarFromJobA1)
      name: echovar1

答案2

得分: 0

如果您想引用来自“Setup”作业的变量,您需要声明您依赖于该作业。

- job: Upload
  dependsOn: 
  - Setup
  - DoStuff

仅声明作业依赖于“DoStuff”是不够的,因为“DoStuff”已声明其依赖于“Setup”。

英文:

If you want to reference a variable from the Setup job, you need to declare you're dependent on that job.

- job: Upload
  dependsOn: 
  - Setup
  - DoStuff

It's not enough to declare your job depends on DoStuff which declares its dependency on Setup.

huangapple
  • 本文由 发表于 2023年2月23日 21:01:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545185.html
匿名

发表评论

匿名网友

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

确定