How Restrict the Agent Job to Run only if Specific Job succeeded

夙愿已清 提交于 2020-01-05 07:10:14

问题


I have 3 Jobs in Azure Devops Release Pipeline :

  • Agent job1
  • Agent job2
  • Agent job3

I want to configure Agent job2 In such a way that it should run even if Agent job1 Fails.

for that, I have set Agent job2 property of Run this job to even if the previous job fails.

Now, I want to configure Agent job3 in such a way that it should run only if Agent job2 succeded

What configuration I need to make in Agent Job3 to make it Dependant on Agent Job2


回答1:


How Restrict the Agent Job to Run only if Specific Job succeeded

I am afraid there is no such out of box custom conditions to restrict the Agent Job to Run only if Specific Job succeeded.

As workaround, we could set a variable like RunAgentJob3 to False in the Variables:

Then, add a inline powershell task at the end of your second agent job with condition Only when all previous tasks have succeeded, just after the copy task to invoke REST API to update the variable like RunAgentJob3 to 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"}

Reference: Definitions - Update

In the Agent Job3, set the custom conditions to:

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

Now, the Agent Job3 will run only if the agent Job2 succeeded.

Hope this helps.



来源:https://stackoverflow.com/questions/59370481/how-restrict-the-agent-job-to-run-only-if-specific-job-succeeded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!