-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathindex.ts
More file actions
172 lines (152 loc) · 4.56 KB
/
Copy pathindex.ts
File metadata and controls
172 lines (152 loc) · 4.56 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
import * as core from '@actions/core'
import { $, fs, cd } from 'zx'
import { findLocalDeployerBinary } from './deployerBinary'
$.verbose = true
interface ComposerLock {
packages?: Array<{ name: string; version: string }>
'packages-dev'?: Array<{ name: string; version: string }>
}
interface DeployerManifestEntry {
version: string
url: string
}
void (async function main(): Promise<void> {
try {
await ssh()
await dep()
} catch (err) {
core.setFailed(err instanceof Error ? err.message : String(err))
}
})()
async function ssh(): Promise<void> {
if (core.getBooleanInput('skip-ssh-setup')) {
return
}
const sshHomeDir = `${process.env['HOME']}/.ssh`
if (!fs.existsSync(sshHomeDir)) {
fs.mkdirSync(sshHomeDir)
}
const 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'
const 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')
}
const sshConfig = core.getInput('ssh-config')
if (sshConfig !== '') {
fs.writeFileSync(`${sshHomeDir}/config`, sshConfig)
fs.chmodSync(`${sshHomeDir}/config`, '600')
}
}
async function dep(): Promise<void> {
let bin = core.getInput('deployer-binary')
const subDirectory = core.getInput('sub-directory').trim()
if (subDirectory !== '') {
cd(subDirectory)
}
if (bin === '') {
bin = await findLocalDeployerBinary({
fs,
getComposerGlobalHome: async () => {
try {
return (await $`composer -n config --global home`).stdout.trim()
} catch {
return undefined
}
},
})
if (bin !== '') {
console.log(`Using "${bin}".`)
}
}
if (bin === '') {
let version: string | undefined = core.getInput('deployer-version')
if (version === '' && fs.existsSync('composer.lock')) {
const lock: ComposerLock = JSON.parse(
fs.readFileSync('composer.lock', 'utf8'),
)
if (lock.packages) {
version = lock.packages.find(
(p) => p.name === 'deployer/deployer',
)?.version
}
if ((version === '' || version === undefined) && lock['packages-dev']) {
version = lock['packages-dev'].find(
(p) => p.name === 'deployer/deployer',
)?.version
}
}
if (version === '' || version === undefined) {
throw new Error(
'Deployer binary not found. Please specify deployer-binary or deployer-version.',
)
}
version = version.replace(/^v/, '')
const manifest: DeployerManifestEntry[] = JSON.parse(
(await $`curl -L https://deployer.org/manifest.json`).stdout,
)
let url: string | undefined
for (const asset of manifest) {
if (asset.version === version) {
url = asset.url
break
}
}
if (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`
bin = 'deployer.phar'
}
const cmd = core.getInput('dep').split(' ')
const recipeArgs: string[] = []
const recipeInput = core.getInput('recipe')
if (recipeInput !== '') {
recipeArgs.push(`--file=${recipeInput}`)
}
const ansi = core.getBooleanInput('ansi') ? '--ansi' : '--no-ansi'
const verbosityArgs: string[] = []
const verbosityInput = core.getInput('verbosity')
if (verbosityInput !== '') {
verbosityArgs.push(verbosityInput)
}
const options: string[] = []
try {
const optionsArg = core.getInput('options')
if (optionsArg !== '') {
for (const [key, value] of Object.entries(JSON.parse(optionsArg))) {
options.push('-o', `${key}=${value}`)
}
}
} catch (e) {
console.error('Invalid JSON in options')
}
let phpBin = 'php'
const phpBinArg = core.getInput('php-binary')
if (phpBinArg !== '') {
phpBin = phpBinArg
}
try {
await $`${phpBin} ${bin} ${cmd} ${recipeArgs} --no-interaction ${ansi} ${verbosityArgs} ${options}`
} catch (err) {
core.setFailed(`Failed: dep ${cmd}`)
}
}