Skip to content

Commit 7e7f2de

Browse files
committed
Updated to check if sudo command is available
1 parent 222b3da commit 7e7f2de

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"@actions/cache": "^3.2.2",
2222
"@actions/core": "^1.10.0",
2323
"@actions/exec": "^1.1.0",
24-
"@actions/io": "^1.1.2",
24+
"@actions/io": "^1.1.3",
2525
"@actions/tool-cache": "^1.7.1"
2626
},
2727
"devDependencies": {

src/script.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// Copyright 2020-2022 The MathWorks, Inc.
1+
// Copyright 2020-2023 The MathWorks, Inc.
22

33
import * as exec from "@actions/exec";
4+
import * as io from "@actions/io";
45
import * as tc from "@actions/tool-cache";
56
import path from "path";
67

@@ -13,7 +14,7 @@ import path from "path";
1314
*/
1415
export async function downloadAndRunScript(platform: string, url: string, args?: string[]) {
1516
const scriptPath = await tc.downloadTool(url);
16-
const cmd = generateExecCommand(platform, scriptPath);
17+
const cmd = await generateExecCommand(platform, scriptPath);
1718

1819
const exitCode = await exec.exec(cmd, args);
1920

@@ -29,12 +30,17 @@ export async function downloadAndRunScript(platform: string, url: string, args?:
2930
* @param platform Operating system of the runner (e.g. "win32" or "linux").
3031
* @param scriptPath Path to the script (on runner's filesystem).
3132
*/
32-
export function generateExecCommand(platform: string, scriptPath: string): string {
33+
export async function generateExecCommand(platform: string, scriptPath: string): Promise<string> {
3334
// Run the install script using bash
3435
let installCmd = `bash ${scriptPath}`;
3536

3637
if (platform !== "win32") {
37-
installCmd = `sudo -E ${installCmd}`;
38+
try {
39+
await io.which("sudo", true);
40+
installCmd = `sudo -E ${installCmd}`;
41+
} catch {
42+
// Sudo not available, do not prepend
43+
}
3844
}
3945

4046
return installCmd;
@@ -48,4 +54,4 @@ export function defaultInstallRoot(platform: string, programName: string): strin
4854
installRoot = path.join("/","opt", programName);
4955
}
5056
return installRoot;
51-
}
57+
}

0 commit comments

Comments
 (0)