deployphp/index.js

134 lines
3.6 KiB
JavaScript
Raw Normal View History

2023-01-10 14:46:25 +00:00
import core from '@actions/core'
import { $, fs, cd } from 'zx'
2020-11-10 22:03:10 +00:00
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() {
2023-01-10 14:46:25 +00:00
if (core.getBooleanInput('skip-ssh-setup')) {
return
}
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'
2023-01-10 14:46:25 +00:00
await $`ssh-agent -a ${authSock}`
2020-11-10 22:03:10 +00:00
core.exportVariable('SSH_AUTH_SOCK', authSock)
let privateKey = core.getInput('private-key')
if (privateKey !== '') {
privateKey = privateKey.replace('/\r/g', '').trim() + '\n'
2023-01-10 14:46:25 +00:00
let p = $`ssh-add -`
p.stdin.write(privateKey)
p.stdin.end()
await p
}
2020-11-10 22:03:10 +00:00
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() {
2021-10-21 20:08:17 +00:00
let dep = core.getInput('deployer-binary')
let subDirectory = core.getInput('sub-directory').trim()
if (subDirectory !== '') {
cd(subDirectory)
}
2021-10-21 20:08:17 +00:00
if (dep === '')
2023-01-10 14:46:25 +00:00
for (let c of ['vendor/bin/deployer.phar', 'vendor/bin/dep', 'deployer.phar']) {
if (fs.existsSync(c)) {
dep = c
console.log(`Using "${c}".`)
break
}
2020-11-10 22:03:10 +00:00
}
2021-10-21 20:08:17 +00:00
if (dep === '') {
2021-10-15 21:07:42 +00:00
let version = core.getInput('deployer-version')
2023-01-10 14:46:25 +00:00
if (version === '' && fs.existsSync('composer.lock')) {
let lock = JSON.parse(fs.readFileSync('composer.lock', 'utf8'))
if (lock['packages']) {
version = lock['packages']
.find(p => p.name === 'deployer/deployer')
?.version
2021-10-24 17:26:47 +00:00
}
if ((version === '' || typeof version === 'undefined') && lock['packages-dev']) {
2023-01-10 14:46:25 +00:00
version = lock['packages-dev']
.find(p => p.name === 'deployer/deployer')
?.version
2023-01-10 14:46:25 +00:00
}
}
if (version === '' || typeof version === 'undefined') {
2023-01-10 14:46:25 +00:00
throw new Error('Deployer binary not found. Please specify deployer-binary or deployer-version.')
}
version = version.replace(/^v/, '')
let manifest = JSON.parse((await $`curl -L https://deployer.org/manifest.json`).stdout)
let url
for (let asset of manifest) {
if (asset.version === version) {
url = asset.url
break
2021-10-15 21:07:42 +00:00
}
}
if (typeof url === 'undefined') {
core.setFailed(`The version "${version}"" does not exist in the "https://deployer.org/manifest.json" file."`)
2023-01-10 14:46:25 +00:00
} else {
console.log(`Downloading "${url}".`)
await $`curl -LO ${url}`
}
await $`sudo chmod +x deployer.phar`
2021-10-15 20:41:21 +00:00
dep = 'deployer.phar'
2020-11-10 22:03:10 +00:00
}
2023-01-10 17:10:04 +00:00
let cmd = core.getInput('dep').split(' ')
let recipe = core.getInput('recipe')
if (recipe !== '') {
recipe = `--file=${recipe}`
}
2023-01-10 14:46:25 +00:00
let ansi = core.getBooleanInput('ansi') ? '--ansi' : '--no-ansi'
let verbosity = core.getInput('verbosity')
let options = []
try {
let optionsArg = core.getInput('options')
if (optionsArg !== '') {
2023-06-22 11:20:39 +00:00
for (let [key, value] in Object.entries(JSON.parse(optionsArg))) {
options.push('-o', `${key}=${value}`)
}
2023-01-10 14:46:25 +00:00
}
2023-06-22 11:20:39 +00:00
} catch (e) {
console.error('Invalid JSON in options')
}
2021-10-15 21:20:14 +00:00
2021-10-15 20:41:21 +00:00
try {
await $`php ${dep} ${cmd} ${recipe} --no-interaction ${ansi} ${verbosity} ${options}`
2021-10-15 20:41:21 +00:00
} 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
}