|
1 | 1 | const core = require('@actions/core'); |
2 | | - |
3 | 2 | const { execSync } = require('child_process'); |
4 | 3 |
|
5 | 4 | const host = core.getInput('host'); |
6 | 5 | const port = core.getInput('port'); |
7 | 6 | const key = core.getInput('key'); |
8 | 7 | const lifetimeInSeconds = core.getInput('lifetime'); |
| 8 | +const shouldPurgeEntry = core.getBooleanInput('purge-entry', {required: false}); |
9 | 9 | let socketPath = core.getInput('socket-path'); |
10 | 10 |
|
11 | | -// Create random socket path, if none passed. |
12 | | -if (!socketPath) { |
13 | | - try { |
14 | | - socketPath = execSync('mktemp -u', {encoding: 'utf-8'}).trim(); |
15 | | - } catch (e) { |
16 | | - core.setFailed(e.message); |
17 | | - process.exit(1); |
18 | | - } |
19 | | -} |
20 | | - |
21 | | -console.log(`Attempting to create ${socketPath}...`); |
| 11 | +// Check if we already have a pid & sock. |
| 12 | +const pid = process.env.SSH_AGENT_PID; |
| 13 | +const sock = process.env.SSH_AUTH_SOCK; |
22 | 14 |
|
23 | 15 | // Prepare the host file. |
24 | 16 | execSync('mkdir -p ~/.ssh'); |
25 | 17 | execSync('touch ~/.ssh/known_hosts'); |
26 | | -execSync(`sed -i -e '/^${host} /d' ~/.ssh/known_hosts`); |
27 | | -execSync(`ssh-keyscan${port ? ` -p ${port}` : ''} "${host}" >> ~/.ssh/known_hosts`); |
28 | 18 |
|
29 | | -// Start the agent (or re-use one) |
30 | | -try { |
31 | | - execSync(`ssh-agent -a "${socketPath}"`) |
32 | | -} catch (e) { |
33 | | - if (e.message.includes('Address already in use')) { |
34 | | - core.info('Agent already exists on sock. Skipping creation.'); |
35 | | - } else { |
36 | | - core.setFailed(e.message); |
37 | | - process.exit(1); |
38 | | - } |
39 | | -} |
| 19 | +if (pid) { |
| 20 | + core.info('SSH Agent already running. Skipping spawn of ssh-agent...'); |
40 | 21 |
|
41 | | -// Pluck the pid and set values (if possible) |
42 | | -try { |
43 | | - const pid = parseInt(execSync(`fuser ${socketPath} 2> /dev/null`, {encoding: 'utf-8'})); |
| 22 | + core.exportVariable('SSH_AUTH_SOCK', sock); |
| 23 | + core.setOutput('socket-path', sock); |
44 | 24 | core.exportVariable('SSH_AGENT_PID', pid); |
45 | 25 | core.setOutput('agent-pid', pid); |
46 | | -} catch (e) { |
47 | | - core.warning('PID capture failed (fuser). Skipping...'); |
| 26 | +} else { |
| 27 | + // Create random socket path, if none passed. |
| 28 | + if (!socketPath) { |
| 29 | + try { |
| 30 | + socketPath = execSync('mktemp -u', {encoding: 'utf-8'}).trim(); |
| 31 | + } catch (e) { |
| 32 | + core.setFailed(e.message); |
| 33 | + process.exit(1); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + console.log(`Attempting to create ${socketPath}...`); |
| 38 | + |
| 39 | + try { |
| 40 | + execSync(`ssh-agent -a "${socketPath}"`) |
| 41 | + } catch (e) { |
| 42 | + if (e.message.includes('Address already in use')) { |
| 43 | + core.info('Agent already exists on sock. Skipping creation.'); |
| 44 | + } else { |
| 45 | + core.setFailed(e.message); |
| 46 | + process.exit(1); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Pluck the pid and set values (if possible) |
| 51 | + try { |
| 52 | + const pid = parseInt(execSync(`fuser ${socketPath} 2> /dev/null`, {encoding: 'utf-8'})); |
| 53 | + core.exportVariable('SSH_AGENT_PID', pid); |
| 54 | + core.setOutput('agent-pid', pid); |
| 55 | + } catch (e) { |
| 56 | + core.warning('PID capture failed (fuser). Skipping...'); |
| 57 | + } |
| 58 | + |
| 59 | + core.exportVariable('SSH_AUTH_SOCK', socketPath); |
| 60 | + core.setOutput('socket-path', socketPath); |
48 | 61 | } |
49 | 62 |
|
50 | | -// Add the key and set outputs |
51 | | -core.exportVariable('SSH_AUTH_SOCK', socketPath); |
52 | | -core.setOutput('socket-path', socketPath); |
| 63 | +if (shouldPurgeEntry) { |
| 64 | + execSync(`sed -i -e '/^${host} /d' ~/.ssh/known_hosts`); |
| 65 | +} |
| 66 | +execSync(`ssh-keyscan${port ? ` -p ${port}` : ''} "${host}" >> ~/.ssh/known_hosts`); |
| 67 | + |
53 | 68 | execSync(`echo "${key}" | base64 -d | ssh-add -t ${lifetimeInSeconds} -`); |
54 | 69 | core.info('Done; exiting.'); |
0 commit comments