如何在 Azure 发布管道中跨代理作业使用输出变量

How to use output variables across agent jobs in azure release pipeline

在我的 Azure 发布管道中,我有 2 个代理作业,一个用于 sql 使用 power-shell 的部署,另一个用于使用 power-shell 的 kubernetes。 如何在第一个代理作业中设置输出变量并在第二个代理作业中使用 power-shell.

使用

Write-Output "##vso[task.setvariable variable=testvar;isOutput=true;]testvalue"

然后引用输出变量,就好像它存在于未来的任务中一样。

$(taskreference.testvariable)

可以在 powershell 脚本任务的输出部分设置任务引用名称:

但是当我阅读文档时,cross-job 参考文献似乎还不可用:

TODO

I am not sure how are we going to generate Job ref name, since we don’t have job chaining at this point.

It should be something like:

{DefinitionName}_{JobName}

See: Azure-Pipelines-Agent/docs/Outputvariable.md

所以现在变量只能在同一个作业中使用。

It does look like YAML build do already support cross-phase output variable references.

职位:

# Set an output variable from job A
- job: A
  pool:
    vmImage: 'vs2017-win2016'
  steps:
  - powershell: echo "##vso[task.setvariable variable=myOutputVar;isOutput=true]this is the value"
    name: setvarStep
  - script: echo $(setvarStep.myOutputVar)
    name: echovar

# Map the variable into job B
- job: B
  dependsOn: A
  pool:
    vmImage: 'ubuntu-16.04'
  variables:
    myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutputVar'] ]  # map in the variable
                                                                          # remember, expressions require single quotes
  steps:
  - script: echo $(myVarFromJobA)
    name: echovar

How to use output variables across agent jobs in azure release pipeline

恐怕目前无法直接跨代理作业使用输出变量。

有相关问题Variables set via logging commands are not persistent between agents,可以跟进

要解决此问题,您可以尝试以下 解决方法

  • 在发布定义Variable中定义一个变量。
  • 使用RESTAPI(Definitions - Update)更新版本值 代理作业 1 中的定义变量。
  • 在下一个代理作业中使用发布定义变量的更新值。

关于使用RESTAPI更新发布定义变量值的详细信息,您可以关注下面的工单:

希望这对您有所帮助。