Skip to content

Commit 72bad59

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 ea17a05 commit 72bad59

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

.github/workflows/demo.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ jobs:
99
runs-on: ${{ matrix.os }}
1010
steps:
1111
- uses: actions/checkout@v3
12-
- name: Setup key
12+
- name: Setup first key
1313
uses: ./
1414
with:
15-
ssh-private-key: |
16-
${{ secrets.MPDUDE_TEST_1_DEPLOY_KEY }}
17-
${{ secrets.MPDUDE_TEST_2_DEPLOY_KEY }}
15+
ssh-private-key: ${{ secrets.MPDUDE_TEST_1_DEPLOY_KEY }}
16+
- name: Setup second key
17+
uses: ./
18+
with:
19+
ssh-private-key: ${{ secrets.MPDUDE_TEST_2_DEPLOY_KEY }}
1820
- run: |
1921
git clone https://github.com/mpdude/test-1.git test-1-http
2022
git clone [email protected]:mpdude/test-1.git test-1-git
@@ -43,4 +45,3 @@ jobs:
4345
git clone https://github.com/mpdude/test-2.git test-2-http
4446
git clone [email protected]:mpdude/test-2.git test-2-git
4547
git clone ssh://[email protected]/mpdude/test-2.git test-2-git-ssh
46-

dist/index.js

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

354-
console.log("Starting ssh-agent");
355-
356354
const authSock = core.getInput('ssh-auth-sock');
357355
const sshAgentArgs = (authSock && authSock.length > 0) ? ['-a', authSock] : [];
358356

359-
// Extract auth socket path and agent pid and set them as job variables
360-
child_process.execFileSync(sshAgentCmd, sshAgentArgs).toString().split("\n").forEach(function(line) {
361-
const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line);
357+
if (child_process.spawnSync(sshAdd, ['-l'], { env: { ...process.env, SSH_AUTH_SOCK: authSock || process.env.SSH_AUTH_SOCK } }).status === 0) {
358+
console.log('ssh-agent is already running, not starting a new one');
359+
} else {
360+
console.log("Starting ssh-agent");
362361

363-
if (matches && matches.length > 0) {
364-
// This will also set process.env accordingly, so changes take effect for this script
365-
core.exportVariable(matches[1], matches[2])
366-
console.log(`${matches[1]}=${matches[2]}`);
367-
}
368-
});
362+
// Extract auth socket path and agent pid and set them as job variables
363+
child_process.execFileSync(sshAgent, sshAgentArgs).toString().split("\n").forEach(function(line) {
364+
const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line);
365+
366+
if (matches && matches.length > 0) {
367+
// This will also set process.env accordingly, so changes take effect for this script
368+
core.exportVariable(matches[1], matches[2]);
369+
console.log(`${matches[1]}=${matches[2]}`);
370+
}
371+
});
372+
}
369373

370374
console.log("Adding private key(s) to agent");
371375

index.js

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

34-
console.log("Starting ssh-agent");
35-
3634
const authSock = core.getInput('ssh-auth-sock');
3735
const sshAgentArgs = (authSock && authSock.length > 0) ? ['-a', authSock] : [];
3836

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

5054
console.log("Adding private key(s) to agent");
5155

0 commit comments

Comments
 (0)