Spring Boot Banking Application

The goal of this project is to use Jenkins, Docker, and GitHub to put up a CI/CD pipeline for a Spring Boot banking application. It leverages a multi-node Jenkins architecture, shared libraries, and webhooks to automate development, deployment, and integration operations. The pipeline includes containerization of the application, automatic triggering via GitHub webhooks, and deployment on a development server hosted on AWS EC2 instances. By following this setup, the deployment process is streamlined, ensuring consistent and efficient updates to the application.

  • Project Summary

This project sets up a CI/CD pipeline to automate the build, deployment, and integration of a Spring Boot banking app. It uses Jenkins, Docker, and GitHub with features like multi-node Jenkins, shared libraries, and webhooks for automatic updates.

  • Key Steps:
  1. AWS EC2 Instances: We will create two EC2 instances—one for the Jenkins Master to manage the CI/CD processes and another for the Jenkins Agent, which will handle the deployment of the Spring Boot application.

  2. Jenkins Setup: The Jenkins Master will be configured to automate the CI/CD pipeline, while the Jenkins Agent will be set up to run builds and deployments.

  3. Containerization: The Spring Boot application will be containerized using Docker, facilitating easy deployment and scaling.

  4. GitHub Integration: We will integrate GitHub to enable automatic deployment triggered by code changes, ensuring that the latest version of the application is always deployed.

  5. Multi-Node Architecture: A multi-node Jenkins setup will be established, allowing for efficient resource management and deployment on a dedicated development server.

  6. Jenkinsfile Creation: A Jenkinsfile will be created to define the automated build and deployment pipeline, outlining the necessary stages for code cloning, building, pushing to Docker Hub, and deploying the application.

  7. Webhooks for Automation: GitHub webhooks will be configured to trigger Jenkins builds automatically upon code changes, streamlining the development workflow.

  • Step-by-Step Guide: Setting Up a Jenkins Master-Agent Architecture on AWS

    Step 1: Set Up an AWS Account

    1. Sign in to AWS

      • Create an AWS account at aws.amazon.com if you don’t already have one.
    2. Access AWS Management Console

      • Log in to the AWS Management Console to manage services.

Step 2: Launch EC2 Instances

2.1 Launch the Jenkins Master Instance

  1. Navigate to EC2 Dashboard

    • From the AWS Management Console, select EC2 under the Services menu.
  2. Launch Instance

    • Click Launch Instance and choose an Amazon Machine Image (AMI), such as Ubuntu 24.04 LTS or Amazon Linux 2.
  3. Choose an Instance Type

    • Select a t2.micro instance type for Jenkins Master.
  4. Configure Instance Details

    • Set the instance count to 1 and configure networking settings.
  5. Add Storage

    • Use the default storage size or increase it based on requirements.
  6. Configure Security Group

    • Allow:

      • TCP port 8080 (for Jenkins web interface).

      • SSH (port 22) for remote access.

  7. Review and Launch

    • Review the settings, select or create a key pair for SSH, and launch the instance.

2.2 Launch the Jenkins Agent Instance

  1. Repeat Launch Steps

    • Follow the same steps to launch a second EC2 instance for the Jenkins Agent.
  2. Instance Type

    • Select a t2.Medium instance type to support application hosting.
  3. Security Group

    • Ensure inbound rules allow SSH (port 22) and other necessary ports (e.g., for Spring Boot app).

Step 2: Connect to Both Instances

  • Use SSH to connect to the instances:
    ssh -i <your-key>.pem ubuntu@<your-ec2-public-ip>

Step 3: Update Both Instances

  • Run the following command on both the Master and Agent instances to update their packages:

      sudo apt update && sudo apt upgrade -y
    

Step 4: Install Java

  • Jenkins requires Java to run. Install OpenJDK 17 on both instances:

      sudo apt install openjdk-17-jre -y
      java -version
    

Step 5: Install Jenkins on Master

  1. Install Dependencies

     sudo apt-get install -y ca-certificates curl gnupg
    
  2. Add Jenkins Repository

     curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
     echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
    
  3. Install Jenkins

    sudo apt-get update
    sudo apt-get install jenkins -y
  1. Start Jenkins
    sudo systemctl enable jenkins
    sudo systemctl start jenkins
  1. Check Jenkins Status
    sudo systemctl status jenkins

Step 6: Install Docker on Both Instances

  • Install Docker on both the Master and Agent instances:

      sudo apt install docker.io -y
    
  • Add Jenkins to the Docker group (on both instances):

    sudo usermod -aG docker $USER
    newgrp docker

Step 7: Install Docker Compose on Both Instances

  • Install Docker Compose:

      sudo apt install docker-compose -y
    
  • Verify installation:

      docker --version
      docker-compose --version
    

Step 8: Configure Security Group for Jenkins

  1. Go to the EC2 Dashboard in AWS.

  2. Edit the Security Group for the Master instance:

    • Add a Custom TCP Rule for port 8080.
  3. Access Jenkins in your browser using:

     http://<your-master-public-ip>:8080
    

Step 9: Retrieve Jenkins Admin Password

  1. Get the initial admin password from the Master instance:

     sudo cat /var/lib/jenkins/secrets/initialAdminPassword
    

2. Paste this password into the Jenkins setup page in your browser to complete the setup.

  • Configuring a Jenkins Agent as a Development Server for Deploying the Spring Boot Banking Application

  • To add a new node in Jenkins:

  1. Log in to Jenkins.

  2. Go to Manage Jenkins > Manage Nodes

  3. Click New Node:

    • Type a name for the agent “bankapp“.

    • Select Permanent Agent and click OK.

  4. Set up the Node:

    1. Remote root directory: Enter /home/ubuntu/bankapp. Make sure to create a folder named bankapp on the machine (you can take any name for your remote directory).

    2. Labels: Add a label like “bankapp“ for this node.

    3. Usage: Select Only build jobs with label expressions matching this node.

  5. To set up the launch method for the node:

    1. Under Launch Method: Select Launch agents via SSH.

    2. Host: Enter the public IP address of your Jenkins Agent instance.

    3. Credentials:

      • Choose SSH Username with private key.

      • For the username, use ubuntu.

      • Add the private key:

        • On your local machine, run sudo cat <private-key-file-name> to copy the content of your private key file.

        • Paste the private key into the provided box.

    4. Save and Connect: Click Save to connect Jenkins to the Agent.

Step 10: To set up Docker Hub credentials in Jenkins:

  1. Go to Jenkins Settings:

    • In Jenkins, click on Manage Jenkins > Security > Credentials > System > Global credentials (unrestricted).
  2. Add New Credentials:

    • Click on Add Credentials.
  3. Choose Credential Type:

    • For Kind, select Username with password.

    • Username: Enter your Docker Hub username.

    • Password: Use a Personal Access Token from Docker Hub as the password.

      • To get the token, go to Docker Hub, generate the token, and paste it in the Password field.
  4. Save the Credentials:

    • Click Save to store your credentials.

Step 11: To create a Jenkins pipeline job:

  1. Create a New Job:

    • From the Jenkins dashboard, click New Item.

    • Enter a name for your job (e.g., "Springboot-BankApp").

    • Select Pipeline and click OK.

  2. Set Up GitHub Integration:

    • In the General section, check the box for GitHub project.

    • Enter the URL of your GitHub repository.

  3. Configure the Pipeline:

    • Under the Pipeline section, select Pipeline script from SCM.

    • Set SCM to Git and provide the URL of your Git repository.

    • Choose the dev branch and set Script Path to Jenkinsfile.

  • How to create the Jenkinsfile on GitHub:

  1. Create the Jenkinsfile: In your GitHub repository, create a file named Jenkinsfile. Add the following script inside it:

    Here's a simple explanation of each part:

    1. @Library('shared'): This line uses a shared library for common code or functions used in multiple pipelines.

    2. agent {label 'dev-server'}: It tells Jenkins to run the pipeline on a server with the label dev-server (You can take any Label name).

    3. stages: This section contains multiple stages (steps in the process) that Jenkins will run one after the other.

      • stage("Code"):

        • It clones the code from your GitHub repository (Springboot-BankApp) and checks out the main branch.

        • After that, it prints "Code cloning done."

      • stage("Build"):

        • It builds the Docker image for the app and tags it as "latest."

        • It prints "Code build bhi hogaya" (meaning "Code build is done").

      • stage("Push to DockerHub"):

        • It pushes the built Docker image to Docker Hub with the name bankapp-mini and tag latest.

        • It prints "Push to DockerHub is also done."

      • stage("Deploy"):

        • This step deploys the app (the exact deployment process is defined elsewhere).

        • It prints "Deployment bhi done" (meaning "Deployment is done").

2. This script does the following:

  • Clones the code from GitHub.

  • Builds a Docker image.

  • Pushes the image to Docker Hub.

  • Deploys the application on the Jenkins Agent.

  1. Commit the Changes: Save the Jenkinsfile and commit it to your GitHub repository.

Step 12: To set up automatic deployment in Jenkins when you make changes to your code, you need to configure webhooks in your GitHub repository. Here's how to do it:

  1. Go to GitHub Repository Settings:

    • Open your GitHub repository and click on the Settings tab.
  2. Set Up Webhooks:

    • On the left sidebar, click on Webhooks, then click on Add webhook.
  3. Enter the Webhook Details:

    • In the Payload URL field, enter:

        http://<your-ec2-public-ip>:8080/github-webhook/
      
    • Set Content type to application/json (default).

    • Choose Just the push event to trigger the webhook on code pushes.

  4. Save the Webhook:

    • Click on Add webhook. Once added, you should see a green tick, confirming the webhook is set up correctly.

Step 13: Steps to Configure the Shared Library in Jenkins:

  1. Go to Jenkins Configuration:

    • Open Jenkins and log in.

    • From the Jenkins dashboard, click on Manage Jenkins.

  2. Configure Global Pipeline Libraries:

    • In the Manage Jenkins page, click on Configure System.

    • Scroll down to the Global Pipeline Libraries section.

  3. Add a Shared Library:

    • In the Global Pipeline Libraries section, click on Add to add a new shared library.

    • For the Library Name, enter Shared (this should match the name used in your Jenkinsfile).

    • For the Source Code Management section, select Git.

    • In the Repository URL, enter the Git URL of the repository that contains the shared library code.

    • Set the Default Version (e.g., main or master branch) to specify the version of the library Jenkins should use.

  4. Save the Configuration:

    • After adding the shared library, scroll down and click Save.

Why this matters:

Your Jenkins pipeline can utilize the reusable code or functions found in the shared library. You can instruct Jenkins on where to locate the shared library and the version to use by specifying it in Jenkins.

Step 14: Building and Accessing Your Project in Jenkins

Step 1: Trigger the First Build

  1. Go to the Jenkins Dashboard:

    • Open Jenkins in your browser.

    • Find the pipeline job you created for your Spring Boot application.

  2. Start the Build:

    • Click the Build Now button for your pipeline job.

    • This will start the process of building and deploying your Spring Boot application.

Step 2: Access the Application

  1. Allow Traffic to Your Application:

    • Go to the Jenkins Agent EC2 instance security settings.

    • Add a new inbound rule to allow traffic on port 8000 (this is where your application will run).

  2. Visit the Application:

    • After the build is successful, open a browser.

    • Type the following URL to access your deployed Spring Boot application:

        http://<your-ec2-public-ip>:8000
      

By following these steps, you've successfully set up a fully automated CI/CD pipeline for your Spring Boot application using Jenkins. Keep experimenting, and don't hesitate to explore new tools and techniques to enhance your DevOps journey.

  • Output of projects.

THANK YOU FOR WATCHING THIS BLOG AND THE NEXT BLOG COMING SOON.