Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AaronMcCaughan/c8560786d5688ab41d8c730d67f430d3 to your computer and use it in GitHub Desktop.
Save AaronMcCaughan/c8560786d5688ab41d8c730d67f430d3 to your computer and use it in GitHub Desktop.
Connect to Jenkins and trigger a build job
/*
This script will connect to Jenkins using Basic Authentication and trigger the execution of a Job called 'Demo' using API' requests.
It will then copy the log file from Jenkins to the execution log in Plutora as well as send build progress updates back to the Plutora UI
To execute this script on your own instance of Plutora you will need to replace all credentials and API end points. These are highlighted as comments like this: <<INSERT Text >>
*/
/********************************************************
*************STEP 1: Import npm's************************
********************************************************/
const https = require("https");
const http = require("http");
/********************************************************
*******STEP 2: Define you API and SSH parameters*********
********************************************************/
let options = {
host: '<<INSERT YOUR HOST URL>>',
port: 80,
path: '/job/Demo/lastBuild/api/json',
method: 'GET',
headers: {
accept: 'application/json',
authorization: '<<INSERT YOUR AUTHENTICATION DETAILS FOR JENKINS>>'
}
};
/********************************************************
****STEP 3: Create an API Request function********
********************************************************/
let req = function (options) {
return new Promise((resolve, reject) => {
http.get(options, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
try {
resolve(JSON.parse(data));
}
catch (error) {
resolve(data);
}
});
}).on("error", (err) => {
console.error("Error: " + err.message);
reject(err.message);
});
});
}
/********************************************************
********STEP 4: Define the main run function ************
********************************************************/
let run = function (args) {
return new Promise((resolve, reject) => {
let number = 0;
return req(options).then((data) => {
number = data.number + 1;
options.path = '/job/Demo/build?token=<<INSERT BUILD TOKEN>>';
return req(options).then(() => {
options.path = "/job/Demo/" + number + "/api/json?tree=building,number,result,executor[progress]";
setTimeout(function () {
var interval = setInterval(function () {
return req(options).then((data) => {
if (!data.building) {
clearInterval(interval);
options.path = "/job/Demo/" + number + "/consoleText";
return req(options).then((log) => {
//Console.log will write to the Plutora execution log file
console.log("Build Number: " + number);
console.log("Build Log: " + log);
console.log(data.result);
if (data.result === "SUCCESS") {
return resolve();
} else {
return reject();
}
}, (err) => {
return reject(err);
});
}
//Notifier.progress will send updates back to the UI in Plutora
notifier.progress('Building...' + data.executor.progress + '%');
}, (err) => {
clearInterval(interval);
return reject(err);
});
}, 3000);
}, 5000);
}, (err) => {
return reject(err);
});
}, (err) => {
return reject(err);
});
});
};
/********************************************************
***************STEP 5: RUN SCRIPT ***********************
********************************************************/
module.exports = {
run: run
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment