-1

Does anyone have an example of starting a jenkins job with auth and parameterized in Java Script?

Its like a curl post but I am not sure how to do that exactly and I couldn't find any example passing username and token as well as parameters.

Jason
  • 2,246
  • 6
  • 34
  • 53

1 Answers1

5

You can use the jenkins npm library to perform actions on npm using a specific user:

import Jenkins = require('jenkins');

const jenkins

Jenkins({ baseUrl: `https://${ username }:${ password }@${ urlToJenkinsInstance }`, crumbIssuer: true })
      .then((_jenkins) => {
        jenkins = _jenkins;

        return new Promise((resolve, reject) => {
          // start building a job
          jenkins.job.build({
            name: jobName,
            // put all the parameters here:
            parameters: {
              PARAM1: 'hello',
              PARAM2: 'world'
            }
          }, function (err, data) {
            if (err) { return reject(err); }

            resolve(data);
          });
        });
      })
      .then((queueId) => {
        console.log('job queueId: ', queueId);
      })
      .catch(printErrorAndExit);

If you need to continue and monitor the flow, after the job is added to the queue, you need to wait for the job to start, which will add a jobId to the queue item using jenkins.queue.item(queueId, callback).

Then, you can monitor the actual job and check when it finished.

In the following code, I defined two functions called convertQueueIdToBuildId and waitForJobToFinish.

convertQueueIdToBuildId

This will wait for the queue item to get a permanent ID to start and checking for the job status. I defined the interval to check this every 5 seconds.

waitForJobToFinish

This will get the permanent jobId and check every 5 seconds what's the job's status. if it's SUCCESS, we can resolve the promise, and if we get either ABORTED or FAILURE, we error the promise to indicate something didn't go well. We can play with what makes the promise fail or resolve based on the use.

// continuing the previous promise chain:
  .then((queueId) => convertQueueIdToBuildId(jenkins, jobName, queueId, 5000))
  .then((buildId) => waitForJobToFinish(jenkins, jobName, buildId, 5000))
  .catch(printErrorAndExit);

function convertQueueIdToBuildId(jenkins, jobName, queueId, interval, spinner) {
  return convertQueueIdToBuildIdInner(queueId)
    .then((data) => {
      if (isNumber(data)) {
        return data;
      }

      return waitFor(interval)
        .then(() => convertQueueIdToBuildId(jenkins, jobName, queueId, interval, spinner));
    });

  function convertQueueIdToBuildIdInner(queueId) {
    return new Promise((resolve, reject) => {
      jenkins.queue.item(queueId, (err, data) => {
        if (err) {
          return reject(err);
        }

        resolve(data && data.executable && data.executable.number);
      });
    });
  }
}

function waitForJobToFinish(jenkins, jobName, buildId, interval) {
  return waitForJobToFinishInner(jobName)
    .then((data) => {
      if ('SUCCESS' === data.result) {
        return data;
      }

      if (['ABORTED', 'FAILURE'].indexOf(data.result) > -1) {
        const errorMessage = `JENKINS[${ jobName }:${ buildId }] job ${ data.result }`;
        throw new Error(errorMessage);
      }

      return waitFor(interval)
        .then(() => waitForJobToFinish(jenkins, jobName, buildId, interval));
    });

  function waitForJobToFinishInner(jobName) {
    return new Promise((resolve, reject) => {
      jenkins.build.get(jobName, buildId, function (err, data) {
        if (err) {
          return reject(err);
        }

        resolve(data);
      });
    });
  }
}

function waitFor(interval) {
  return new Promise((resolve) => setTimeout(() => resolve(), interval));
}
Thatkookooguy
  • 6,669
  • 1
  • 29
  • 54
  • Can't this be done by a simple javascript curl method rather than using npm? – Jason May 19 '19 at 08:05
  • 1
    @Jason you can do this totally vanilla with no additional libraries if that's the way you want to go. Basically, [this](https://nodejs.org/api/http.html#http_http_request_options_callback) is how you do an HTTP request in node, and [here](https://wiki.jenkins.io/display/JENKINS/Remote+access+API) you can find what each Jenkins path is (you should use the equivalent to what I used in the answer). Sounds to me like a lot of extra work for not using some open source libraries that you can check their code for problems, no? – Thatkookooguy May 19 '19 at 08:17