Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Submitting a job and waiting until it completes? #102

Open
rayterrill opened this issue Jan 16, 2021 · 8 comments
Open

Support for Submitting a job and waiting until it completes? #102

rayterrill opened this issue Jan 16, 2021 · 8 comments
Labels

Comments

@rayterrill
Copy link

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.

@silas
Copy link
Owner

silas commented Jan 16, 2021

There is no builtin function, but you can submit the job, get the build id (see #30 (comment)), and then query the job until it finishes.

@marisaroque
Copy link

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.

@silas
Copy link
Owner

silas commented Jan 31, 2021

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

@marisaroque
Copy link

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

@markcellus
Copy link

@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? 🤔

@silas
Copy link
Owner

silas commented Dec 17, 2021

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

@markcellus
Copy link

markcellus commented Dec 17, 2021

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?

@silas
Copy link
Owner

silas commented Dec 17, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants