-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdeployment.js
72 lines (60 loc) · 2.09 KB
/
deployment.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
const fs = require('fs');
const { Octokit } = require('@octokit/rest');
const commandLineArgs = require('command-line-args')
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
});
async function createDeployment(options) {
options.task = 'deploy';
options.required_contexts = [];
let result = await octokit.rest.repos.createDeployment(options)
const id = result.data.id;
console.log(id);
fs.writeFileSync(process.env.HOME + '/deployment_action', id.toString());
console.log(result.status == 201 ? 'Done': 'Error');
if(result.status != 201) {
console.error(result);
process.exit(1);
}
}
async function createDeploymentStatus(options) {
const idTxt = fs.readFileSync(process.env.HOME + '/deployment_action', 'utf8');
options.deployment_id = parseInt(idTxt);
delete options.setstatus;
delete options.ref;
try {
result = await octokit.rest.repos.createDeploymentStatus(options)
console.log(result.status == 201 ? 'Done' : 'Error');
if (result.status != 201) {
console.error(result);
process.exit(1);
}
} catch(e) {
console.error(e);
process.exit(1);
}
}
function main(options) {
if(options.setstatus) {
createDeploymentStatus(options);
} else {
createDeployment(options);
}
}
const optionDefinitions = [
{ name: 'setstatus', alias: 'f', type: Boolean },
{ name: 'owner', alias: 'o', type: String },
{ name: 'repo', alias: 'r', type: String },
{ name: 'ref', alias: 'c', type: String },
{ name: 'deployment_id', alias: 'i', type: Number },
{ name: 'state', alias: 's', type: String },
{ name: 'log_url', alias: 'l', type: String },
{ name: 'description', alias: 'd', type: String },
{ name: 'environment', alias: 'e', type: String },
{ name: 'environment_url', alias: 'u', type: String },
{ name: 'auto_inactive', type: Boolean },
{ name: 'auto_merge', type: Boolean, defaultValue: false },
]
const options = commandLineArgs(optionDefinitions)
console.log('Run', options);
main(options);