Skip to content

Continuous Integration with Jenkins and Docker

As Jenkins is one of the most popular CI tools on the market with over a thousand plugins, in this article, we are going to set up a CI pipeline for a SpringBoot application.


Install docker

Start the docker service and login the docker hub.

sudo curl -sSL | sh
sudo service docker start
sudo docker login

Install docker-compose

sudo -i
curl -L`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

Create jenkins container

Start Jenkins container and configure the user.

sudo docker run --name jenkins -p 8080:8080 -p 50000:50000 -v /var/jenkins_home jenkins

Deploy SSH key

Use nsenter to attach the Jenkins container, and run commands in Jenkins container to generate ssh key, then the Jenkins server will have .ssh folder generated under ~, under the .ssh folder there are id_rsa (private key) and (public key).

Copy the content in, and go back to the remote server on which jenkins will SSH and execute shell scripts and deploy the application, and paste the content in ~/authorized_keys.

sudo docker run -v /usr/local/bin:/target jpetazzo/nsenter

PID=$(sudo docker inspect --format {{.State.Pid}} jenkins)
sudo nsenter --target $PID --mount --uts --ipc --net --pid

ssh-keygen -t rsa
#Copy key

Restart the SSH service

Restart the ssh service in remote server, and create a folder to put scripts into it and modify the permission of it. For example: ~/agriculture-platform.

service sshd restart
sudo mkdir ~/agriculture-platform/
sudo chmod 777 ~/agriculture-platform/

Jenkins configuration

Publish over SSH plugin

Install the plugin "Publish over SSH" in Jenkins, and configure it, then test the connection. Publish over SSH plugin

Generate API token

Login your jenkins with admin user and click the user on the right hand side, then select configure and generate a token here. Publish over SSH plugin

Create maven project

Create a maven project in Jenkins, configure the git URL and credentials for it. Create maven project

Enable build trigger

Enable the remote build trigger token which is the above generated API token. Build trigger

Build maven commands

Configure the build step with maven build commands. Build goals

After build steps

Configure the after build step to SSH and copy the jar file to the remote server. Here we execute a shell script to deploy the SpringBoot application. Build publish

Create webhook in Github

Login the Bitbucket or Github and enter your SpringBoot project, enter the Webhooks page, add new a webhook with Jenkins server info and your job name and the above generated API token.

The webhook will be invoked when you push any code to the master branch. The related jenkins project will receive this webhook request and execute the build job immediately. Create webhook

Try push code

If we try to push the code to Bitbucket now, we will find a webhook request error in Webhooks page. Try push

Configure global security

We should login the Jenkins and configure the Global Security and enable the webhook in order to fix the above issue. Configure global security

Deploy configuration

Shell scripts

Now Jenkins is configured to transfer the SpringBoot jar to "~/agriculture-platform" folder and run the shell script "~/agriculture-platform/".

Login the remote server, we create "agriculture-platform" folder under ~, and create Dockerfile and under it. Shell scripts

SpringBoot Dockerfile

The Dockerfile content is as bellow, it creates a docker image and copy the jar into it, then run the jar.

FROM hustakin/java8
MAINTAINER Frankie Fan ""

ADD agriculture-platform.jar agriculture-platform.jar
ENTRYPOINT ["java","","","-jar","/agriculture-platform.jar"]
EXPOSE 8001 8002

Build scripts

The content is as bellow, it creates a docker image and copy the jar into it, then run the jar.

cd ~/docker/agriculture-platform/
sudo docker stop agriculture
sudo docker rm agriculture
sudo docker build --no-cache -t="hustakin/test" .
sudo docker run -p 80:80 -p 443:443 -v /data/log/dev:/logs --name agriculture --restart=always -d hustakin/test --param=hello

Push code

After all the above configurations, now we completely finish the CI process. When we push the code to Bitbucket or Github, the Jenkins will start to maven build the code and transfer the jar to the remote server, then the remote server will rebuild the SpringBoot docker image and recreate the docker container. CI build