-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
79 lines (65 loc) · 1.87 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
78
79
const fs = require('fs').promises;
const core = require('@actions/core');
const exec = require('@actions/exec');
const io = require('@actions/io');
const reVariables = /^(?<name>SSH_AUTH_SOCK|SSH_AGENT_PID)=(?<value>[^;]+);/;
const run = async () => {
let exitCode;
const key = core.getInput('ssh-private-key', { required: true });
const encoding = core.getInput('encoding');
exitCode = await io.mkdirP('~/.ssh');
if (exitCode) {
core.setFailed('Unable to create ~/.ssh directory');
return;
}
// working around a GitHub toolkit bug
// https://github.com/actions/toolkit/issues/649
try {
const { stdout } = await exec.getExecOutput('ssh-keyscan -t rsa github.com');
await fs.writeFile('~/.ssh/known_hosts', stdout, { flags: 'a' });
}
catch (e) {
core.setFailed('ssh-keyscan failed');
return;
}
let stdout = '';
exitCode = await exec.exec('ssh-agent', [], {
listeners: {
stdout: (data) => {
stdout += data.toString();
},
},
});
if (exitCode) {
core.setFailed('ssh-agent failed to start');
return;
}
stdout.trim().split('\n').forEach((line) => {
const match = line.match(reVariables);
if (match) {
core.exportVariable(match.groups.name, match.groups.value);
core.info(`exported environment variable: ${match.groups.name}=${match.groups.value}`);
}
});
exitCode = await exec.exec('ssh-add', ['-'], {
input: Buffer.from(encoding ? Buffer.from(key, encoding).toString('ascii') : key),
});
if (exitCode) {
core.setFailed('ssh-add failed');
}
};
const cleanup = async () => {
const exitCode = await exec.exec('ssh-agent', ['-k']);
if (exitCode) {
core.setFailed('ssh-agent was not stopped');
}
};
if (core.getState('isPost') === 'true') {
cleanup();
}
else {
core.saveState('isPost', 'true');
run()
.catch((e) => core.setFailed(e.message))
;
}