Skip to content

Commit 0e4076a

Browse files
authored
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 fc49353 commit 0e4076a

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

index.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,27 @@ try {
2121
fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=\n');
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');
24-
25-
console.log("Starting ssh-agent");
26-
24+
2725
const authSock = core.getInput('ssh-auth-sock');
2826
const sshAgentArgs = (authSock && authSock.length > 0) ? ['-a', authSock] : [];
29-
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);
33-
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-
});
40-
27+
28+
if (child_process.spawnSync(sshAdd, ['-l'], { env: { ...process.env, SSH_AUTH_SOCK: authSock || env['SSH_AUTH_SOCK'] } }) !== 0) {
29+
console.log('ssh-agent is already running, not starting a new one')
30+
} else {
31+
console.log("Starting ssh-agent");
32+
33+
// Extract auth socket path and agent pid and set them as job variables
34+
child_process.execFileSync(sshAgent, sshAgentArgs).toString().split("\n").forEach(function(line) {
35+
const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line);
36+
37+
if (matches && matches.length > 0) {
38+
// This will also set process.env accordingly, so changes take effect for this script
39+
core.exportVariable(matches[1], matches[2])
40+
console.log(`${matches[1]}=${matches[2]}`);
41+
}
42+
});
43+
}
44+
4145
console.log("Adding private key(s) to agent");
4246

4347
privateKey.split(/(?=-----BEGIN)/).forEach(function(key) {

0 commit comments

Comments
 (0)