-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
69 lines (58 loc) · 2.17 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
const core = require('@actions/core');
const { execSync } = require('child_process');
const host = core.getInput('host');
const port = core.getInput('port');
const key = core.getInput('key');
const lifetimeInSeconds = core.getInput('lifetime');
const shouldPurgeEntry = core.getBooleanInput('purge-entry', {required: false});
let socketPath = core.getInput('socket-path');
// Check if we already have a pid & sock.
const pid = process.env.SSH_AGENT_PID;
const sock = process.env.SSH_AUTH_SOCK;
// Prepare the host file.
execSync('mkdir -p ~/.ssh');
execSync('touch ~/.ssh/known_hosts');
if (pid) {
core.info('SSH Agent already running. Skipping spawn of ssh-agent...');
core.exportVariable('SSH_AUTH_SOCK', sock);
core.setOutput('socket-path', sock);
core.exportVariable('SSH_AGENT_PID', pid);
core.setOutput('agent-pid', pid);
} else {
// Create random socket path, if none passed.
if (!socketPath) {
try {
socketPath = execSync('mktemp -u', {encoding: 'utf-8'}).trim();
} catch (e) {
core.setFailed(e.message);
process.exit(1);
}
}
console.log(`Attempting to create ${socketPath}...`);
try {
execSync(`ssh-agent -a "${socketPath}"`)
} catch (e) {
if (e.message.includes('Address already in use')) {
core.info('Agent already exists on sock. Skipping creation.');
} else {
core.setFailed(e.message);
process.exit(1);
}
}
// Pluck the pid and set values (if possible)
try {
const pid = parseInt(execSync(`fuser ${socketPath} 2> /dev/null`, {encoding: 'utf-8'}));
core.exportVariable('SSH_AGENT_PID', pid);
core.setOutput('agent-pid', pid);
} catch (e) {
core.info('PID capture failed (fuser). Skipping...');
}
core.exportVariable('SSH_AUTH_SOCK', socketPath);
core.setOutput('socket-path', socketPath);
}
if (shouldPurgeEntry) {
execSync(`sed -i -e '/^${host} /d' ~/.ssh/known_hosts`);
}
execSync(`ssh-keyscan${port ? ` -p ${port}` : ''} "${host}" >> ~/.ssh/known_hosts`);
execSync(`echo "${key}" | base64 -d | ssh-add -t ${lifetimeInSeconds} -`);
core.info('Done; exiting.');