@@ -11,7 +11,7 @@ import {
1111 isFedoraBased ,
1212 getPackageManagerCommand ,
1313} from './os_arch' ;
14- import { debugLog } from './utils' ;
14+ import { debugLog , hasRootPrivileges } from './utils' ;
1515import {
1616 getCudaLocalInstallerUrl ,
1717 findCudaRepoAndPackageLinux ,
@@ -20,14 +20,23 @@ import {
2020import * as tc from '@actions/tool-cache' ;
2121import * as io from '@actions/io' ;
2222
23+ /**
24+ * Get sudo prefix for command execution
25+ * @returns 'sudo' if root privileges are not present, empty string otherwise
26+ */
27+ function getSudoPrefix ( ) : string {
28+ return hasRootPrivileges ( ) ? '' : 'sudo' ;
29+ }
30+
2331/**
2432 * Install CUDA on Linux
2533 * @param installerPath - Path to the CUDA installer (.run file)
2634 */
2735async function installCudaLinuxLocal ( installerPath : string ) : Promise < void > {
2836 // https://docs.nvidia.com/cuda/cuda-installation-guide-linux/#runfile-installation
2937 core . info ( 'Installing CUDA on Linux...' ) ;
30- const command = `sudo sh ${ installerPath } ` ;
38+ const sudoPrefix = getSudoPrefix ( ) ;
39+ const command = `${ sudoPrefix } sh ${ installerPath } ` . trim ( ) ;
3140
3241 // Install CUDA toolkit only (without driver)
3342 // --silent: Run installer in silent mode
@@ -140,6 +149,7 @@ async function installCudaLinuxNetwork(
140149 const repoUrl = cudaRepoAndPackage . repoUrl ;
141150 const packageName = cudaRepoAndPackage . packageName ;
142151
152+ const sudoPrefix = getSudoPrefix ( ) ;
143153 let cudaPath : string | undefined = undefined ;
144154 try {
145155 if ( isDebianBased ( osInfo ) ) {
@@ -153,22 +163,26 @@ async function installCudaLinuxNetwork(
153163 repoFilePath = path . resolve ( repoFilePath ) ;
154164
155165 if ( repoUrl . endsWith ( '.deb' ) ) {
156- await exec . exec ( `sudo dpkg -i ${ repoFilePath } ` ) ;
157- await exec . exec ( `sudo apt-get update` ) ;
166+ await exec . exec ( `${ sudoPrefix } dpkg -i ${ repoFilePath } ` . trim ( ) ) ;
167+ await exec . exec ( `${ sudoPrefix } apt-get update` . trim ( ) ) ;
158168 } else if ( repoUrl . endsWith ( '.pin' ) ) {
159- await exec . exec ( `sudo mv ${ repoFilePath } /etc/apt/preferences.d/cuda-repository-pin-600` ) ;
169+ await exec . exec (
170+ `${ sudoPrefix } mv ${ repoFilePath } /etc/apt/preferences.d/cuda-repository-pin-600` . trim ( )
171+ ) ;
160172 const repoRootUrl = repoUrl . replace ( / \/ [ \w . - ] + \. p i n $ / , '' ) ;
161- await exec . exec ( `sudo add-apt-repository "deb ${ repoRootUrl } /"` ) ;
162- await exec . exec ( `sudo apt-get update` ) ;
173+ await exec . exec ( `${ sudoPrefix } add-apt-repository "deb ${ repoRootUrl } /"` . trim ( ) ) ;
174+ await exec . exec ( `${ sudoPrefix } apt-get update` . trim ( ) ) ;
163175 }
164176 // Install CUDA toolkit
165- await exec . exec ( `sudo apt-get install -y ${ packageName } ` ) ;
177+ await exec . exec ( `${ sudoPrefix } apt-get install -y ${ packageName } ` . trim ( ) ) ;
166178 cudaPath = '/usr/local/cuda' ;
167179 } else if ( isFedoraBased ( osInfo ) ) {
168180 const packageManagerCommand = await getPackageManagerCommand ( osInfo ) ;
169- await exec . exec ( `sudo ${ packageManagerCommand } config-manager --add-repo ${ repoUrl } ` ) ;
170- await exec . exec ( `sudo ${ packageManagerCommand } clean all` ) ;
171- await exec . exec ( `sudo ${ packageManagerCommand } install -y ${ packageName } ` ) ;
181+ await exec . exec (
182+ `${ sudoPrefix } ${ packageManagerCommand } config-manager --add-repo ${ repoUrl } ` . trim ( )
183+ ) ;
184+ await exec . exec ( `${ sudoPrefix } ${ packageManagerCommand } clean all` . trim ( ) ) ;
185+ await exec . exec ( `${ sudoPrefix } ${ packageManagerCommand } install -y ${ packageName } ` . trim ( ) ) ;
172186 cudaPath = '/usr/local/cuda' ;
173187 }
174188 } catch ( error ) {
0 commit comments