2

I’ve deployed the latest Jenkins WAR to a Tomcat 6 server on this flavor of Linux …

[daveaip-30-888-22-999 ~]$ uname -a
Linux ip-30-888-22-999 4.1.7-99.99.amzn1.x86_64 #1 SMP Mon Sep 14 23:20:33 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

I have a job that builds a Gradle project. After the build has succeeded, I want to copy the newly-produced WAR file to the Tomcat 6 directory. I have this shell script as an additional build step …

cp ./build/libs/myproject.war /usr/share/tomcat6/webapps

The Tomcat6 webs directory has these permissions ….

[daveaip-30-888-22-999 ~]$ ls -al /var/lib/tomcat6/webapps
total 77876
drwxrwxr-x  4 tomcat tomcat     4096 Oct 23 19:52 .

But the command fails with this error …

+ cp ./build/libs/myproject.war /usr/share/tomcat6/webapps
cp: cannot create regular file ‘/usr/share/tomcat6/webapps/myproject.war’: Permission denied

What do I need to do to give Jenkins permissions to copy this file over? I don’t want to change the perms of the directory to 777.

Edit: In response to the answer given, I added a "whoami" into the build script ...

whoami
cp ./build/libs/myproject.war $CATALINA_HOME/webapps

And the output was

+ whoami
tomcat
+ cp ./build/libs/myproject.war /usr/share/tomcat6/webapps

So I am already running under the user that is the owner of the directory.

Dave
  • 15,639
  • 133
  • 442
  • 830

1 Answers1

2
  1. See which user running cp ./build/libs/myproject.war /usr/share/tomcat6/webapps command then, you can give permission to that user.

  2. Run this command in build -> Shell Script section of your job's configure.

    $ whoami    // show the user, say 'xyz'
    

    then, run this command locally from teminal to change the ownership of the directory.

    $ sudo chown -R xyz: /var/lib/tomcat6/webapps  // give permission to that user
    
  3. If two or more users needs permission to that directory, then you can create a group with users, then just give the permission to that group. see this & this

Community
  • 1
  • 1
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73
  • 1
    Hey I added the "whoami" into the statement but the user that it output is alreday the user who owns the directory I'm tryihgn to copy to. – Dave Oct 27 '15 at 13:26
  • Can you try `cp -f ./build/libs/myproject.war /usr/share/tomcat6/webapps`? and also restart the server. – Sajib Khan Oct 28 '15 at 04:32
  • see the permission of `./build/libs/myproject.war`. You can try if copy works from your jenkins terminal. – Sajib Khan Oct 28 '15 at 04:42
  • Yup, the file ownership of ./build/libs/myproject.war was the problem. Changing that allowed everything to work. – Dave Oct 29 '15 at 14:15