Set Up Gated Check-in for Spring Boot Projects with Github and Jenkins

Introduction

One of the biggest challenges the developers are facing is being Agile or, in other words, being able to deliver stable releases of the products on time. With every Sprint, we need to deliver new features, bug-free features.

And why bother with building, testing, and deploying manually when we could have an automated process? A process that would check every piece of code before merging it in the stable branch and then deploying it. For example, deploying it on a testing machine ready for the QA team.

There are many tools for implementing this CI/CD process, but we'll stick with an awesome open-source tool, Jenkins.

Jenkins is an automation server. It helps automate building, testing, and deploying your application. Jenkins supports Git and Maven which will be our guest stars in this mini project.

This article will guide you through configuring the Jenkins process to be triggered every time a commit is made. Then we'll make sure our GitHub repository is configured properly for this scenario. Lastly, we will make a small test to show that everything works properly.

Prerequisites

For you to be able to follow this tutorial, some small things need to be set up.

  • A Jenkins server. You can use DigitalOcean to create a droplet with Jenkins ready to use. It should take less than 5 minutes.
  • A GitHub repository. Note that you can use and source control hosting provider as long as they provide you the ability to add Webhooks.
  • A Spring Boot application with tests. If you don't have one already you can clone this repository.

Configuring Jenkins

So after you created the Jenkins droplet, there are some steps you need to follow for the machine to be up and running.

Let's connect to our newly created droplet and prepare our environment. We'll start by running some commands in the terminal:

$ ssh root@YOUR_VM_IP

You will be asked to create your Jenkins password. Use a strong password and restart your Jenkins service:

$ systemctl restart jenkins

Your Jenkins server is now accessible at the following address http://YOUR_VM_IP:8080 and you should see something like this. After that, you'll be redirected to the login page:

And voilà, we are ready!

The default username is admin, and the password is the one you created at the beginning.

Before we create our first project, let's go to Manage Jenkins ~> Manage Plugins and install GitHub Pull Request Builder.

Now go to Manage Jenkins ~> Configure System set up the GitHub server. Let's go and click on New Item in the upper left corner of the screen:

Now, go ahead and type a name for your new project and select Freestyle project:

We need to specify some parameters for the project, so Jenkins will know what commit it should use for the build and test part.

The first thing we need to do is to specify the project URL, under the General tab, and GitHub project.

Under Source Code Management tab, select Git. After you enter your Repository URL and your credentials, under Branched to build, use ${ghprbActualCommit}.

Click on the Advanced... button and add the following details:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

  • Name: origin
  • Refspec: +refs/pull/${ghprbPullId}/*:refs/remotes/origin/pr/${ghprbPullId}/*

Note: This value is required in order to build only the pull requests.

After we have configured our repository, we need to specify when this job will be triggered. For that, go to Build Triggers tab and select GitHub Pull Request Builder.

Firstly, select the credentials configured earlier and check Use GitHub Hooks for build triggering. Now, add a Update commit status during build trigger and configure it like in the image:

Add a Build Status Messages trigger for SUCCESS, ERROR, and FAILURE.

For the Build phase we'll add Invoke top-level Maven targets with the following settings:

  • Maven version - your desired Maven version, can be configured from System Configuration
  • Goals - clean install -U
  • POM - pom.xml

Click Save and we are done!

GitHub Repository Configuration

We've managed so far to configure the Jenkins workflow, but what about GitHub? That's what we're going to do right now.

Go to Settings on your repository page and select the Webhooks option from the left menu. Add a new webhook with the following details:

For the Payload URL type http://YOUR_VM_IP:8080/ghprbhook/ and we'll select Pull Requests as the triggering option. The content type should be application/json. Leave the Secret field empty and Save.

Testing the Workflow

The moment we've been waiting for. Create a new branch, push it to GitHub, and then create a Pull Request. As soon as we create the Pull Request, a job will be triggered on Jenkins:

Soon, a Details link will appear on the pull request's page. If you click on the link, you'll see a page like the this one:

Now if we go to the Console Output we can see that all the tests have passed:

[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
...
Finished: SUCCESS

For the sake of this tutorial, I'll make another commit with a failing test to see that it works properly. And the result:

[ERROR] Tests run: 3, Failures: 1, Errors: 0, Skipped: 0
...
Finished: FAILURE

Here, we've successfully implemented a gated check-in for Spring Boot projects, using GitHub and Jenkins.

Conclusion

Software Engineers are expected to provide bug-free solutions to their clients. With complex logic, it becomes paramount to test software and ensure checks and test cases to make sure that the project is stable.

In this article, we've implemented a gated check-in for Spring Boot projects, using GitHub and Jenkins, to ensure passing checks in the code we push.

Was this article helpful?

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms