forked from deployphp/action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (117 loc) · 3.6 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import core from '@actions/core'
import { $, fs, cd } from 'zx'
void async function main() {
try {
await ssh()
await dep()
} catch (err) {
core.setFailed(err.message)
}
}()
async function ssh() {
if (core.getBooleanInput('skip-ssh-setup')) {
return
}
let sshHomeDir = `${process.env['HOME']}/.ssh`
if (!fs.existsSync(sshHomeDir)) {
fs.mkdirSync(sshHomeDir)
}
let authSock = '/tmp/ssh-auth.sock'
await $`ssh-agent -a ${authSock}`
core.exportVariable('SSH_AUTH_SOCK', authSock)
let privateKey = core.getInput('private-key')
if (privateKey !== '') {
privateKey = privateKey.replace('/\r/g', '').trim() + '\n'
let p = $`ssh-add -`
p.stdin.write(privateKey)
p.stdin.end()
await p
}
const knownHosts = core.getInput('known-hosts')
if (knownHosts !== '') {
fs.appendFileSync(`${sshHomeDir}/known_hosts`, knownHosts)
fs.chmodSync(`${sshHomeDir}/known_hosts`, '600')
} else {
fs.appendFileSync(`${sshHomeDir}/config`, `StrictHostKeyChecking no`)
fs.chmodSync(`${sshHomeDir}/config`, '600')
}
let sshConfig = core.getInput('ssh-config')
if (sshConfig !== '') {
fs.writeFileSync(`${sshHomeDir}/config`, sshConfig)
fs.chmodSync(`${sshHomeDir}/config`, '600')
}
}
async function dep() {
let dep = core.getInput('deployer-binary')
let subDirectory = core.getInput('sub-directory').trim()
if (subDirectory !== '') {
cd(subDirectory)
}
if (dep === '')
for (let c of ['vendor/bin/deployer.phar', 'vendor/bin/dep', 'deployer.phar']) {
if (fs.existsSync(c)) {
dep = c
console.log(`Using "${c}".`)
break
}
}
if (dep === '') {
let version = core.getInput('deployer-version')
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
}
if ((version === '' || typeof version === 'undefined') && lock['packages-dev']) {
version = lock['packages-dev']
.find(p => p.name === 'deployer/deployer')
?.version
}
}
if (version === '' || typeof version === 'undefined') {
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
}
}
if (url === null) {
console.error(`The version "${version}"" does not exist in the "https://deployer.org/manifest.json" file."`)
} else {
console.log(`Downloading "${url}".`)
await $`curl -LO ${url}`
}
await $`sudo chmod +x deployer.phar`
dep = 'deployer.phar'
}
let cmd = core.getInput('dep').split(' ')
let recipe = core.getInput('recipe')
if (recipe !== '') {
recipe = `--file=${recipe}`
}
let ansi = core.getBooleanInput('ansi') ? '--ansi' : '--no-ansi'
let verbosity = core.getInput('verbosity')
let options = []
try {
let optionsArg = core.getInput('options')
if (optionsArg !== '') {
for (let [key, value] of Object.entries(JSON.parse(optionsArg))) {
options.push('-o', `${key}=${value}`)
}
}
} catch (e) {
console.error('Invalid JSON in options')
}
try {
await $`php ${dep} ${cmd} ${recipe} --no-interaction ${ansi} ${verbosity} ${options}`
} catch (err) {
core.setFailed(`Failed: dep ${cmd}`)
}
}