forked from deployphp/action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (139 loc) · 4.29 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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'
// Clean up any existing SSH agents
try {
await $`killall ssh-agent`
} catch (err) {
// Ignore errors if the agent wasn't running
}
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 {
// Handle SSH config file
const configPath = `${sshHomeDir}/config`
let configContent = ''
if (fs.existsSync(configPath)) {
configContent = fs.readFileSync(configPath, 'utf8')
}
if (!configContent.includes('StrictHostKeyChecking')) {
configContent += `\nStrictHostKeyChecking no\n`
} else {
configContent = configContent.replace(/StrictHostKeyChecking.*\n/g, 'StrictHostKeyChecking no\n')
}
fs.writeFileSync(configPath, configContent)
fs.chmodSync(configPath, '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 (typeof url === 'undefined') {
core.setFailed(`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] in Object.entries(JSON.parse(optionsArg))) {
options.push('-o', `${key}=${value}`)
}
}
} catch (e) {
console.error('Invalid JSON in options')
}
let phpBin = 'php'
let phpBinArg = core.getInput('php-binary');
if (phpBinArg !== '') {
phpBin = phpBinArg
}
try {
await $`${phpBin} ${dep} ${cmd} ${recipe} --no-interaction ${ansi} ${verbosity} ${options}`
} catch (err) {
core.setFailed(`Failed: dep ${cmd}`)
}
}