-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
99 lines (89 loc) · 4.28 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
const core = require('@actions/core');
const axios = require('axios');
const main = async () => {
const timeStart = new Date();
try {
const maxWaitSeconds = 60 * 5;
const hostname = core.getInput('hostname', {required: true});
const port = core.getInput('cPanelApiPort', {required: true});
const repository_root = core.getInput('repository_root', {required: true});
const updateRepository = core.getBooleanInput('updateRepository', {required: true});
const cpanel_token = core.getInput('cpanel_token', {required: true});
const cpanel_username = core.getInput('cpanel_username', {required: true});
const baseUrl = `${hostname}:${port}/execute`;
core.info(`baseUrl: '${baseUrl}'`);
const updateRepoEndpoint = baseUrl + "/VersionControl/update";
const createDeploymentTaskEndpoint = baseUrl + "/VersionControlDeployment/create";
const getDeploymentStatusEndpoint = baseUrl + "/VersionControlDeployment/retrieve";
if (updateRepository) {
const branch = core.getInput('branch', {required: true});
let updateRes = await axios.get(updateRepoEndpoint, {
port: port,
params: {
repository_root,
branch,
},
headers: {"Authorization": `cpanel ${cpanel_username}:${cpanel_token}`}
});
updateRes = updateRes.data;
core.debug(`updateRes: ${JSON.stringify(updateRes, null, 2)}`);
if (updateRes.errors !== null) {
// noinspection ExceptionCaughtLocallyJS
throw new Error(updateRes.errors);
}
}
let startDeployRes = await axios.get(createDeploymentTaskEndpoint, {
params: {
repository_root,
},
headers: {"Authorization": `cpanel ${cpanel_username}:${cpanel_token}`}
});
startDeployRes = startDeployRes.data;
core.debug(`startDeployRes: ${JSON.stringify(startDeployRes, null, 2)}`);
if (startDeployRes.errors !== null) {
// noinspection ExceptionCaughtLocallyJS
throw new Error("Failed to start deployment task: " + JSON.stringify(startDeployRes.errors, null, 2));
}
const taskId = startDeployRes.data.task_id;
if (!taskId) {
// noinspection ExceptionCaughtLocallyJS
throw new Error("Failed to start deployment task - task_id = " + taskId);
}
for (let i=0; i<maxWaitSeconds; i++) {
core.info(`polling iteration ${i}`);
let pollRes = await axios.get(getDeploymentStatusEndpoint, {
headers: {"Authorization": `cpanel ${cpanel_username}:${cpanel_token}`}
});
pollRes = pollRes.data;
if (pollRes.errors != null){
}
const taskData = pollRes.data.filter( info => info.task_id === taskId )[0];
if (taskData.timestamps.succeeded != null) {
core.info(`task succeeded at ${taskData.timestamps.succeeded}`);
break;
}
if (taskData.timestamps.failed != null) {
core.info(`task failed at ${taskData.timestamps.failed}`);
core.info(`errors: ${pollRes.errors}`);
core.info(`messages: ${pollRes.messages}`);
core.debug(`latest poll result: ${pollRes}`);
// noinspection ExceptionCaughtLocallyJS
throw new Error(`Task failed to deploy. errors: ${pollRes.errors}`);
}
//not failed nor success - wait
core.debug(`task ${taskId} still running. taskData: ${JSON.stringify(taskData, null, 2)}`);
await new Promise(r => setTimeout(r, 1000));
}
const duration = new Date() - timeStart;
core.setOutput("duration", duration);
core.info(`deployment duration: ${duration}`);
} catch (error) {
const duration = new Date() - timeStart;
const errorBody = error.response?.data;
core.info(`failed deployment duration: ${duration}`);
core.debug(`errorBody: ${errorBody}`);
core.setOutput("duration", duration);
core.setFailed(error.message + (errorBody == null ? "" : `\n${errorBody}` ));
}
};
main();