Run shell command in jenkins as root user?

Raj Gupta picture Raj Gupta · Apr 28, 2015 · Viewed 77.6k times · Source

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.

Answer

oden picture oden · Aug 4, 2015

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