feat: start working on better self hosted runner support

essentially attempt to spawn ssh agent independently and kill it when the action is over
This commit is contained in:
Sn0wCrack 2024-09-19 13:18:38 +10:00
parent 280404946f
commit dca1d5d96c
3 changed files with 43 additions and 6 deletions

View file

@ -33,6 +33,11 @@ inputs:
default: '' default: ''
description: Content of `~/.ssh/known_hosts` file. description: Content of `~/.ssh/known_hosts` file.
disable-strict-host-checking:
required: false
default: 'true'
description: Disable Strict Host Checking if no known_hosts are provided
ssh-config: ssh-config:
required: false required: false
default: '' default: ''
@ -71,6 +76,7 @@ inputs:
runs: runs:
using: 'node20' using: 'node20'
main: 'index.js' main: 'index.js'
post: 'cleanup.js'
branding: branding:
color: blue color: blue

20
cleanup.js Normal file
View file

@ -0,0 +1,20 @@
import core from '@actions/core'
import { $ } from 'zx'
void (async function main() {
try {
await cleanup()
} catch (err) {
core.setFailed(err.message)
}
})()
async function cleanup() {
if (core.getBooleanInput('skip-ssh-setup')) {
return
}
// Remove all keys from ssh-agent and kill process
await $`ssh-add -D`
await $`kill \$SSH_AGENT_PID`
}

View file

@ -15,15 +15,24 @@ async function ssh() {
return return
} }
let sshHomeDir = `${process.env['HOME']}/.ssh` const sshHomeDir = `${process.env['HOME']}/.ssh`
if (!fs.existsSync(sshHomeDir)) { if (!fs.existsSync(sshHomeDir)) {
fs.mkdirSync(sshHomeDir) fs.mkdirSync(sshHomeDir)
} }
let authSock = '/tmp/ssh-auth.sock' await $`eval \`ssh-agent\``
await $`ssh-agent -a ${authSock}`
core.exportVariable('SSH_AUTH_SOCK', authSock) const sshAgentSocket = await $`echo \$SSH_AUTH_SOCKET`
const sshAgentProcessId = await $`echo \$SSH_AGENT_PID`
if (!sshAgentSocket || !sshAgentProcessId) {
throw new Error('Failed to start ssh-agent')
}
core.exportVariable('SSH_AUTH_SOCK', sshAgentSocket.trim())
core.exportVariable('SSH_AGENT_PID', sshAgentProcessId.trim())
let privateKey = core.getInput('private-key') let privateKey = core.getInput('private-key')
if (privateKey !== '') { if (privateKey !== '') {
@ -39,8 +48,10 @@ async function ssh() {
fs.appendFileSync(`${sshHomeDir}/known_hosts`, knownHosts) fs.appendFileSync(`${sshHomeDir}/known_hosts`, knownHosts)
fs.chmodSync(`${sshHomeDir}/known_hosts`, '600') fs.chmodSync(`${sshHomeDir}/known_hosts`, '600')
} else { } else {
fs.appendFileSync(`${sshHomeDir}/config`, `StrictHostKeyChecking no`) if (core.getBooleanInput('disable-strict-host-checking')) {
fs.chmodSync(`${sshHomeDir}/config`, '600') fs.appendFileSync(`${sshHomeDir}/config`, `StrictHostKeyChecking no`)
fs.chmodSync(`${sshHomeDir}/config`, '600')
}
} }
let sshConfig = core.getInput('ssh-config') let sshConfig = core.getInput('ssh-config')