Eddie
Eddie

Reputation: 621

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

I have 3 Jobs in Azure Devops Release Pipeline :

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

enter image description here

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

Upvotes: 1

Views: 1286

Answers (1)

Leo Liu
Leo Liu

Reputation: 76770

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:

enter image description here

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.

Upvotes: 1

Related Questions