From 3f570f79fc2db71483ba26b30af5d0f582a86f07 Mon Sep 17 00:00:00 2001 From: Alexander Mirgorod Date: Mon, 30 Sep 2024 17:38:04 +0300 Subject: [PATCH] ability to set instance domain --- action.yml | 3 +++ dist/index.js | 16 ++++++++++------ index.js | 16 ++++++++++------ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/action.yml b/action.yml index 8f32cc8..eb3c506 100644 --- a/action.yml +++ b/action.yml @@ -1,6 +1,9 @@ name: 'webfactory/ssh-agent' description: 'Run `ssh-agent` and load an SSH key to access other private repositories' inputs: + instance-domain: + description: 'Domain name of the instance (gitea/forgejo)' + required: false ssh-private-key: description: 'Private SSH key to register in the SSH agent' required: true diff --git a/dist/index.js b/dist/index.js index 2c23f23..fd2bcf1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -341,6 +341,10 @@ const crypto = __webpack_require__(417); const { homePath, sshAgentCmdDefault, sshAddCmdDefault, gitCmdDefault } = __webpack_require__(972); try { + const instanceDomain = core.getInput('instance-domain', {default: 'github.com'}); + const escapedDomain = instanceDomain.replace(/[-.]/g, '\\$&'); + const regexDomain = new RegExp(`\\b${escapedDomain}[:/]([_.a-z0-9-]+\/[_.a-z0-9-]+)`, 'i'); + const privateKey = core.getInput('ssh-private-key'); const logPublicKey = core.getBooleanInput('log-public-key', {default: true}); @@ -390,7 +394,7 @@ try { console.log('Configuring deployment key(s)'); child_process.execFileSync(sshAddCmd, ['-L']).toString().trim().split(/\r?\n/).forEach(function(key) { - const parts = key.match(/\bgithub\.com[:/]([_.a-z0-9-]+\/[_.a-z0-9-]+)/i); + const parts = key.match(regexDomain); if (!parts) { if (logPublicKey) { @@ -404,12 +408,12 @@ try { fs.writeFileSync(`${homeSsh}/key-${sha256}`, key + "\n", { mode: '600' }); - child_process.execSync(`${gitCmd} config --global --replace-all url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`); - child_process.execSync(`${gitCmd} config --global --add url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "git@github.com:${ownerAndRepo}"`); - child_process.execSync(`${gitCmd} config --global --add url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "ssh://git@github.com/${ownerAndRepo}"`); + child_process.execSync(`${gitCmd} config --global --replace-all url."git@key-${sha256}.${instanceDomain}:${ownerAndRepo}".insteadOf "https://${instanceDomain}/${ownerAndRepo}"`); + child_process.execSync(`${gitCmd} config --global --add url."git@key-${sha256}.${instanceDomain}:${ownerAndRepo}".insteadOf "git@${instanceDomain}:${ownerAndRepo}"`); + child_process.execSync(`${gitCmd} config --global --add url."git@key-${sha256}.${instanceDomain}:${ownerAndRepo}".insteadOf "ssh://git@${instanceDomain}/${ownerAndRepo}"`); - const sshConfig = `\nHost key-${sha256}.github.com\n` - + ` HostName github.com\n` + const sshConfig = `\nHost key-${sha256}.${instanceDomain}\n` + + ` HostName ${instanceDomain}\n` + ` IdentityFile ${homeSsh}/key-${sha256}\n` + ` IdentitiesOnly yes\n`; diff --git a/index.js b/index.js index 0c2e08b..b4873ed 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,10 @@ const crypto = require('crypto'); const { homePath, sshAgentCmdDefault, sshAddCmdDefault, gitCmdDefault } = require('./paths.js'); try { + const instanceDomain = core.getInput('instance-domain', {default: 'github.com'}); + const escapedDomain = instanceDomain.replace(/[-.]/g, '\\$&'); + const regexDomain = new RegExp(`\\b${escapedDomain}[:/]([_.a-z0-9-]+\/[_.a-z0-9-]+)`, 'i'); + const privateKey = core.getInput('ssh-private-key'); const logPublicKey = core.getBooleanInput('log-public-key', {default: true}); @@ -54,7 +58,7 @@ try { console.log('Configuring deployment key(s)'); child_process.execFileSync(sshAddCmd, ['-L']).toString().trim().split(/\r?\n/).forEach(function(key) { - const parts = key.match(/\bgithub\.com[:/]([_.a-z0-9-]+\/[_.a-z0-9-]+)/i); + const parts = key.match(regexDomain); if (!parts) { if (logPublicKey) { @@ -68,12 +72,12 @@ try { fs.writeFileSync(`${homeSsh}/key-${sha256}`, key + "\n", { mode: '600' }); - child_process.execSync(`${gitCmd} config --global --replace-all url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`); - child_process.execSync(`${gitCmd} config --global --add url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "git@github.com:${ownerAndRepo}"`); - child_process.execSync(`${gitCmd} config --global --add url."git@key-${sha256}.github.com:${ownerAndRepo}".insteadOf "ssh://git@github.com/${ownerAndRepo}"`); + child_process.execSync(`${gitCmd} config --global --replace-all url."git@key-${sha256}.${instanceDomain}:${ownerAndRepo}".insteadOf "https://${instanceDomain}/${ownerAndRepo}"`); + child_process.execSync(`${gitCmd} config --global --add url."git@key-${sha256}.${instanceDomain}:${ownerAndRepo}".insteadOf "git@${instanceDomain}:${ownerAndRepo}"`); + child_process.execSync(`${gitCmd} config --global --add url."git@key-${sha256}.${instanceDomain}:${ownerAndRepo}".insteadOf "ssh://git@${instanceDomain}/${ownerAndRepo}"`); - const sshConfig = `\nHost key-${sha256}.github.com\n` - + ` HostName github.com\n` + const sshConfig = `\nHost key-${sha256}.${instanceDomain}\n` + + ` HostName ${instanceDomain}\n` + ` IdentityFile ${homeSsh}/key-${sha256}\n` + ` IdentitiesOnly yes\n`;