deployphp/index.js

82 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2020-11-10 22:03:10 +00:00
const core = require('@actions/core')
const fs = require('fs')
const execa = require('execa')
2021-10-15 20:41:21 +00:00
void async function main() {
2020-11-10 22:05:39 +00:00
try {
2021-10-15 20:41:21 +00:00
await ssh()
await dep()
2020-11-10 22:05:39 +00:00
} catch (err) {
core.setFailed(err.message)
}
2020-11-10 22:03:10 +00:00
}()
2021-10-15 20:41:21 +00:00
async function ssh() {
2021-10-15 21:07:42 +00:00
let sshHomeDir = `${process.env['HOME']}/.ssh`
2021-10-15 21:07:42 +00:00
if (!fs.existsSync(sshHomeDir)) {
fs.mkdirSync(sshHomeDir)
}
2020-11-10 22:03:10 +00:00
let authSock = '/tmp/ssh-auth.sock'
execa.sync('ssh-agent', ['-a', authSock])
core.exportVariable('SSH_AUTH_SOCK', authSock)
let privateKey = core.getInput('private-key').replace('/\r/g', '').trim() + '\n'
execa.sync('ssh-add', ['-'], {input: privateKey})
2021-05-01 09:32:31 +00:00
const knownHosts = core.getInput('known-hosts')
if (knownHosts !== '') {
2021-10-15 21:07:42 +00:00
fs.appendFileSync(`${sshHomeDir}/known_hosts`, knownHosts)
fs.chmodSync(`${sshHomeDir}/known_hosts`, '600')
2021-05-01 09:32:31 +00:00
} else {
2021-10-15 21:07:42 +00:00
fs.appendFileSync(`${sshHomeDir}/config`, `StrictHostKeyChecking no`)
fs.chmodSync(`${sshHomeDir}/config`, '600')
2021-05-01 09:32:31 +00:00
}
2021-10-15 21:07:42 +00:00
let sshConfig = core.getInput('ssh-config')
2021-05-01 09:32:31 +00:00
if (sshConfig !== '') {
2021-10-15 21:07:42 +00:00
fs.writeFileSync(`${sshHomeDir}/config`, sshConfig)
fs.chmodSync(`${sshHomeDir}/config`, '600')
2020-11-10 22:03:10 +00:00
}
}
2021-10-15 20:41:21 +00:00
async function dep() {
2020-11-10 22:03:10 +00:00
let dep
2021-10-15 20:41:21 +00:00
for (let c of ['vendor/bin/dep', 'deployer.phar']) {
2020-11-10 22:03:10 +00:00
if (fs.existsSync(c)) {
dep = c
2021-10-15 21:34:50 +00:00
console.log(`Using "${c}".`)
2020-11-10 22:03:10 +00:00
break
}
}
if (!dep) {
2021-10-15 21:07:42 +00:00
let version = core.getInput('deployer-version')
if (version === '') {
2021-10-15 21:34:50 +00:00
console.log(`Downloading "https://deployer.org/deployer.phar".`)
2021-10-15 21:07:42 +00:00
execa.commandSync('curl -LO https://deployer.org/deployer.phar')
} else {
if (!/^v/.test(version)) {
version = 'v' + version
}
2021-10-15 21:34:50 +00:00
console.log(`Downloading "https://deployer.org/releases/${version}/deployer.phar".`)
2021-10-15 21:07:42 +00:00
execa.commandSync(`curl -LO https://deployer.org/releases/${version}/deployer.phar`)
}
execa.commandSync('sudo chmod +x deployer.phar')
2021-10-15 20:41:21 +00:00
dep = 'deployer.phar'
2020-11-10 22:03:10 +00:00
}
2021-10-15 21:20:14 +00:00
let cmd = core.getInput('dep')
let p = execa.command(`php ${dep} --ansi -v ${cmd}`)
2021-10-15 20:41:21 +00:00
p.stdout.pipe(process.stdout)
p.stderr.pipe(process.stderr)
2021-10-15 21:20:14 +00:00
2021-10-15 20:41:21 +00:00
try {
await p
} catch (err) {
2021-10-15 21:20:14 +00:00
core.setFailed(`Failed: dep ${cmd}`)
2021-10-15 20:41:21 +00:00
}
2020-11-10 22:03:10 +00:00
}