Gaurav Joshi
Gaurav Joshi

Reputation: 1001

Is there any way we can get task name in the release pipeline to execute specific task based on an condition

I'm working on a release pipeline there are around 3 task in the 1 Agent. taskA,taskB,taskC I want to run specific task based on specific task failed. I tried custom condition but it didn't satisfied my case. I want task C to be execute when only task B is failed for that I'm using output variable as well. In this case its working only for taskB. When TaskA failed Task B will skip and output variable become null in that case my task C is execute which is not correct.

I'm trying to make a condition which fulfill both TaskA and TaskB condition.

  1. if TaskA failed --> TaskC should not run
  2. if TaskB failed --> TaskC should run.

Here is my condition:-> and(eq(Agent.JobStatus, 'failed'), in(variables['oneboxout.oneboxvar'],'False')) Is there any way we can get task name only so my work would be easier.

Below are my task screenshot for your reference.

enter image description here

Upvotes: 0

Views: 1122

Answers (1)

Leo Liu
Leo Liu

Reputation: 76770

Is there any way we can get task name only so my work would be easier.

The answer is yes.

We could use the REST API Releases - Get Release to get the task name or status;

https://learn.microsoft.com/en-us/rest/api/azure/devops/release/releases/get%20release?view=azure-devops-rest-6.0

As the above REST API, we need provide the current release Id to that REST API. To resolve this, we could use the REST API Releases - List with Parameter definitionId={definitionId} and powershell parameter Select-Object -first 1 to get the current release Id.

To resolve this request, I would like use following method:

Summary:

  1. Add powershell a task (Let's call it Get JobB task result)between JobB and JobC to invoke REST API to get the result of the JobB with condition Even if a previous task has failed, unless the build was canceled.
  2. Set a variable RunJobC with different value based on the result of the task JobB in above powershell task.
  3. Set condition and(always(), eq(variables['RunJobC'], 'True')) for the JobC.

My test scripts (Check Allow scripts to access the OAuth token option in the Phase):

$url = "https://vsrm.dev.azure.com/M<YourOrganization>/<YourProject>/_apis/release/releases?definitionId=24&api-version=6.0"
$RealeasePipeline= Invoke-RestMethod -Uri $url -Headers @{   
 Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
} -Method Get

$ReleaseId= $RealeasePipeline.value.id | Select-Object -first 1

Write-Host The current Release Id: $ReleaseId

$url2 = "https://vsrm.dev.azure.com/<YourOrganization>/<YourProject>/_apis/release/releases/$($ReleaseId)?api-version=6.0"
$ReleaseInfo= Invoke-RestMethod -Uri $url2 -Headers @{   
 Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
} -Method Get

$TargetTask=$ReleaseInfo.environments.deploySteps.releaseDeployPhases.deploymentJobs.tasks| where { $_.Name -eq "JobB"}

Write-Host JobB task Result is: $TargetTask.status

if ($TargetTask.status -eq "succeeded"){
  Write-Host ("##vso[task.setvariable variable=RunJobC]False")
}elseif($TargetTask.status -eq "Failed"){
  Write-Host ("##vso[task.setvariable variable=RunJobC]True")
}

My test result:

enter image description here

Upvotes: 1

Related Questions