node-jenkins icon indicating copy to clipboard operation
node-jenkins copied to clipboard

Support for Submitting a job and waiting until it completes?

Open rayterrill opened this issue 4 years ago • 8 comments

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.

rayterrill avatar Jan 16 '21 01:01 rayterrill

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.

silas avatar Jan 16 '21 01:01 silas

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 avatar Jan 30 '21 21:01 marisaroque

@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();

silas avatar Jan 31 '21 00:01 silas

Perfect, @silas! It works like a charm and really helped a lot. Thank you. 🙂

marisaroque avatar Feb 02 '21 10:02 marisaroque

@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 avatar Dec 17 '21 15:12 markcellus

@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.

silas avatar Dec 17 '21 15:12 silas

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?

markcellus avatar Dec 17 '21 15:12 markcellus

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.

silas avatar Dec 17 '21 15:12 silas