0

In my Azure DevOps release pipeline I have a powershell script that sends a REST request to another application with the status 'failed' or 'successful'.

I want the status to send 'failed' if any of the previous jobs failed. So basically something like this:

if (($Agent  -eq "Succeeded") -and ($LastJobsFailed -ne "true")) {
   $change_status="successful"
}
else {
   $change_status="failed"
}

Now I know that Azure Devops uses this status somewhere, since you can specify whether a job starts or not based on the results of the last jobs.

As a workaround I copied my script twice one time with status "successful" and this only runs when all jobs succeed and vice versa. But i'd like to do everything in one script :)

so I would expect it would be possible to find a list with all previous job statuses or something. Anyone any ideas?

thanks!

Jay05
  • 173
  • 2
  • 9
  • How many stages (environments) do you have? – Shayki Abramczyk Jul 18 '19 at 13:23
  • I'm not sure if this applies. Any exceptions from powershell commands are kept in a $error variable. It's actually an object array, with the latest exception in $error[0]. You have to pipe it to format-list -force to see all the properties. – js2010 Jul 19 '19 at 02:58
  • @ShaykiAbramczyk the use case here is that the logic runs in one staging environment which consists of several jobs. Kinda like below answer of Lu Mike. – Jay05 Jul 19 '19 at 07:18
  • @Jay05 If you have only 1 stage, you can add another stage with one task to check the first stage error/success. – Shayki Abramczyk Jul 19 '19 at 07:25

1 Answers1

1

I think you don't need to to check the status of previous jobs in the powershell script. A workaround for that is, you can create one job(named jobSendOK) which sepecify the run condition as "Only when all previous jobs have succeed", and create another one job (named jobSendNG)which sepecify the run condition as "Only when a previous job has failed". In jobSendOK, add a powershell task for sending 'successful', while jobSendNG has a powershell task for sending 'failed'.

enter image description here

Lu Mike
  • 677
  • 4
  • 4
  • 1
    Thanks for the answer Lu Mike. This is actually the exact workaround that I made and it does work perfectly. thing is you're still maintaining 2 scripts, that's why I posted the question to see if there was a better way. If not then I'll mark this as the accepted answer :) – Jay05 Jul 19 '19 at 07:13
  • Another way is to use REST API to get the jobs info by using powershell script, and then analysis the response data(json) to get the job status. In my opnion, that is more complex then this way. – Lu Mike Jul 19 '19 at 07:32
  • Actually I tried accessing the REST API to get the job info, couldn't really find a way how to do it. If I ever figure it out i'll post a link to it. You have any ideas Lu Mike? – Jay05 Jul 25 '19 at 07:21
  • @Jay05, you may take a look at api [Get Release](https://learn.microsoft.com/en-us/rest/api/azure/devops/release/releases/get%20release?view=azure-devops-server-rest-5.0).It will get all the status info about the task. – Lu Mike Jul 26 '19 at 05:40