node-jenkins
node-jenkins copied to clipboard
Support for Submitting a job and waiting until it completes?
Is there a mechanism to support submitting a job and waiting for it to complete or chaining the calls together to allow for this functionality? Cheers for this module @silas this is super helpful.
There is no builtin function, but you can submit the job, get the build id (see https://github.com/silas/node-jenkins/issues/30#issuecomment-238436339), and then query the job until it finishes.
Hey, @rayterrill.
Did you implement the solution that @silas suggested? Could you please share it here? I'm currently trying to do the same thing. Actually, I'm struggling with it so far.
Many thanks.
@marisaroque Something like this should work:
const jenkins = require('jenkins')({
baseUrl: 'http://admin:admin@localhost:8080',
crumbIssuer: true,
promisify: true,
});
async function main() {
const jobName = 'test';
const queueId = await jenkins.job.build(jobName);
let queueItem;
while (true) {
queueItem = await jenkins.queue.item(queueId);
if (queueItem.executable) {
break;
}
if (queueItem.cancelled) {
console.log('queue cancelled');
return;
}
console.log('waiting on queue...');
await new Promise(r => setTimeout(r, 1000));
}
let job;
while (true) {
job = await jenkins.build.get(jobName, queueItem.executable.number);
if (!job.building) {
break;
}
console.log('waiting on job...');
await new Promise(r => setTimeout(r, 1000));
}
console.log(job.fullDisplayName, job.result);
}
main();
Perfect, @silas! It works like a charm and really helped a lot. Thank you. 🙂
@silas Just stumbling upon this issue. Thanks for posting that code snippet above--it really helps. However, am I correct that it's missing code that would handle a possible build failure? :thinking:
@markcellus I guess it depends on what you definition of complete it. it's suppose to poll until it's not building anymore. You can look at the state of job
at that point to decide what to do.
Oh ok that makes sense. So I'm guessing I can't use the err
in the callback for jenkins.job.build()
? since that doesn't seem to represent the build failing. If that's true, what use-cases does that err
object actually represent then?
This is a pretty light wrapper around the jenkins api, err is basically just an unexpected status code from the api build endpoint, a network error, or invalid input.
An error from jenkins.job.build()
just means it couldn't add the build request to the build queue for some reason.