-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.js
69 lines (59 loc) · 2.52 KB
/
lib.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
const core = require('@actions/core');
const fs = require('fs');
const axios = require('axios');
const { setTimeout } = require('timers/promises');
module.exports = async function () {
const WAITSTEP = 5; // seconds between two consecutive checks of the job's status
try {
// eval expression "$VAR" on extravars template file
const values = {
...JSON.parse(core.getInput('vars')),
ARTIFACT_URL: core.getInput('asset_url'),
IMAGE_URL: core.getInput('image_url'),
GITHUB_RUN_ID: process.env.GITHUB_RUN_ID, // Tower workaround to force restart
};
const varsFilename = core.getInput('extravars_template_filename');
const vars = fs
.readFileSync(varsFilename)
.toString()
.replace(/\$(\w+)/g, (m, p1) => (p1 in values ? values[p1] : p1));
// launch Tower by a rest API
const towerTemplateId = core.getInput('tower_template_id');
const towerUrl = core.getInput('tower_url');
const auth = { username: core.getInput('tower_user'), password: core.getInput('tower_password') };
core.info(`⚡️ Launching Tower job ${towerUrl}/job_templates/${towerTemplateId}/launch :\n${vars}`);
const response = await axios({
method: 'POST',
url: `${towerUrl}/job_templates/${towerTemplateId}/launch/`,
auth,
data: vars && { extra_vars: vars },
});
const jobId = response.data.job;
core.info(`🚀 Deploy job launched ${towerUrl}/#/jobs/playbook/${jobId}`);
// poll waiting until the end of job in order to know if it will succed or not
const maxsteps = (core.getInput('tower_timeout') || 300) / WAITSTEP;
let step = 0;
while (step < maxsteps) {
await setTimeout(WAITSTEP * 1000);
const res = await axios({ url: `${towerUrl}/jobs/${jobId}/`, auth });
if (res.data.status === 'successful') {
break;
} else if (res.data.status === 'failed' || res.data.status === 'cancelled') {
core.error(`❌ Deployment failed (${res.data.status}) ${towerUrl}/#/jobs/playbook/${jobId}`);
core.setFailed(res.data);
return -1;
}
core.info(`⌛️ Check ${step}/${maxsteps} : ${res.data.status}`);
step++;
}
core.info(`✅ Automatic deployment succeeded.`);
} catch (error) {
if (error.response && error.response.status === 401) {
core.setFailed('HTTP 401 : maybe you have to check tower_ser/tower_password inputs ?');
} else if (error.response && error.response.data) {
core.setFailed(error.response.data);
} else {
core.setFailed(error);
}
}
};