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.
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));
}