-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (55 loc) · 2.06 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
// Inspired by https://github.com/Ilshidur/action-discord
// Released under the MIT License, Copyright (c) 2018 Nicolas Coutin
const axios = require('axios');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const { argv } = yargs(hideBin(process.argv));
const REQUIRED_ENV_VARS = [
'GITHUB_EVENT_PATH',
'GITHUB_REPOSITORY',
'GITHUB_WORKFLOW',
'GITHUB_ACTOR',
'GITHUB_EVENT_NAME',
'GITHUB_ACTION',
];
process.env.GITHUB_ACTION = process.env.GITHUB_ACTION || '<missing GITHUB_ACTION env var>';
REQUIRED_ENV_VARS.forEach((env) => {
if (!process.env[env] || !process.env[env].length) {
console.error(
`Env var ${env} is not defined. Maybe try to set it if you are running the script manually.`,
);
process.exit(1);
}
});
const [endpoint, token, server, job, selector, version, cfAccessClientId, cfAccessClientSecret] = [...argv._];
console.debug('args:', {
endpoint, token, server, job, selector, version, cfAccessClientId, cfAccessClientSecret,
});
const headers = {
Authorization: `${token}`,
'Content-Type': 'application/json',
};
if (cfAccessClientId) {
headers['CF-Access-Client-Id'] = cfAccessClientId;
headers['CF-Access-Client-Secret'] = cfAccessClientSecret;
}
const params = {
server, job, selector, version,
};
const serializedParams = Object.keys(params).reduce((accumulator, value) => ({ ...accumulator, [value]: params[value] ? params[value] : undefined }), {});
console.debug('headers:', headers);
console.debug('serializedParams:', serializedParams);
(async () => {
try {
const response = await axios.post(`${endpoint}/api/deploy`, serializedParams, { headers });
if (typeof response.data !== 'object') {
console.warn(response.data);
throw new Error('The response does not contain JSON');
}
console.debug(response.data);
process.exit(0);
} catch (err) {
console.error('error in POST request:', err.message, err?.response?.data?.message);
process.exit(1);
}
})();