Skip to content

Commit 39a1f2c

Browse files
committed
Don't start ssh-agent if it's already running
This allows to run the action multiple times to add multiple keys without removing already added ones.
1 parent 28cb4d8 commit 39a1f2c

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

dist/index.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -342,21 +342,26 @@ try {
342342
fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl\n');
343343
fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==\n');
344344

345-
console.log("Starting ssh-agent");
346-
347345
const authSock = core.getInput('ssh-auth-sock');
348346
const sshAgentArgs = (authSock && authSock.length > 0) ? ['-a', authSock] : [];
349347

350-
// Extract auth socket path and agent pid and set them as job variables
351-
child_process.execFileSync(sshAgent, sshAgentArgs).toString().split("\n").forEach(function(line) {
352-
const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line);
353348

354-
if (matches && matches.length > 0) {
355-
// This will also set process.env accordingly, so changes take effect for this script
356-
core.exportVariable(matches[1], matches[2])
357-
console.log(`${matches[1]}=${matches[2]}`);
358-
}
359-
});
349+
if (child_process.spawnSync(sshAdd, ['-l'], { env: { ...process.env, SSH_AUTH_SOCK: authSock || process.env.SSH_AUTH_SOCK } }).status === 0) {
350+
console.log('ssh-agent is already running, not starting a new one')
351+
} else {
352+
console.log("Starting ssh-agent");
353+
354+
// Extract auth socket path and agent pid and set them as job variables
355+
child_process.execFileSync(sshAgent, sshAgentArgs).toString().split("\n").forEach(function(line) {
356+
const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line);
357+
358+
if (matches && matches.length > 0) {
359+
// This will also set process.env accordingly, so changes take effect for this script
360+
core.exportVariable(matches[1], matches[2])
361+
console.log(`${matches[1]}=${matches[2]}`);
362+
}
363+
});
364+
}
360365

361366
console.log("Adding private key(s) to agent");
362367

index.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,26 @@ try {
2222
fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl\n');
2323
fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==\n');
2424

25-
console.log("Starting ssh-agent");
26-
2725
const authSock = core.getInput('ssh-auth-sock');
2826
const sshAgentArgs = (authSock && authSock.length > 0) ? ['-a', authSock] : [];
2927

30-
// Extract auth socket path and agent pid and set them as job variables
31-
child_process.execFileSync(sshAgent, sshAgentArgs).toString().split("\n").forEach(function(line) {
32-
const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line);
3328

34-
if (matches && matches.length > 0) {
35-
// This will also set process.env accordingly, so changes take effect for this script
36-
core.exportVariable(matches[1], matches[2])
37-
console.log(`${matches[1]}=${matches[2]}`);
38-
}
39-
});
29+
if (child_process.spawnSync(sshAdd, ['-l'], { env: { ...process.env, SSH_AUTH_SOCK: authSock || process.env.SSH_AUTH_SOCK } }).status === 0) {
30+
console.log('ssh-agent is already running, not starting a new one')
31+
} else {
32+
console.log("Starting ssh-agent");
33+
34+
// Extract auth socket path and agent pid and set them as job variables
35+
child_process.execFileSync(sshAgent, sshAgentArgs).toString().split("\n").forEach(function(line) {
36+
const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line);
37+
38+
if (matches && matches.length > 0) {
39+
// This will also set process.env accordingly, so changes take effect for this script
40+
core.exportVariable(matches[1], matches[2])
41+
console.log(`${matches[1]}=${matches[2]}`);
42+
}
43+
});
44+
}
4045

4146
console.log("Adding private key(s) to agent");
4247

0 commit comments

Comments
 (0)