From c6eb7ee1d8c524a5f411bccbd84e4a3712759837 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Tue, 14 Jan 2020 09:29:16 +0000 Subject: [PATCH] Exit with a helpful error message when the secret has not been configured --- CHANGELOG.md | 2 ++ dist/index.js | 13 ++++++++++++- index.js | 13 ++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d07023..ac50e04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Multiple SSH keys can now be provided (#14, closes #7). Thanks to @webknjaz and @bradmartin for support and tests. +* Catch empty ssh-private-key input values and exit with a helpful + error message right away. diff --git a/dist/index.js b/dist/index.js index 602df64..5ee5d2f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -58,9 +58,18 @@ const child_process = __webpack_require__(129); const fs = __webpack_require__(747); try { + const home = process.env['HOME']; const homeSsh = home + '/.ssh'; + const privateKey = core.getInput('ssh-private-key').trim(); + + if (!privateKey) { + core.setFailed("The ssh-private-key argument is empty. Maybe the secret has not been configured, or you are using a wrong secret name in your workflow file."); + + return; + } + console.log(`Adding GitHub.com keys to ${homeSsh}/known_hosts`); fs.mkdirSync(homeSsh, { recursive: true}); fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==\n'); @@ -72,11 +81,13 @@ try { core.exportVariable('SSH_AUTH_SOCK', authSock); console.log("Adding private key to agent"); - core.getInput('ssh-private-key').split(/(?=-----BEGIN)/).forEach(function(key) { + privateKey.split(/(?=-----BEGIN)/).forEach(function(key) { child_process.execSync('ssh-add -', { input: key.trim() + "\n" }); }); + console.log("Keys added:"); child_process.execSync('ssh-add -l', { stdio: 'inherit' }); + } catch (error) { core.setFailed(error.message); } diff --git a/index.js b/index.js index 140afb4..2da939f 100644 --- a/index.js +++ b/index.js @@ -3,9 +3,18 @@ const child_process = require('child_process'); const fs = require('fs'); try { + const home = process.env['HOME']; const homeSsh = home + '/.ssh'; + const privateKey = core.getInput('ssh-private-key').trim(); + + if (!privateKey) { + core.setFailed("The ssh-private-key argument is empty. Maybe the secret has not been configured, or you are using a wrong secret name in your workflow file."); + + return; + } + console.log(`Adding GitHub.com keys to ${homeSsh}/known_hosts`); fs.mkdirSync(homeSsh, { recursive: true}); fs.appendFileSync(`${homeSsh}/known_hosts`, '\ngithub.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==\n'); @@ -17,11 +26,13 @@ try { core.exportVariable('SSH_AUTH_SOCK', authSock); console.log("Adding private key to agent"); - core.getInput('ssh-private-key').split(/(?=-----BEGIN)/).forEach(function(key) { + privateKey.split(/(?=-----BEGIN)/).forEach(function(key) { child_process.execSync('ssh-add -', { input: key.trim() + "\n" }); }); + console.log("Keys added:"); child_process.execSync('ssh-add -l', { stdio: 'inherit' }); + } catch (error) { core.setFailed(error.message); }