-
Notifications
You must be signed in to change notification settings - Fork 10
/
post-run.js
81 lines (72 loc) · 2.58 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
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
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'))
if (core.isDebug()) {
core.debug(`stdout: ${stdout}`)
core.debug(`stderr: ${stderr}`)
core.debug(`stdoutPath: ${stdoutPath}`)
core.debug(`stderrPath: ${stderrPath}`)
core.debug(`shouldLog: ${shouldLog}`)
core.debug(`logOutput: ${logOutput}`)
core.debug(`logOutputResume: ${logOutputResume}`)
core.debug(`logOutputIf: ${logOutputIf}`)
core.debug(`workingDirectory: ${workingDirectory}`)
core.debug(`pid: ${pid}`)
core.debug(`reason: ${reason}`)
core.debug(`cwd: ${cwd}`)
}
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`)
try {
await streamLog(stdoutPath, start)
} catch(err) {
console.error('Error streaming stdout:', err)
}
})
}
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`)
try {
await streamLog(stderrPath, start)
} catch(err) {
console.error('Error streaming stderr:', err)
}
})
}
}
(async() => {
try {
if (shouldLog) {
await streamLogs()
}
} catch(err) {
console.error('Error streaming logs:', err)
} finally {
process.exit(0)
}
})();