DevOps Blog 2: CI Pipeline setup for Maven-Spring Boot Project with Custom Dockerized Jenkins-Maven on AWS

Mukesh Vivekanandan
6 min readSep 8, 2023

This blog offers a comprehensive guide to setting up a Jenkins CI pipeline on AWS EC2 instance using Docker and integrating it with a Maven-based Spring Boot application on GitHub. Through detailed steps, readers will learn how to deploy Jenkins inside a Docker container, create a Spring Boot /hello endpoint, and automate builds using a Jenkinsfile, all while maintaining secure access with GitHub personal access tokens.

Summary

Project: CI Pipeline setup for Maven-Spring Boot Project with Custom Dockerized Jenkins-Maven on AWS

Outcome: Learn how to create a simple Maven-Spring Boot Project, Spin up EC2 instance, Create Jenkinsfile to checkout, build and test the java project, Create customized Jenkins-Maven image using Dockerfile. Generate the Personal Access Token in GitHub for authentication (to avoid exposing password in configuration)

GitHub Repo 1 : Jenkinsfile for the Pipeline to instruct

GitHub Repo 2 : Maven-Spring Boot Project

High-Level Architecture

Building Jenkins Pipeline to checkout maven project, compile/build and unit test

Prerequisites

  1. AWS Account: Active AWS account for EC2 instance creation.
  2. GitHub Account: Required for repository management and access tokens.
  3. Basic Understanding: Familiarity with Jenkins, Docker, Maven, and Spring Boot.
  4. Local Environment: Maven and Git installed on your local machine.
  5. Editor: A code editor (like Visual Studio Code) for project management.

Steps

1. Access AWS Console:

- Navigate to https://aws.amazon.com and log in to your AWS account.

- From the AWS Management Console home, click on “Services” and select “EC2”.

2. Set Up EC2 Instance:

- Click the "Launch Instance" button.
- Select the Free Tier Amazon Linux 2 AMI.
- Ensure it's t2.micro for the free tier eligibility.
- On the "Configure Security Group" step, create a new security group with the following rules:
- Type: SSH, Port: 22, Source: Anywhere.
- Type: Custom TCP, Port: 8080, Source: Anywhere.
- Review and launch the instance.

3. Plan for Jenkins Deployment:

  • Instead of installing Jenkins directly on the EC2 instance, opt to run Jenkins inside a Docker container for modularity and scalability.

4. Install Required Packages:

- SSH into your EC2 instance.
- Update the package list: `sudo yum update -y`
- Install Docker and Git: `sudo yum install docker git -y`

5. Set Up Docker for Jenkins:

- Switch to the root user: `sudo su`

- Create a Dockerfile with the following content:

#Retrieve the docker container id
PuTTy ssh into the ec2 instance with username ec2-user using your-ec2-ip-address

sudo su #sudo as root user

vi Dockerfile # Creating Dockerfile

Dockerfile

# Use the official Jenkins image as a starting point
FROM jenkins/jenkins:lts-jdk11

# Maintainer Info
LABEL maintainer="your-email@example.com"

# Switch to root user to install Maven
USER root

# Install Maven
RUN apt-get update && apt-get install -y maven

# Switch back to the Jenkins user
USER jenkins

# Set the environment variable for Maven
ENV MAVEN_HOME /usr/share/maven
ENV PATH $MAVEN_HOME/bin:$PATH

# Copy Jenkins job configurations and other setup configurations if needed
# COPY jobs/ /var/jenkins_home/jobs/

# Expose the default Jenkins port
EXPOSE 8080

- Build the Docker image:

docker build -t jenkins-maven .

- Run Jenkins inside the custom Docker container:

docker run -d -p 8080:8080 -p 50000:50000 jenkins-maven

6. Complete Jenkins Setup:

- Access Jenkins via `http://<your-ec2-ip>:8080/`.

- Retrieve the initial admin password from the EC2 instance: `cat /var/jenkins_home/secrets/initialAdminPassword`

-Follow the setup wizard: set up an admin user and install the recommended plugins.

7. Set Up Maven Project on GitHub:

- Use the Spring Boot Initializr (https://start.spring.io/) with Java 11 and Spring 2.7.15 to initialize a Maven project.

- Create a `/hello` GET endpoint in your Spring Boot application that returns a “Hello World” JSON response.

- Implement a unit test for the `/hello` endpoint.

  • Initialize a Git repository in your project directory and push the project to a new GitHub repository.
If you are unable to follow above steps, feel free to download or clone/fork the same Spring-Maven Project repository

https://github.com/Mukezz/CIDemoBlog2.git

Using above repo create your own repo under your Github account

8: Incorporate Jenkins Pipeline

Create a separate GitHub repository for Jenkinsfile. Create a `Jenkinsfile` at the root of your project. This file will contain the stages and steps for your Jenkins pipeline:

pipeline {
agent any

tools {
maven 'Maven_3_8_7' // Replace 'Maven_3_8_7' with the name of your Maven installation in Jenkins
}

stages {
stage('Checkout') {
steps {
// Assuming you've set up a GitHub credential in Jenkins named 'github-credentials'
//git branch: 'main',
credentialsId: 'github-credentials',
url: 'https://github.com/Mukezz/CIDemoBlog2.git'

checkout([$class: 'GitSCM', branches: [[name: '*/main']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'github-credentials', url: 'https://github.com/Mukezz/CIDemoBlog2.git']]])
}
}

stage('Compile') {
steps {
script {
sh "mvn compile"
}
}
}

stage('Test') {
steps {
script {
sh "mvn test"
}
}
}
}
}

This Jenkinsfile instructs Jenkins to:

  • Use any available agent to run the pipeline.
  • Utilize the specified Maven tool (Maven_3_8_7) for Maven commands.
  • Define three stages: Checkout, Clean & Compile, and Run Tests.
  • Within each stage, execute the appropriate Maven commands.

After creating the `Jenkinsfile`, commit and push it to your GitHub repository.

9: Create a Personal Access Token in GitHub

1. Login to GitHub:
- Open a web browser and navigate to [GitHub](https://github.com/).
- Enter your credentials and log in.
2. Navigate to Developer Settings:
- Click on your profile picture (top-right corner).
- From the dropdown menu, select "Settings".
- In the left sidebar, scroll down and click on "Developer settings".
3. Access Personal Access Tokens:
- In the left sidebar of the Developer settings page, click on "Personal access tokens" and "Fine-Grained Personal Token".
4. Generate a New Token:
- Click on the "Generate new token" button (top-right of the main pane).
5. Set Up Token Details:
- Note: Provide a meaningful description for your token to remember its purpose later.
- Expiration: Decide on an expiration time for the token. It's often a good idea to set an expiration if the token serves a temporary purpose.
- Select Scopes: Depending on your use-case, you'll select the appropriate permissions (or "scopes") for this token. For Jenkins:
- Check the `repo` checkbox (This grants full control of private and public repositories).
- If you plan to update commit statuses or other administrative tasks, you might need to select additional scopes.
6. Generate the Token:
- Scroll down and click the "Generate token" button.
7. Copy and Store Your New Token:
- After generating, GitHub will display the token only once. Make sure to copy the token to a safe location.
- If you lose it, you will need to regenerate a new token.
Fine-grained personal access token generation

Now you have a personal access token! For Jenkins, you’ll use this token as the password (with your GitHub username) when setting up credentials.

11: Configure a Jenkins Pipeline Job:

- In Jenkins, select “New Item” and then choose “Pipeline”.

- Name your pipeline and then scroll to the “Pipeline” section.

- Under “Definition”, select “Pipeline script from SCM”. For SCM, choose “Git”, and then provide the URL to your GitHub repository.

  • Ensure the path to your `Jenkinsfile` is correctly set.
Screenshot of the Job Creation using the Pipeline option
Screenshot of the pipeline in the configuration job using github Jenkinsfile
Provide your GitHub Username and Personal Generates Token as password and ID should match with Jenkinsfile credentialsId. Example ‘github-credentials’

12: Setting up Maven Installation in Jenkins

Follow the highlights to setup the Maven Installation

13: Trigger Jenkins Job

Go to your newly created Jenkins job and click “Build Now” to trigger the pipeline. Monitor the execution and, upon successful completion, you’ll see the stages of the pipeline execution.

Please comment your questions , feel free me to reach me on LinkedIn for any further clarification on the steps.

--

--

Mukesh Vivekanandan

Software Engineer | Technology Enthusiast | Badminton Player