【问题标题】:How Restrict the Agent Job to Run only if Specific Job succeeded仅在特定作业成功时如何限制代理作业运行
【发布时间】:2019-12-17 08:45:59
【问题描述】:

我在 Azure Devops Release Pipeline 中有 3 个工作:

  • 代理作业1
  • 代理作业2
  • 代理作业3

我想配置 Agent job2,使其即使 Agent job1 失败也能运行。

为此,我已将 Run this jobAgent job2 属性设置为 即使之前的作业失败

现在,我想配置 Agent job3,使其仅在 Agent job2 成功时才运行

我需要在 Agent Job3 中进行什么配置以使其依赖于 Agent Job2

【问题讨论】:

  • 您可以在 Agent Job 2 的末尾调用 Write-Host "##vso[task.setvariable variable=Job.Succed]$true" 并在 Agent Job 3 中读取 $env:Job.Succed(我认为是这样)。
  • @MarTin : 你能提供一些参考例子吗?另外我需要在哪里写 Write-Host "##vso[task.setvariable variable=Job.Succed]$true" 在 Powershell 中?
  • 你的意思是这样的? logging commands
  • @MarTin : Agent Job1 ,Agent Job2,Agent Job3 彼此不同,因此我们无法跨代理发送变量
  • 为什么你只在一个代理任务中不这样做?

标签: azure-devops azure-pipelines azure-pipelines-release-pipeline azure-devops-self-hosted-agent


【解决方案1】:

如何限制代理作业仅在特定作业成功时才运行

恐怕没有这种开箱即用的自定义条件来限制代理作业只有在特定作业成功时才能运行

作为解决方法,我们可以在变量中将RunAgentJob3 之类的变量设置为False

然后,在您的第二个代理作业结束时添加一个内联 powershell 任务,条件为仅when all previous tasks have succeeded,在复制任务之后调用 REST API 以将变量(如RunAgentJob3)更新为true

$url = "https://dev.azure.com/<OrganizationName>/<ProjectName>/_apis/build/definitions/55?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 named RunAgentJob3to its new value true
$pipeline.variables.RunAgentJob3.value = "true"

####****************** 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"}

参考:Definitions - Update

在 Agent Job3 中,将自定义条件设置为:

eq(variables['RunAgentJob3'],'true')

现在,只有当代理 Job2 成功时,代理 Job3 才会运行。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    相关资源
    最近更新 更多