Skip to content

Commit 9a22e4c

Browse files
authored
fix: Preserve environment variables when running Grype (#202)
1 parent 3578976 commit 9a22e4c

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

dist/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exports.GRYPE_VERSION = "v0.52.0";
1414

1515
const cache = __nccwpck_require__(7784);
1616
const core = __nccwpck_require__(2186);
17-
const { exec } = __nccwpck_require__(1514);
17+
const exec = __nccwpck_require__(1514);
1818
const fs = __nccwpck_require__(7147);
1919
const stream = __nccwpck_require__(2781);
2020
const { GRYPE_VERSION } = __nccwpck_require__(6244);
@@ -31,10 +31,10 @@ async function downloadGrype(version) {
3131
// Download the installer, and run
3232
const installPath = await cache.downloadTool(url);
3333
// Make sure the tool's executable bit is set
34-
await exec(`chmod +x ${installPath}`);
34+
await exec.exec(`chmod +x ${installPath}`);
3535

3636
let cmd = `${installPath} -b ${installPath}_grype ${version}`;
37-
await exec(cmd);
37+
await exec.exec(cmd);
3838
let grypePath = `${installPath}_grype/grype`;
3939

4040
// Cache the downloaded file
@@ -121,6 +121,7 @@ async function runScan({ source, failBuild, severityCutoff, outputFormat }) {
121121
const out = {};
122122

123123
const env = {
124+
...process.env,
124125
GRYPE_CHECK_FOR_APP_UPDATE: "false",
125126
};
126127

@@ -203,7 +204,7 @@ async function runScan({ source, failBuild, severityCutoff, outputFormat }) {
203204
const exitCode = await core.group(`${cmd} output...`, async () => {
204205
core.info(`Executing: ${cmd} ` + cmdArgs.join(" "));
205206

206-
return exec(cmd, cmdArgs, {
207+
return exec.exec(cmd, cmdArgs, {
207208
env,
208209
ignoreReturnCode: true,
209210
outStream,

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const cache = require("@actions/tool-cache");
22
const core = require("@actions/core");
3-
const { exec } = require("@actions/exec");
3+
const exec = require("@actions/exec");
44
const fs = require("fs");
55
const stream = require("stream");
66
const { GRYPE_VERSION } = require("./GrypeVersion");
@@ -17,10 +17,10 @@ async function downloadGrype(version) {
1717
// Download the installer, and run
1818
const installPath = await cache.downloadTool(url);
1919
// Make sure the tool's executable bit is set
20-
await exec(`chmod +x ${installPath}`);
20+
await exec.exec(`chmod +x ${installPath}`);
2121

2222
let cmd = `${installPath} -b ${installPath}_grype ${version}`;
23-
await exec(cmd);
23+
await exec.exec(cmd);
2424
let grypePath = `${installPath}_grype/grype`;
2525

2626
// Cache the downloaded file
@@ -107,6 +107,7 @@ async function runScan({ source, failBuild, severityCutoff, outputFormat }) {
107107
const out = {};
108108

109109
const env = {
110+
...process.env,
110111
GRYPE_CHECK_FOR_APP_UPDATE: "false",
111112
};
112113

@@ -189,7 +190,7 @@ async function runScan({ source, failBuild, severityCutoff, outputFormat }) {
189190
const exitCode = await core.group(`${cmd} output...`, async () => {
190191
core.info(`Executing: ${cmd} ` + cmdArgs.join(" "));
191192

192-
return exec(cmd, cmdArgs, {
193+
return exec.exec(cmd, cmdArgs, {
193194
env,
194195
ignoreReturnCode: true,
195196
outStream,

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
},
1010
"scripts": {
1111
"lint": "eslint index.js",
12-
"test": "eslint index.js && npm run download-pinned-grype-db && GRYPE_DB_AUTO_UPDATE=false GRYPE_DB_CACHE_DIR=./grype-db jest",
13-
"test:update-snapshots": "eslint index.js && npm run download-pinned-grype-db && GRYPE_DB_AUTO_UPDATE=false GRYPE_DB_CACHE_DIR=./grype-db jest --updateSnapshot",
14-
"download-pinned-grype-db": "mkdir -p grype-db/3 && curl -sL https://toolbox-data.anchore.io/grype/databases/vulnerability-db_v3_2022-03-16T08:14:11Z.tar.gz | tar zxf - -C grype-db/3",
12+
"test": "eslint index.js && npm run download-pinned-grype-db && GRYPE_DB_AUTO_UPDATE=false GRYPE_DB_CACHE_DIR=./grype-db GRYPE_DB_VALIDATE_AGE=false jest",
13+
"test:update-snapshots": "eslint index.js && npm run download-pinned-grype-db && GRYPE_DB_AUTO_UPDATE=false GRYPE_DB_CACHE_DIR=./grype-db GRYPE_DB_VALIDATE_AGE=false jest --updateSnapshot",
14+
"download-pinned-grype-db": "mkdir -p grype-db/5 && curl -sL https://toolbox-data.anchore.io/grype/databases/vulnerability-db_v5_2022-10-17T08:14:57Z_b50a86ce07d268101316.tar.gz | tar zxf - -C grype-db/5",
1515
"build": "ncc build ./index.js",
1616
"precommit": "pretty-quick --staged && npm run build && git add dist/",
1717
"prepare": "husky install",

tests/action_args.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { run } = require("../index");
22
const core = require("@actions/core");
3+
const exec = require("@actions/exec");
34

45
jest.setTimeout(30000);
56

@@ -116,4 +117,44 @@ describe("Github action args", () => {
116117
spyOutput.mockRestore();
117118
spyStdout.mockRestore();
118119
});
120+
121+
it("runs with environment variables", async () => {
122+
const inputs = {
123+
path: "tests/fixtures/npm-project",
124+
};
125+
const spyInput = jest.spyOn(core, "getInput").mockImplementation((name) => {
126+
try {
127+
return inputs[name];
128+
} finally {
129+
inputs[name] = true;
130+
}
131+
});
132+
process.env.BOGUS_ENVIRONMENT_VARIABLE = "bogus";
133+
134+
try {
135+
var call = {}; // commandLine, args, options
136+
const baseExec = exec.exec;
137+
const spyExec = jest
138+
.spyOn(exec, "exec")
139+
.mockImplementation((commandLine, args, options) => {
140+
call = {
141+
commandLine,
142+
args,
143+
options,
144+
};
145+
return baseExec(commandLine, args, options);
146+
});
147+
148+
await run();
149+
150+
expect(call.options).toBeDefined();
151+
expect(call.options.env.BOGUS_ENVIRONMENT_VARIABLE).toEqual("bogus");
152+
153+
spyExec.mockRestore();
154+
} finally {
155+
delete process.env.BOGUS_ENVIRONMENT_VARIABLE;
156+
}
157+
158+
spyInput.mockRestore();
159+
});
119160
});

0 commit comments

Comments
 (0)