generated from actions/javascript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
43 lines (32 loc) · 1.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const core = require('@actions/core')
const execa = require('execa')
const fs = require('node:fs/promises')
async function run() {
try {
const privateKey = core.getInput('ssh-private-key', { required: true })
const host = core.getInput('ssh-host', { required: true })
const authSock = core.getInput('ssh-socket')
const port = core.getInput('ssh-port')
// Create the required directory
const sshDir = process.env['HOME'] + '/.ssh'
await fs.mkdir(sshDir, {recursive: true})
console.log('Starting ssh-agent')
// Start the ssh agent
await execa('ssh-agent', ['-a', authSock])
core.exportVariable('SSH_AUTH_SOCK', authSock)
console.log('Adding private key')
// Add the private key
const key = privateKey.replace('/\r/g', '').trim() + '\n'
await execa('ssh-add', ['-'], {input: key})
console.log('Adding host to known_hosts')
// Add the host to the known_hosts file
const {stdout} = await execa('ssh-keyscan', ['-p', port, host])
const knownHostsFile = sshDir + '/known_hosts'
await fs.appendFile(knownHostsFile, stdout)
await fs.chmod(knownHostsFile, '644')
}
catch (error) {
core.setFailed(error.message);
}
}
run()