Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy SpringBoot Maven application with Jenkins ?

Tags:

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.

like image 836
azizunsal Avatar asked Feb 13 '15 12:02

azizunsal


People also ask

What is the use of Jenkins in spring boot?

Creating a new job This definition uses the Jenkins Job DSL Plugin syntax to create a pipeline job from a Jenkinsfile in the spring-boot-api-example repository (or use your own if you prefer). A Jenkinsfile is a pipeline definition that lives inside the repository of the project that it applies to.

Where can I deploy my maven package Spring boot application?

Deploying in Java Archive (JAR) as a standalone application xml once the Spring project is created via Spring Initializr as a Maven project. In order to package the application in a single (fat) jar file, run the maven command mvn package under project directory.


2 Answers

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' 
like image 70
kryger Avatar answered Oct 12 '22 12:10

kryger


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

like image 28
Rogerio Coli Avatar answered Oct 12 '22 12:10

Rogerio Coli