-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
111 lines (103 loc) · 3.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const request = require('request');
const core = require('@actions/core');
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 requestJenkinsJob(jobName, params, headers) {
const jenkinsEndpoint = core.getInput('url');
const req = {
method: 'POST',
url: `${jenkinsEndpoint}/job/${jobName}/buildWithParameters`,
form: params,
headers: headers
}
await 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, headers) {
const jenkinsEndpoint = core.getInput('url');
const req = {
method: 'get',
url: `${jenkinsEndpoint}/job/${jobName}/lastBuild/api/json`,
headers: headers
}
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, headers) {
core.info(`>>> Waiting for "${jobName}" ...`);
while (true) {
let data = await getJobStatus(jobName, headers);
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 {
// User input params
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()}`);
}
// create auth token for Jenkins API
const API_TOKEN = Buffer.from(`${core.getInput('user_name')}:${core.getInput('api_token')}`).toString('base64');
let headers = {
'Authorization': `Basic ${API_TOKEN}`
}
if (core.getInput('headers')) {
let user_headers = JSON.parse(core.getInput('headers'));
headers = {
...headers,
...user_headers
}
}
// POST API call
await requestJenkinsJob(jobName, params, headers);
// Waiting for job completion
if (core.getInput('wait') == 'true') {
await waitJenkinsJob(jobName, startTs, headers);
}
} catch (err) {
core.setFailed(err.message);
core.error(err.message);
} finally {
clearTimeout(timer);
}
}
process.env.NODE_TLS_REJECT_UNAUTHORIZED="0";
main();