-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathaction.js
More file actions
116 lines (99 loc) · 2.93 KB
/
action.js
File metadata and controls
116 lines (99 loc) · 2.93 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
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
106
107
108
109
110
111
112
113
114
115
116
const { execSync, spawnSync } = require("child_process");
const core = require("@actions/core");
const RUN_OPTIONS_DEFAULTS = { dir: null, ignoreErrors: false, prefix: "" };
const EXECUTE_OPTIONS_DEFAULTS = { dir: null, ignoreErrors: false };
/**
* Returns the value for an environment variable. If the variable is required but doesn't have a
* value, an error is thrown
* @param {string} name - Name of the environment variable
* @param {boolean} required - Whether an error should be thrown if the variable doesn't have a
* value
* @returns {string | null} - Value of the environment variable
*/
function getEnv(name, required = false) {
const nameUppercase = name.toUpperCase();
const value = process.env[nameUppercase];
if (value == null) {
// Value is either not set (`undefined`) or set to `null`
if (required) {
throw new Error(`Environment variable "${nameUppercase}" is not defined`);
}
return null;
}
return value;
}
/**
* Executes the provided shell command
* @param {string} cmd - Shell command to execute
* @param {{dir: string, ignoreErrors: boolean}} [options] - {@see RUN_OPTIONS_DEFAULTS}
* @returns {{status: number, stdout: string, stderr: string}} - Output of the shell command
*/
function run(cmd, options) {
const optionsWithDefaults = {
...RUN_OPTIONS_DEFAULTS,
...options,
};
core.debug(cmd);
try {
const stdout = execSync(cmd, {
encoding: "utf8",
cwd: optionsWithDefaults.dir,
maxBuffer: 20 * 1024 * 1024,
});
const output = {
status: 0,
stdout: stdout.trim(),
stderr: "",
};
core.debug(`Stdout: ${output.stdout}`);
return output;
} catch (err) {
if (optionsWithDefaults.ignoreErrors) {
const output = {
status: err.status,
stdout: err.stdout.trim(),
stderr: err.stderr.trim(),
};
core.debug(`Exit code: ${output.status}`);
core.debug(`Stdout: ${output.stdout}`);
core.debug(`Stderr: ${output.stderr}`);
return output;
}
throw err;
}
}
/**
* Executes the provided binary with the given arguments.
* @param {string} command - binary to execute
* @param {{dir: string, ignoreErrors: boolean}} [options] - {@see EXECUTE_OPTIONS_DEFAULTS}
* @returns {{status: number, stdout: string, stderr: string}} - Output of the command
*/
function execute(command, args, options) {
const optionsWithDefaults = {
...EXECUTE_OPTIONS_DEFAULTS,
...options,
};
core.debug(`${command} ${args.filter(e => e).join(" ")}`);
const process = spawnSync(command, args.filter(e => e), {
cwd: optionsWithDefaults.dir,
encoding: "utf-8",
maxBuffer: 20 * 1024 * 1024,
});
const output = {
status: process.status,
stdout: process.stdout.trim(),
stderr: process.stderr.trim(),
};
core.debug(`Exit code: ${output.status}`);
core.debug(`Stdout: ${output.stdout}`);
core.debug(`Stderr: ${output.stderr}`);
if (!optionsWithDefaults.ignoreErrors && child.status != 0) {
throw child.error;
}
return output;
}
module.exports = {
execute,
getEnv,
run,
};