forked from runs-on/action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (59 loc) · 2.2 KB
/
index.js
File metadata and controls
65 lines (59 loc) · 2.2 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
const childProcess = require('child_process')
const os = require('os')
const process = require('process')
const path = require('path')
const ARGS = ''.split(',').filter(arg => arg !== '')
const WINDOWS = 'win32'
const LINUX = 'linux'
const AMD64 = 'x64'
const ARM64 = 'arm64'
function chooseBinary() {
const platform = os.platform()
const arch = os.arch()
if (platform === LINUX && arch === AMD64) {
return `main-linux-amd64`
}
if (platform === LINUX && arch === ARM64) {
return `main-linux-arm64`
}
if (platform === WINDOWS && arch === AMD64) {
return `main-windows-amd64.exe`
}
console.error(`Unsupported platform (${platform}) and architecture (${arch})`)
process.exit(0)
}
function main() {
// Skip all operations if not running on RunsOn runners
if (!process.env.RUNS_ON_RUNNER_NAME || process.env.RUNS_ON_RUNNER_NAME === '') {
console.log('This action is only meant to be run on RunsOn (https://runs-on.com) runners, skipping all operations')
process.exit(0)
}
const binary = chooseBinary()
const mainScript = path.join(__dirname, binary)
if (os.platform() === WINDOWS) {
console.log(`Starting ${mainScript} with arguments ${ARGS.join(' ')}`, ARGS.length)
// runner user has elevated privileges, so we can just run the script directly
childProcess.execFileSync(mainScript, ARGS, { stdio: 'inherit' })
} else {
try {
childProcess.execFileSync('sudo', ['-n', '-E', mainScript, ...ARGS], { stdio: 'inherit' })
} catch (error) {
try {
const whoami = childProcess.execSync('whoami').toString().trim()
console.log(`Current user (whoami): ${whoami}`)
} catch (error) {
console.log('Could not determine user via whoami')
}
if (error.code === 'ENOENT') {
// sudo not available (likely in container, which is already running as root), try running directly
childProcess.execFileSync(mainScript, ARGS, { stdio: 'inherit' })
} else {
throw error
}
}
}
process.exit(0)
}
if (require.main === module) {
main()
}