Creating CICD pipeline in Jenkins for a SpringBoot application

Vishal KerkettaVishal Kerketta
4 min read

Overview

This Project gives a practical demo to create a CICD pipeline for a multi-tier SpringBoot project that has Nginx as a proxy server containerized Java and MYSQL application.
You will learn and use Several components of Jenkins like Jenkins Agent, Jenkins Shared Library, Webhook, and declarative Syntax.
with some other tools like docker, nginx web server, Mysql database, and Maven.

CICD Workflow

  • Cloning the Project code from GitHub.

  • Build a docker image and push it to the docker hub.

  • Pull the image from the docker hub and deploy an application using docker-compose.

Creating CICD pipeline

  1. Install Jenkins and access on port 8080. Login and install Suggested Plugins.

     http://<Instance_IP>:8080
    
  2. Agent Setup: Agents are set to distribute the builds in parallel execution. They communicate with the Jenkins master via SSH, and JRE(Java Runtime Environment) should be installed on that node.

    Configure Jenkins agent:

    • generate a SSH public-private key pair, so that the Jenkins master can communicate with the worker node.

    • Navigate to Dashboard > Manage Jenkins > Credentials and add credential (SSH Username with private key)

    • Navigate to Dashboard > Manage Jenkins > Nodes.
      add node and wait for it to come online.

      • Docker and docker-compose:V2 should be installed on the Worker or deployment server for deploying application using docker-compose.

      • Add the user into the docker group, who is executing the Jenkins job in the worker node.

  3. Credentials: Add credentials like SSH key, and dockerhub access token so that jenkins can use while job execution.

  4. Jenkins Shared Library

    • A shared library is used to store common repetitive steps to manage and configure pipelines in a centralized way, it uses Groovy syntax and shared Groovy files should be in the vars folder.

    • To configure the shared library for your Jenkins Server Navigate through Dashboard > Manage Jenkins > System, and add Global Trusted Pipeline Libraries. (Modern SCM),

    • give the version as the branch name.

    • Import Library in the pipeline.

Understand shared libraries as functions stored in any repository that you can use in the pipeline to get the job done.

  1. Create Job

    • Create a job of type pipeline and configure it, tick on GitHub project, add webhook, and poll SCM (1 hour) both for the automatic builds and select pipeline script from SCM.

    • WebHook and poll SCM.

      • Webhook: Automatically built when a change is made to the Source code.

      • Poll: periodically monitor the repository and if any changes are detected, build Job.

  2. Pipeline:

     @Library("shared-library@DevOps") _
    
     pipeline {
         agent {label 'runner_1'}
    
         stages {
             stage('Checkout code') {
                 steps {
                     codeCheckout('DevOps', 'https://github.com/joakim077/Springboot-BankApp.git')
                 }
             }
             stage('build') {
                 steps {
                     buildImage("springboot-application")
                 }
             }
             stage('Push Image') {
                 steps {
                     pushImage("springboot-application")
                 }
             }
             stage('Deploy'){
                 steps{
                     deploy()
                 }
             }
    
         }
     }
    
  3. Execute job

    • Build Job: Build the Job manually first and check if everything works properly.

    • Whenever there will be a change detected by Jekins on SCM it will build automatically.

  4. Installing nginx and Domain mapping:
    After successfully building of CICD pipeline, you can access the application on port 8080.

     http://<IP>:8080/
    

    Install the nginx web server for proxy and SSL termination and map the domain with your nginx server’s public IP.
    Go to your Domain registrar’s dashboard and for your domain add an A record (IPV4) or a CNAME record (Public IPV4 DNS).

  5. SSL Encryption for HTTPS and configure nginx as proxy server:

    • Create a new Nginx server block configuration file for our bank-app application

        sudo touch /etc/nginx/sites-available/bank-app
      
    • Configure server: add the following code

        server {
             listen 80;
             server_name bank.joakim.online; # Replace with your domain
      
             location / {
                 proxy_pass http://localhost:8080; # Since Bankapp is running on port 8080
                 proxy_set_header Host $host;
                 proxy_set_header X-Real-IP $remote_addr;
                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                 proxy_set_header X-Forwarded-Proto $scheme;
             }
      
             location ~ /\. {
                 deny all;
             }
         }
      
    • Create a symbolic link to the configuration file in the sites-enabled directory:

        sudo ln -s /etc/nginx/sites-available/bank-app /etc/nginx/sites-enabled/
      
    • Test Nginx Configuration and restart nginx if test is successful

        sudo nginx -t 
        sudo systemctl restart nginx
      
    • SSL certificate to the Domain.

        sudo apt install python3-certbot-nginx
      
        certbot --version
      
        certbot --nginx -d bank.joakim.online.com
      

Conclusion

In this project, we delve into creating a CICD pipeline for the containerized SpringBoot application, push the docker image to the docker hub, deploy an application, and map the web server with our domain.
learned about Jenkins pipeline, shared library, and agents. also used docker, docker-compose:V2. and AWS EC2.

Remember two things while working with Jenkins: Plugins and Restart the Jenkins server.

Thanks for reading and happy learning!

0
Subscribe to my newsletter

Read articles from Vishal Kerketta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vishal Kerketta
Vishal Kerketta

Hello! I'm a student passionate about becoming a Cloud and DevOps Engineer. I have a solid foundation in AWS Cloud, Linux, Docker, and Kubernetes.