-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathcmdline.ts
94 lines (80 loc) · 3.24 KB
/
cmdline.ts
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
import fs = require('fs');
import path = require('path');
import tl = require('azure-pipelines-task-lib/task');
import tr = require('azure-pipelines-task-lib/toolrunner');
import { v4 as uuidV4 } from 'uuid';
async function run() {
try {
tl.setResourcePath(path.join(__dirname, 'task.json'));
// Get inputs.
let failOnStderr = tl.getBoolInput('failOnStderr', false);
let script: string = tl.getInput('script', false) || '';
let workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);
if (fs.existsSync(script)) {
script = `exec ${script}`;
}
// Write the script to disk.
console.log(tl.loc('GeneratingScript'));
tl.assertAgent('2.115.0');
let tempDirectory = tl.getVariable('agent.tempDirectory');
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
let filePath = path.join(tempDirectory, uuidV4() + '.sh');
fs.writeFileSync(
filePath,
script, // Don't add a BOM. It causes the script to fail on some operating systems (e.g. on Ubuntu 14).
{ encoding: 'utf8' });
// Print one-liner scripts.
if (script.indexOf('\n') < 0 && script.toUpperCase().indexOf('##VSO[') < 0) {
console.log(tl.loc('ScriptContents'));
console.log(script);
}
// Create the tool runner.
console.log('========================== Starting Command Output ===========================');
let bash = tl.tool(tl.which('bash', true))
.arg('--noprofile')
.arg(`--norc`)
.arg(filePath);
let options: tr.IExecOptions = {
cwd: workingDirectory,
failOnStdErr: false,
errStream: process.stdout, // Direct all output to STDOUT, otherwise the output may appear out
outStream: process.stdout, // of order since Node buffers it's own STDOUT but not STDERR.
ignoreReturnCode: true
};
// Listen for stderr.
let stderrFailure = false;
const aggregatedStderr: string[] = [];
if (failOnStderr) {
bash.on('stderr', (data: Buffer) => {
stderrFailure = true;
aggregatedStderr.push(data.toString('utf8'));
});
}
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGTERM', 'EXIT'].forEach((signal) => {
process.on(signal, () => {
bash.killChildProcess(signal as NodeJS.Signals);
});
});
// Run bash.
let exitCode: number = await bash.exec(options);
let result = tl.TaskResult.Succeeded;
// Fail on exit code.
if (exitCode !== 0) {
tl.error(tl.loc('JS_ExitCode', exitCode));
result = tl.TaskResult.Failed;
}
// Fail on stderr.
if (stderrFailure) {
tl.error(tl.loc('JS_Stderr'));
aggregatedStderr.forEach((err: string) => {
tl.error(err, tl.IssueSource.CustomerScript);
});
result = tl.TaskResult.Failed;
}
tl.setResult(result, null, true);
}
catch (err) {
tl.setResult(tl.TaskResult.Failed, err.message || 'run() failed', true);
}
}
run();