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

Install docker

Start the docker service and login the docker hub.

Install docker-compose

Create jenkins container

Start Jenkins container and configure the user.

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 id_rsa.pub (public key).

Copy the content in id_rsa.pub, 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.

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.

Jenkins configuration

Publish over SSH plugin

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

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.

Create maven project

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

Enable build trigger

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

Build maven commands

Configure the build step with maven build commands.

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.

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.

Try push code

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

Configure global security

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

Deploy configuration

Shell scripts

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

Login the remote server, we create “agriculture-platform” folder under ~, and create Dockerfile and rebuild.sh under it.

SpringBoot Dockerfile

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

Build scripts

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

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.

Written by

Researcher | Architect | Full-Stack | @hustakin

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store