0

Trying to use the Jenkins ssh agent plugin in pipeline to copy war file to tomcat ( both running on Ubuntu 18.04 on Ec2).

Using standard sample code and using key based authentication. Tomcat is up and running.

stage('Deploy to Tomcat'){
        sshagent(['Tomcat-cred']) {
         sh 'scp -o StrictHostKeyChecking=no target/*.war ubuntu@xxxx:/opt/tomcat/latest/webapps/'
      }
   }

Get Error: scp: /opt/tomcat/latest/webapps//myweb-0.0.7-SNAPSHOT.war: Permission denied

Had setup the tomcat user with permissions to 'latest' (symlink)

Here are my file permissions:

drwxrwxrwx 7 tomcat tomcat  4096 Sep 16 15:52 webapps.

I tried now with some changes:

scp: /opt/tomcat/latest/webapps/: No such file or directory

scp: /opt/tomcat/apache-tomcat-9.0.26/webapps/: No such file or directory

The directory of course exists:

    ubuntu@ip-xxxxx:/opt/tomcat/apache-tomcat-9.0.26$ ls

  webapps
Sam-T
  • 1,877
  • 6
  • 23
  • 51
  • Does tomcat user really have the premission to write on ubuntu@xxxx:/opt/tomcat/latest/webapps/? How about try directly from command line. Maybe it is not a problem with sshAgent.. – Jiho Oct 04 '19 at 22:49
  • I use the 'ubuntu' user when I ssh with Jenkins - but that user also should have permissions. Added some more details in the post above. Now the error is different - "No such file" – Sam-T Oct 05 '19 at 01:27
  • I tried to search in google with 'scp no such file or directory but file exists', and there were several solutions, but I'm not sure which one would be solve your problem.. Maybe you can try some of those solutions. – Jiho Oct 07 '19 at 02:11

3 Answers3

1

This problem turned out to be a comedy of errors. Sorry.

The issue is with IP addresses that keep changing on AWS- after you stop and restart (normal behavior). I kept messing up the IP in Jenkins config and Jenkinsfile- both Tomcat server and Jenkins server IP, somehow one or the other would be wrong(hard to keep up). But I kept checking with the terminal - on terminal you of of course need to to have correct IP, making it hard to debug.

Oh well, sometimes simple things can also get messed up

Sam-T
  • 1,877
  • 6
  • 23
  • 51
  • It is good to know that the problem has been solved! I'm glad you found out the reason and share it to here:D – Jiho Oct 08 '19 at 00:40
1

changing ownership for webapps in Tomcat_server can avoid this error

chown -R ec2-user:ec2-user  /opt/apache-tomcat-9.0.29/webapps
Dharman
  • 30,962
  • 25
  • 85
  • 135
govardhan
  • 11
  • 2
0

I think out some solutions..

1.Send file to another directory, then copy again to target directory

stage('Deploy to Tomcat'){
      sshagent(['Tomcat-cred']) {
         sh """
           scp -o StrictHostKeyChecking=no target/*.war ubuntu@xxxx:/home/ubuntu
           ssh -o StrictHostKeyChecking=no ubuntu@xxxx 'cp -r /home/ubuntu/*.war /opt/tomcat/latest/webapps/'
         """
      }
   }

2.Not just use sshagent plugin

stage('Deploy to Tomcat'){
      script {
         // you may need to set ssh keys on target server
         sh 'scp -o StrictHostKeyChecking=no target/*.war ubuntu@xxxx:/opt/tomcat/latest/webapps/'
      }
   }

I hope you solve this problem..

Jiho
  • 338
  • 1
  • 6
  • 14