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

Create index_v2.js #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions index_v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
const request = require('request');
const core = require('@actions/core');

// create auth token for Jenkins API
const API_TOKEN = Buffer.from(`${core.getInput('user_name')}:${core.getInput('api_token')}`).toString('base64');

let timer = setTimeout(() => {
core.setFailed("Job Timeout");
core.error("Exception Error: Timed out");
}, (Number(core.getInput('timeout')) * 1000));

const sleep = (seconds) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, (seconds * 1000));
});
};

async function requestJenkinsJobWithParam(jobName, params) {
const result = await CallJobJenkinsWithParameters(jobName, params);
console.log(result);

}

async function requestJenkinsJobWithoutParam(jobName, params) {
const result = await CallJobJenkinsWithoutParameters(jobName, params);
console.log(result);

}
function CallJobJenkinsWithParameters(jobName, params) {
const jenkinsEndpoint = core.getInput('url');
const req = {
method: 'POST',
url: `${jenkinsEndpoint}/job/${jobName}/buildWithParameters`,
form: params,
headers: {
'Authorization': `Basic ${API_TOKEN}`
}
}
return new Promise((resolve, reject) => request(req)
.on('response', (res) => {
core.info(`>>> Job is started!`);
resolve();
})
.on("error", (err) => {
core.setFailed(err);
core.error(JSON.stringify(err));
clearTimeout(timer);
reject();
})
);
}

function CallJobJenkinsWithoutParameters(jobName, params) {
const jenkinsEndpoint = core.getInput('url');
const req = {
method: 'POST',
url: `${jenkinsEndpoint}/job/${jobName}/build`,
//form: params,
headers: {
'Authorization': `Basic ${API_TOKEN}`
}
}
return new Promise((resolve, reject) => request(req)
.on('response', (res) => {
core.info(`>>> Job is started!`);
resolve();
})
.on("error", (err) => {
core.setFailed(err);
core.error(JSON.stringify(err));
clearTimeout(timer);
reject();
})
);
}

async function getJobStatus(jobName) {
const jenkinsEndpoint = core.getInput('url');
const req = {
method: 'get',
url: `${jenkinsEndpoint}/job/${jobName}/lastBuild/api/json`,
headers: {
'Authorization': `Basic ${API_TOKEN}`
}
}
return new Promise((resolve, reject) =>
request(req, (err, res, body) => {
if (err) {
clearTimeout(timer);
reject(err);
}
resolve(JSON.parse(body));
})
);
}
async function waitJenkinsJob(jobName, timestamp) {
core.info(`>>> Waiting for "${jobName}" ...`);
while (true) {
let data = await getJobStatus(jobName);
if (data.timestamp < timestamp) {
core.info(`>>> Job is not started yet... Wait 5 seconds more...`)
} else if (data.result == "SUCCESS") {
core.info(`>>> Job "${data.fullDisplayName}" successfully completed!`);
break;
} else if (data.result == "FAILURE" || data.result == "ABORTED") {
throw new Error(`Failed job ${data.fullDisplayName}`);
} else {
core.info(`>>> Current Duration: ${data.duration}. Expected: ${data.estimatedDuration}`);
}
await sleep(5); // API call interval
}
}

async function main() {
try {
let params = {};
let startTs = + new Date();
let jobName = core.getInput('job_name');
if (core.getInput('parameter')) {
params = JSON.parse(core.getInput('parameter'));

core.info(`>>> Parameter ${params.toString()}`);
requestJenkinsJobWithParam(jobName, params);
}
else{
requestJenkinsJobWithoutParam(jobName, params);
}
// POST API call
//requestJenkinsJob(jobName, params);

// Waiting for job completion
if (core.getInput('wait') == 'true') {
await waitJenkinsJob(jobName, startTs);
}
} catch (err) {
core.setFailed(err.message);
core.error(err.message);
} finally {
clearTimeout(timer);
}
}

process.env.NODE_TLS_REJECT_UNAUTHORIZED="0";
main();