Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run shell command in jenkins as root user?

I have recently started using Jenkins for integration. All was well until I was running jobs on master node without shell command but I have to run jobs on master as well as slave node which contains shell commands to. I am not able to run those shell commands as root user. I have tried

  1. Using SSH Keys.
  2. Setting user name in shell commands.
  3. Using sudo.

I am getting permission denied error every time I use any of the above methods.

like image 671
Raj Gupta Avatar asked Apr 28 '15 18:04

Raj Gupta


1 Answers

I would suggest against running the jenkins user as root. This could expose the operating system and all of the repo's which jenkins can build.

Running any script as root is a security risk, but a slightly safer method would be to grant the jenkins user sudo access to only run the one script, without needing a password.

sudo visudo

and add the following:

jenkins    ALL = NOPASSWD: /var/lib/jenkins/jobs/[job name]/workspace/script

Double check your path via the console log of a failed build script. The one shown here is the default.

Now within the jenkins task you can call sudo $WORKSPACE/your script

like image 199
oden Avatar answered Sep 21 '22 03:09

oden