-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
77 lines (68 loc) · 1.79 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
const core = require("@actions/core");
const exec = require("@actions/exec");
const io = require("@actions/io");
const fs = require("fs");
const values_list = ["PRIVATE_KEY", "USERNAME", "IP", "PORT", "SHELL"];
// get input values
let { PRIVATE_KEY, USERNAME, IP, PORT, SHELL } = (function () {
let values = [];
values_list.forEach((value) => {
values[value] = core.getInput(value);
});
return values;
})();
function create_file(path, content) {
fs.appendFile(path, content, (err) => {
if (err) throw err;
});
}
async function run() {
try {
if (PRIVATE_KEY.trim().length == 0) {
throw "PRIVATE_KEY cannot be empty";
}
if (USERNAME.trim().length == 0) {
throw "USERNAME cannot be empty";
}
if (IP.trim().length == 0) {
throw "IP cannot be empty";
}
if (PORT.trim().length == 0) {
PORT = 22;
} else {
PORT = parseInt(PORT);
if (isNaN(PORT)) {
throw "invalid PORT";
}
}
// get home dir from environment
const HOME = process.env["HOME"];
core.info(HOME);
// config ssh
await io.mkdirP(HOME + "/.ssh");
await create_file(HOME + "/.ssh/deploy.key", PRIVATE_KEY);
await exec.exec(`ls ${HOME}/.ssh`);
await exec.exec(`chmod 600 ${HOME}/.ssh/deploy.key`);
await create_file(
HOME + "/.ssh/config",
`Host server\n\
HostName ${IP}\n\
User ${USERNAME}\n\
Port ${PORT}\n\
IdentityFile ~/.ssh/deploy.key\n\
StrictHostKeyChecking no\n`
);
await exec.exec("ls -al");
// check if SHELL is not null
if (SHELL.trim().length != 0) {
let shells = SHELL.split("\n");
for (let shell of shells) {
core.info(shell);
await exec.exec("ssh server " + shell);
}
}
} catch (error) {
core.setFailed(error);
}
}
run();