This repository has been archived by the owner on Apr 29, 2022. It is now read-only.
forked from InsideTheRopes/action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (84 loc) · 2.96 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
const core = require('@actions/core')
const fs = require('fs')
const execa = require('execa')
const split = require('argv-split')
async function main() {
await prepareSSH()
const deployer = await getDeployer()
await runDeployer(deployer)
}
async function prepareSSH() {
const ssh = `${process.env['HOME']}/.ssh`
if (!fs.existsSync(ssh)) {
fs.mkdirSync(ssh)
const authSock = '/tmp/ssh-auth.sock'
execa.sync('ssh-agent', ['-a', authSock])
core.exportVariable('SSH_AUTH_SOCK', authSock)
}
const privateKey = core.getInput('private-key').replace('/\r/g', '').trim() + '\n'
execa.sync('ssh-add', ['-'], {input: privateKey})
const knownHosts = core.getInput('known-hosts')
if (knownHosts === '') {
fs.appendFileSync(`${ssh}/config`, `StrictHostKeyChecking no`)
} else {
fs.appendFileSync(`${ssh}/known_hosts`, knownHosts)
fs.chmodSync(`${ssh}/known_hosts`, '644')
}
}
/**
* @return {Promise<string>}
*/
async function getDeployer() {
if (core.getInput('bin-path') && core.getInput('version')) {
throw new Error('Incompatible arguments "bin-path" and "version" detected, please use only of them.')
}
if (core.getInput('bin-path')) {
return core.getInput('bin-path')
}
const version = core.getInput('version')
if (version) {
return await downloadDeployer(version);
}
for (let c of ['./vendor/bin/dep', 'bin/dep', './deployer.phar']) {
if (fs.existsSync(c)) {
return c
}
}
try {
if (execa.commandSync('which deployer').exitCode === 0) {
return 'deployer'
}
if (execa.commandSync('which dep').exitCode === 0) {
return 'dep'
}
} catch (error) {}
throw new Error('Deployer bin not found. To fix it, please specify `bin-path` or `version` options.')
}
/**
* @param {string} version
* @return {Promise<string>} Path to deployer
*/
async function downloadDeployer(version) {
version = version.replace(/^v/, '')
/** @see https://deployer.org/download */
const url = version.startsWith('6')
? `https://deployer.org/releases/v${version}/deployer.phar`
: `/~https://github.com/deployphp/deployer/releases/download/v${version}/deployer.phar`
core.info(`Downloading Deployer v${version} from ${url}`)
await execa(`curl -LO ${url}`, [], {shell: 'bash', stdio: 'inherit'})
await execa(`chmod +x ./deployer.phar`, [], {shell: 'bash', stdio: 'inherit'})
return './deployer.phar'
}
/**
* @param {string} deployer
* @return {Promise<void>}
*/
async function runDeployer(deployer) {
await execa(deployer, ['-V'])
const depArgs = split(core.getInput('task')).concat(['--ansi', '--no-interaction'])
const process = await execa(deployer, depArgs, {stdio: 'inherit'})
if (process.exitCode !== 0) {
throw new Error(`Deployer exit with error. Exit code: ${process.exitCode}`)
}
}
main().catch(core.setFailed)