1- // Copyright 2020-2022 The MathWorks, Inc.
1+ // Copyright 2020-2023 The MathWorks, Inc.
22
33import * as exec from "@actions/exec" ;
4+ import * as io from "@actions/io" ;
45import * as tc from "@actions/tool-cache" ;
56import path from "path" ;
67
@@ -13,7 +14,7 @@ import path from "path";
1314 */
1415export 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