From 72bad59e39e5060c84e32fc013d2c52eb6f61a81 Mon Sep 17 00:00:00 2001 From: ojab Date: Fri, 17 Feb 2023 12:30:49 +0000 Subject: [PATCH] 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. --- .github/workflows/demo.yml | 11 ++++++----- dist/index.js | 26 +++++++++++++++----------- index.js | 26 +++++++++++++++----------- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index 1cfd2a1..badf93b 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -9,12 +9,14 @@ jobs: runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 - - name: Setup key + - name: Setup first key uses: ./ with: - ssh-private-key: | - ${{ secrets.MPDUDE_TEST_1_DEPLOY_KEY }} - ${{ secrets.MPDUDE_TEST_2_DEPLOY_KEY }} + ssh-private-key: ${{ secrets.MPDUDE_TEST_1_DEPLOY_KEY }} + - name: Setup second key + uses: ./ + with: + ssh-private-key: ${{ secrets.MPDUDE_TEST_2_DEPLOY_KEY }} - run: | git clone https://github.com/mpdude/test-1.git test-1-http git clone git@github.com:mpdude/test-1.git test-1-git @@ -43,4 +45,3 @@ jobs: git clone https://github.com/mpdude/test-2.git test-2-http git clone git@github.com:mpdude/test-2.git test-2-git git clone ssh://git@github.com/mpdude/test-2.git test-2-git-ssh - diff --git a/dist/index.js b/dist/index.js index bdebdb8..4cc25d3 100644 --- a/dist/index.js +++ b/dist/index.js @@ -351,21 +351,25 @@ try { fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl\n'); fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==\n'); - console.log("Starting ssh-agent"); - const authSock = core.getInput('ssh-auth-sock'); const sshAgentArgs = (authSock && authSock.length > 0) ? ['-a', authSock] : []; - // Extract auth socket path and agent pid and set them as job variables - child_process.execFileSync(sshAgentCmd, sshAgentArgs).toString().split("\n").forEach(function(line) { - const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line); + if (child_process.spawnSync(sshAdd, ['-l'], { env: { ...process.env, SSH_AUTH_SOCK: authSock || process.env.SSH_AUTH_SOCK } }).status === 0) { + console.log('ssh-agent is already running, not starting a new one'); + } else { + console.log("Starting ssh-agent"); - if (matches && matches.length > 0) { - // This will also set process.env accordingly, so changes take effect for this script - core.exportVariable(matches[1], matches[2]) - console.log(`${matches[1]}=${matches[2]}`); - } - }); + // Extract auth socket path and agent pid and set them as job variables + child_process.execFileSync(sshAgent, sshAgentArgs).toString().split("\n").forEach(function(line) { + const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line); + + if (matches && matches.length > 0) { + // This will also set process.env accordingly, so changes take effect for this script + core.exportVariable(matches[1], matches[2]); + console.log(`${matches[1]}=${matches[2]}`); + } + }); + } console.log("Adding private key(s) to agent"); diff --git a/index.js b/index.js index 549bc00..1d8eca9 100644 --- a/index.js +++ b/index.js @@ -31,21 +31,25 @@ try { fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl\n'); fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==\n'); - console.log("Starting ssh-agent"); - const authSock = core.getInput('ssh-auth-sock'); const sshAgentArgs = (authSock && authSock.length > 0) ? ['-a', authSock] : []; - // Extract auth socket path and agent pid and set them as job variables - child_process.execFileSync(sshAgentCmd, sshAgentArgs).toString().split("\n").forEach(function(line) { - const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line); + if (child_process.spawnSync(sshAdd, ['-l'], { env: { ...process.env, SSH_AUTH_SOCK: authSock || process.env.SSH_AUTH_SOCK } }).status === 0) { + console.log('ssh-agent is already running, not starting a new one'); + } else { + console.log("Starting ssh-agent"); - if (matches && matches.length > 0) { - // This will also set process.env accordingly, so changes take effect for this script - core.exportVariable(matches[1], matches[2]) - console.log(`${matches[1]}=${matches[2]}`); - } - }); + // Extract auth socket path and agent pid and set them as job variables + child_process.execFileSync(sshAgent, sshAgentArgs).toString().split("\n").forEach(function(line) { + const matches = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(line); + + if (matches && matches.length > 0) { + // This will also set process.env accordingly, so changes take effect for this script + core.exportVariable(matches[1], matches[2]); + console.log(`${matches[1]}=${matches[2]}`); + } + }); + } console.log("Adding private key(s) to agent");