Skip to content

Commit e0146dc

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 fc49353 commit e0146dc

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

.github/workflows/demo.yml

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ jobs:
99
runs-on: ${{ matrix.os }}
1010
steps:
1111
- uses: actions/checkout@v2
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

dist/index.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,25 @@ try {
139139
fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl\n');
140140
fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==\n');
141141

142-
console.log("Starting ssh-agent");
143-
144142
const authSock = core.getInput('ssh-auth-sock');
145143
const sshAgentArgs = (authSock && authSock.length > 0) ? ['-a', authSock] : [];
146144

147-
// Extract auth socket path and agent pid and set them as job variables
148-
child_process.execFileSync(sshAgent, sshAgentArgs).toString().split("\n").forEach(function(line) {
149-
const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line);
150-
151-
if (matches && matches.length > 0) {
152-
// This will also set process.env accordingly, so changes take effect for this script
153-
core.exportVariable(matches[1], matches[2])
154-
console.log(`${matches[1]}=${matches[2]}`);
155-
}
156-
});
145+
if (child_process.spawnSync(sshAdd, ['-l'], { env: { ...process.env, SSH_AUTH_SOCK: authSock || process.env.SSH_AUTH_SOCK } }) === 0) {
146+
console.log('ssh-agent is already running, not starting a new one')
147+
} else {
148+
console.log("Starting ssh-agent");
149+
150+
// Extract auth socket path and agent pid and set them as job variables
151+
child_process.execFileSync(sshAgent, sshAgentArgs).toString().split("\n").forEach(function(line) {
152+
const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line);
153+
154+
if (matches && matches.length > 0) {
155+
// This will also set process.env accordingly, so changes take effect for this script
156+
core.exportVariable(matches[1], matches[2])
157+
console.log(`${matches[1]}=${matches[2]}`);
158+
}
159+
});
160+
}
157161

158162
console.log("Adding private key(s) to agent");
159163

index.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,25 @@ 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);
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-
});
28+
if (child_process.spawnSync(sshAdd, ['-l'], { env: { ...process.env, SSH_AUTH_SOCK: authSock || process.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+
}
4044

4145
console.log("Adding private key(s) to agent");
4246

0 commit comments

Comments
 (0)