Skip to content

Commit

Permalink
Kill ssh-agent at end of job
Browse files Browse the repository at this point in the history
  • Loading branch information
s0 committed Oct 20, 2019
1 parent 4b2fbb7 commit f7a5bca
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ const KNOWN_HOSTS_GITHUB = path.join(RESOURCES, 'known_hosts_github.com');
const SSH_FOLDER = path.join(homedir(), '.ssh');
const KNOWN_HOSTS_TARGET = path.join(SSH_FOLDER, 'known_hosts');

const SSH_AGENT_PID_EXTRACT = /SSH_AGENT_PID=([0-9]+);/;

interface BaseConfig {
branch: string;
folder: string;
Expand Down Expand Up @@ -221,7 +223,10 @@ const writeToProcess = (command: string, args: string[], opts: {env: { [id: stri

// Setup ssh-agent with private key
console.log(`Setting up ssh-agent on ${SSH_AUTH_SOCK}`);
await exec(`ssh-agent -a ${SSH_AUTH_SOCK}`, {env});
const sshAgentMatch = SSH_AGENT_PID_EXTRACT.exec((await exec(`ssh-agent -a ${SSH_AUTH_SOCK}`, {env})).stdout);
if (!sshAgentMatch)
throw new Error('Unexpected output from ssh-agent');
env.SSH_AGENT_PID = sshAgentMatch[1];
console.log(`Adding private key to ssh-agent at ${SSH_AUTH_SOCK}`);
await writeToProcess('ssh-add', ['-'], {
data: config.privateKey + '\n',
Expand Down Expand Up @@ -290,6 +295,11 @@ const writeToProcess = (command: string, args: string[], opts: {env: { [id: stri
console.log(push.stdout);
console.log(`##[info] Deployment Successful`);

if (config.mode === 'ssh') {
console.log(`##[info] Killing ssh-agent`);
await exec(`ssh-agent -k`, { env });
}

})().catch(err => {
console.error(err);
process.exit(1);
Expand Down

0 comments on commit f7a5bca

Please sign in to comment.