silas / node-jenkins

Jenkins client
https://www.npmjs.com/package/jenkins
MIT License
356 stars 86 forks source link

How to reliably check whether a build has failed? #106

Open markcellus opened 2 years ago

markcellus commented 2 years ago

This is more of a question rather than an "issue". But let's say I'm using the following to retrieve a job object from a build that has failed:

const job = await jenkins.build.get('example', 123);

I tried using the following....

if (job.lastFailedBuild && job.lastFailedBuild.number === 123) {
  throw new Error('Build failed.');
}

But that doesn't work if some other build (567) has failed before the code above :point_up: runs.

Given this scenario, what is the recommended way to detect the build failure and throw an error?

markcellus commented 2 years ago

Think I've found a way. Looks like I was incorrectly assuming that what was returned from build.get() was the job (per code snippet in https://github.com/silas/node-jenkins/issues/102#issuecomment-770305686, which has it assigned to a job const. I think assigning to a build const is more accurate. Then we can read from the result of the build.

const build = await jenkins.build.get('example', 123);
// ... build finishes
if (build.result === 'FAILURE') {
  throw new Error('Build failed.');
}

@silas do you mind confirming that my assumptions here are correct? If so, we can close ticket. Thanks!