How to deploy SpringBoot Maven application with Jenkins ?

ぐ巨炮叔叔 提交于 2019-11-27 20:42:53

问题


I have a Spring Boot application which runs on embedded Tomcat servlet container mvn spring-boot:run . And I don’t want to deploy the project as separate war to standalone Tomcat.

Whenever I push code to BitBucket/Github, a hook runs and triggers Jenkins job (runs on Amazon EC2) to deploy the application.

The Jenkins job has a post build action: mvn spring-boot:run, the problem is that the job hangs when post build action finished.

There should be another way to do this. Any help would be appreciated.


回答1:


The problem is that Jenkins doesn't handle spawning child process from builds very well. Workaround suggested by @Steve in the comment (nohuping) didn't change the behaviour in my case, but a simple workaround was to schedule app's start by using the at unix command:

> echo "mvn spring-boot:run" | at now + 1 minutes

This way Jenkins successfully completes the job without timing out.


If you end up running your application from a .jar file via java -jar app.jar be aware that Boot breaks if the .jar file is overwritten, you'll need to make sure the application is stopped before copying the artifact. If you're using ApplicationPidListener you can verify that the application is running (and stop it if it is) by adding execution of this command:

> test -f application.pid && xargs kill < application.pid || echo 'App was not running, nothing to stop'



回答2:


I find very useful to first copy the artifacts to a specified area on the server to keep track of the deployed artifacts and not to start the app from the jenkins job folder. Then create a server log file there and start to listening to it on the jenkins window until the server started.

To do that I developed a small shell script that you can find here

You will also find a small article explaining how to configure the project on jenkins.

Please let me know if worked for you. Thnaks




回答3:


I assume you have a Jenkins-user on the server and this user is the owner of the Jenkins-service:

  1. log in on the server as root.
  2. run sudo visudo
  3. add "jenkins ALL=(ALL) NOPASSWD:ALL" at the end (jenkins=your Jenkins-user)
  4. Sign In in Jenkins and choose your jobs and click to configure
  5. Choose "Execute Shell" in the "Post build step"
  6. Copy and paste this:
   service=myapp
   if ps ax | grep -v grep | grep -v $0 | grep $service > /dev/null
   then
       sudo service myapp stop
       sudo unlink /etc/init.d/myapp
       sudo chmod +x /path/to/your/myapp.jar
       sudo ln -s /path/to/your/myapp.jar /etc/init.d/myapp
       sudo service myapp start 
    else
       sudo chmod +x  /path/to/your/myapp.jar
       sudo ln -s  /path/to/your/myapp.jar /etc/init.d/myapp
       sudo service myapp start 
    fi

Save and run your job, the service should start automatically.




回答4:


The nohup and the at now + 1 minutes didn't work for me. Since Jenkins was killing the process spun in the background, I ensured the process to not be killed by setting a fake BUILD_ID to that Jenkins task. This is what the Jenkins Execute shell task looks like:

BUILD_ID=do_not_kill_me
java -jar -Dserver.port=8053 /root/Deployments/my_application.war &
exit

As discussed here.




回答5:


This worked for me on jenkins on a linux machine

kill -9 $(lsof -t -i:8080) || echo "Process was not running."
mvn clean compile
echo "mvn spring-boot:run" | at now + 1 minutes

In case no process on 8080 it will print the message and will continue.

Make sure that at is installed on your linux machine. You can use

sudo apt-get install at

to install at



来源:https://stackoverflow.com/questions/28500066/how-to-deploy-springboot-maven-application-with-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!