-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
67 lines (59 loc) · 2.49 KB
/
main.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
const { execSync } = require('child_process');
const core = require('@actions/core');
const remote_docker_host = core.getInput('remote_docker_host', { required: true });
const ssh_private_key = core.getInput('ssh_private_key', { required: true });
const project_name = core.getInput('project_name', { required: true });
const action = core.getInput('action', { required: true });
const AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID;
const AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
const stack_file_name = core.getInput('stack_file_name') || 'docker-compose.yml';
const awsRegion = process.env.AWS_DEFAULT_REGION;
function run(cmd, options = {}) {
if (!options.hide) {
console.log(`$ ${cmd}`);
}
try {
return execSync(cmd, {
shell: '/bin/bash',
encoding: 'utf-8',
env: {
...process.env,
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
},
});
} catch (error) {
core.setFailed(`Deploy failed with error: ${error.message}`);
console.log(error.status);
console.log(error.message); // Holds the message you typically want.
console.log(error.stderr.toString()); // Holds the stderr output. Use `.toString()`.
console.log(error.stdout.toString()); // Holds the stdout output. Use `.toString()`.
}
}
run(`$(aws ecr get-login --no-include-email --region ${awsRegion})`);
const accountData = run(`aws sts get-caller-identity --output json`);
const awsAccountId = JSON.parse(accountData).Account;
// # register the private key with the agent.
run(`echo "Registering SSH keys..."`);
run(`mkdir -p "$HOME/.ssh"`);
run(`printf '%s\n' "${ssh_private_key}" > "$HOME/.ssh/id_rsa"`);
run(`chmod 600 "$HOME/.ssh/id_rsa"`);
run(`eval $(ssh-agent) && ssh-add "$HOME/.ssh/id_rsa"`);
switch (action) {
case 'deploy':
run(`echo "Pull Built Images..."`);
run(
`docker-compose --log-level debug --host ssh://${remote_docker_host} -f ${stack_file_name} pull`
);
run(`echo "Start ${project_name} Services..."`);
run(
`docker-compose -p ${project_name} --log-level debug --host ssh://${remote_docker_host} -f ${stack_file_name} up -d`
);
break;
case 'remove':
run(`echo "Stop ${project_name} Services..."`);
run(
`docker-compose -p ${project_name} --log-level debug --host ssh://${remote_docker_host} -f ${stack_file_name} down -v`
);
break;
}