ADS 2019-如何在构建作业之间传递变量 [英] ADS 2019 - How to pass variables between build jobs

查看:143
本文介绍了ADS 2019-如何在构建作业之间传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Azure DevOps Server 2019.1,我开始使用多作业,以允许我将工作分为多个代理.

Using Azure DevOps Server 2019.1 i am starting to work with Multi jobs, to allow me to split up work onto multiple agents.

流程本身运行良好.我有这样的设置

The flow itself works fine. I have it setup like this

开始工作-这基本上测试了一些变量并更新了内部版本号

Begin Job - this basically tests a few variables and Updated the buildnumber

(取决于Begin作业)RunTest作业-运行多配置"的作业,该作业将分割逗号分隔的任务类别列表

(Depends on Begin Job) RunTest Job - A job to run "multi-configuration", which splits a comma seporated list of task categories

(取决于RunTest作业)结束作业-链中新构建的触发器构建任务

(Depends on RunTest Job) End Job - A trigger build task for a new build in the chain

虽然这些作业依赖于另一个作业,但这似乎仅影响它们的开始时间,而他们将无法访问之前运行的作业所提供的信息.

While the jobs depend on another job, this only seems to affect the time they will start, they will not get access to the information provided by the job that ran before.

基本上,我需要的是已在Begin Job中设置的变量的值(buildNumber). 我需要在RunTest和End Job中使用此版本号. 我如何获得此信息?我阅读了无法做到这一点的文章,但尚未看到有效的解决方法.有人有不错的解决方法吗?

Basically what i need is the value of a variable that has been set (buildNumber) in the Begin Job. I need this version number in the RunTest and End Job. How can i get this information ? I read articles that this is not possible, but have not seen a valid workaround yet. Does anyone have a decent workaround ?

推荐答案

Update2: 使用YAML应该是最简单的解决方案.如果您坚持经典构建视图.您可以尝试通过将值存储在文件(json,xml,yaml等)中来完成此操作,可以直接在Job中读取文件,也可以重新设置变量.

Update2: Using YAML should be the simplest solution. If you insist on Classic build view. You could try to accomplish this by storing the values in a file (json, xml, yaml, what have you), you can read the file in the Job either direct use or re-set the variable again.

当您排队下一个版本时,它不会影响源代码管理中的文件,并且默认值也不会更改.

When you queue next build, it will not effect the file in source control and the default value will also not change.

在同一阶段的作业之间传递变量,这需要使用输出变量.

Passing variables between jobs in the same stage, it requires working with output variables.

但是,根据

However, according to this, using outputs in a different job is not supported in Classic UI Format.

作为此方案中的解决方法,您可以通过

As workarounds in this scenario, you can share variables via Pipeline Variables(share variables across jobs in same pipeline).

1.您可以在管道变量中设置key变量:

1.You can set a key variable in pipeline variables:

2.在您的第一份工作中添加一个带有以下内容的Powershell 内嵌任务:

2.Add one Powershell Inline task with content below in your first job:

$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/definitions/$($env:SYSTEM_DEFINITIONID)?api-version=5.0"

Write-Host "URL: $url"

$pipeline = Invoke-RestMethod -Uri $url -Headers @{

    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"

}

Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"



# Update an existing variable to its new value

$pipeline.variables.key.value = "value"



####****************** update the modified object **************************

$json = @($pipeline) | ConvertTo-Json -Depth 99



$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}



write-host "==========================================================" 

Write-host "The value of Varialbe key is updated to" $updatedef.variables.key.value

write-host "=========================================================="

3.运行管道,我们可以发现key变量的值已成功更新:

3.Run the pipeline we can find the value of key variable is successfully updated:

因此,您可以在第一个作业中运行ps脚本来更新key变量的值,然后所有下一个作业都可以轻松访问更新后的变量.

So you can run the ps script in first job to update the value of key variable, then all next jobs can access the updated variable easily.

注意:

  1. 对于脚本本身,只需要更改行$pipeline.variables.key.value = "value"(必要)和Write-host "The value of Varialbe key is updated to" $updatedef.variables.key.value(可选).
  1. For the script itself, you only need to change lines $pipeline.variables.key.value = "value"(necessary) and Write-host "The value of Varialbe key is updated to" $updatedef.variables.key.value(optional).

如果我想将名为MyTest的变量设置为值MyValue,则这些行应为$pipeline.variables.MyTest.value = "MyValue"Write-host "The value of Varialbe MyTest is updated to" $updatedef.variables.MyTest.value.

If I want to set the variable named MyTest to value MyValue, the lines should be $pipeline.variables.MyTest.value = "MyValue" and Write-host "The value of Varialbe MyTest is updated to" $updatedef.variables.MyTest.value.

  1. 为确保一项任务中的ps任务可以访问OAuth Token,我们应Allow Scripts to Access OAuth Token.点击代理人的工作名称,然后选中相应的框:
  1. To make sure the ps task in one job can access OAuth Token, we should Allow Scripts to Access OAuth Token. Click the agent job name and check the box:

  1. 要使管道具有更新管道变量(编辑构建管道)的权限,请通过管道安全性为用户xxx(ProjectName) build service设置Edit build pipeline 允许.
  1. To enable the pipeline has the permission to update pipeline variable (edit build pipeline), go pipeline security to set the Edit build pipeline allow for user xxx(ProjectName) build service.

这篇关于ADS 2019-如何在构建作业之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆