forked from JarvusInnovations/background-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-run.js
54 lines (45 loc) · 1.85 KB
/
post-run.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
const core = require('@actions/core')
const inputs = require('./input')
const fs = require('fs')
const path = require('path')
const { logOutput, logOutputResume, logOutputIf, workingDirectory } = inputs
const pid = core.getState('post-run')
const reason = core.getState(`reason_${pid}`)
const stdout = parseInt(core.getState('stdout') || 0, 10)
const stderr = parseInt(core.getState('stderr') || 0, 10)
const cwd = workingDirectory || process.env.GITHUB_WORKSPACE || './'
const stdoutPath = path.join(cwd, `${pid}.out`)
const stderrPath = path.join(cwd, `${pid}.err`)
const shouldLog = logOutputIf === 'true' || logOutputIf === reason || (logOutputIf === 'failure' && (reason === 'exit-early' || reason === 'timeout'))
core.debug({ reason, pid, stderrPath, stdoutPath, stdout, stderr, inputs, shouldLog })
if (shouldLog) {
streamLogs()
} else {
process.exit(0)
}
function streamLog(path, start) {
return new Promise((resolve, reject) => {
const log = fs.createReadStream(path, { start, emitClose: true, encoding: 'utf8', autoClose: true })
log.on('close', () => resolve(null))
log.on('error', (err) => reject(err))
log.pipe(process.stdout)
})
}
async function streamLogs() {
if (logOutput.stdout) {
const start = logOutputResume.stdout ? stdout : 0
const truncated = start > 0
await core.group(`${logOutputResume.stdout ? 'Truncated ' : ''}Output:`, async () => {
if (truncated) console.log(`Truncated ${start} bytes of tailed stdout output`)
await streamLog(stdoutPath, start)
})
}
if (logOutput.stderr) {
const start = logOutputResume.stderr ? stderr : 0
const truncated = start > 0
await core.group(`${logOutputResume.stderr ? 'Truncated ' : ''}Error Output:`, async () => {
if (truncated) console.log(`Truncated ${start} bytes of tailed stderr output`)
await streamLog(stderrPath, start)
})
}
}