Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins2 Pipeline: deploy on tomcat

I'm a newbie on Jenkins Pipelines. I wrote this small Groovy script from pulling from git and compiling.

node('master') {
  def workspace = pwd()

  stage 'Git pull'
  git branch: 'develop', 
  credentialsId: 'Cred_xxxx', 
  url: 'https://xxxx/yyyy.git'

  stage 'Builing'
  def mvnHome = tool name: 'Maven3', type: 'hudson.tasks.Maven$MavenInstallation'
  sh "cd ${workspace}/tlt/; ${mvnHome}/bin/mvn install -Pdevelopment"
  sh "cd ${workspace}"
}

Now I would like to deploy on Tomcat7 the file tlt/target/tlt.war. The previous approach was to fill the "Deploy war/ear to container" plugin. Now, I have no idea how to do this with Groovy.

Thanks
Riccardo

like image 247
Riccardo79 Avatar asked Jun 13 '16 10:06

Riccardo79


2 Answers

When using Jenkinsfile you need to copy the war yourself to the tomcat using groovy script.

If Tomcat is on the same server you can do just:

sh 'cp tlt/target/tlt.war TOMCAT_DIRECTORY/webapps/'

If on other host you would need to do scp and configure user and password:

sshagent(['CREDENTIALS_ID']) {
    sh 'scp tlt/target/tlt.war some-remote-host:/LOCATION/TOMCAT/webapps/'
}

where CREDENTIALS_ID is the ID take from the URL of credentials that you need to add in jenkins credentials page (it is in the url, a string like 4644a37d-4291-474e-813b-14b58bef1125).

like image 199
Krzysztof Krasoń Avatar answered Nov 20 '22 10:11

Krzysztof Krasoń


Another solution, more easy is to create a Job with only the task "Deploy war/ear to container" and to call it through the "build" command in Groovy.

like image 36
Riccardo79 Avatar answered Nov 20 '22 09:11

Riccardo79