Devops Project no 1 - Automated CI/CD Pipeline for Java Application: Leveraging GitHub, Maven, SonarQube, and Jenkins in AWS

In this Devops Project we are using Github as SCM, Maven as a build tool, Sonarqube as a code quality test tool, Jenkins as a CI and CD tool...


In this Devops Project we are using Github as SCM, Maven as a build tool, Sonarqube as a code quality test tool, Jenkins as a CI and CD tool, Docker as a containerization tool, and AWS as a cloud platform.

Before Starting the Jenkins Project we need to set up the Ec2 instance with min 8GB of RAM. Inside the Ec2 instance need to install Jenkins. Install docker and run the sonarqube as a container.

Step1: Install the jenkins using the below URL 
    https://www.jenkins.io/doc/book/installing/linux/
Commands for installing jenkins in Amazon-Linux:
    sudo wget -O /etc/yum.repos.d/jenkins.repo     https://pkg.jenkins.io/redhat-stable/jenkins.repo
    sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
    sudo yum upgrade
    sudo yum install java-17*
    sudo yum install jenkins
    sudo systemctl daemon-reload
    systemctl enable jenkins
    systemctl start jenkins
    systemctl status jenkins

Install git and generate the private and public keys for the jenkins user. If you have already installed and configured the jenkins skip the above step.
After installing and starting the jenkins add 8080 and 9000 ports in inbound rules and start configuring the jenkins.

Step2: Install the docker using the commands
Commands:
        yum install docker
        systemctl enable docker
        systemctl start docker
        systemctl status docker

Step3: Pull the Sonarqube Docker images and run the sonarqube docker container
Commands:
        docker run -d -p 9000:9000 sonarqube:lts-community 
        docker ps


Step4: Install the required plugins
    Manage jenkins → plugins → Search Jdk → choose Eclipse Temurin installer and openJDK-native-plugin, SonarQube Scanner, and click install


Now we need to create the Pipeline job
Click pipeline choose example pipeline and change the pipeline 
pipeline {
    agent any
    stages {
        stage('git checkout') {
            steps {
Inside the steps, we need to get the pipeline syntax for that we need to click the pipeline syntax under the pipeline. 

In the Sample step choose git, repository Url add your repo “git@github.com:maneshmohan124/project1-ekart.git”
Choose the branch main untick Include in polling? Include in the changelog? and click Generate pipeline script.

Copy the generated script and paste it into the pipeline.
pipeline {
    agent any
    stages {
        stage('git checkout') {
            steps {
               git branch: 'main ', changelog: false, poll: false, url: 'git@github.com:maneshmohan124/project1-ekart.git'
            }
        }
    }
}
Now we need to add a second stage, the second stage is “compile”
For compilation(build tool), we need to use Maven for that we need to add our tools before the stage ”git checkout”.

tools {
        jdk 'jdk11'
        maven 'maven’'
    }

Add another stage called compile and inside the steps we need to add the maven compile step.

stage('compile') {
            steps {
               sh "mvn clean compile" 
            }   
        }
Now we need to add a third stage, the third stage is “sonarqube Analysis”.

For sonarqube, we need to add an environment inside the pipeline.

environment {
        SCANNER_HOME= tool 'sonar-scanner'
    }

stage('Sonarqube Analysis') {
            steps {
               sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.url=  
            }

steps 2 and 3 we already installed the docker and sonarqube. Now checking the sonarqube by running the publicip:9000
The default username and password is “admin” Once logged in change the password.

Click Administration → click Security → users → click = under the tokens → enter the token number as 1 and click Generate.




In the third stage, we need to add the below lines
stage('Sonarqube Analysis') {

            steps {

               sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.url=http://18.224.153.95:9000/ -Dsonar.login=squ_b2c1d8b9ef60f38a7da2033d592f28c17d2ad42c -Dsonar.projectName=shopping-cart \

               -Dsonar.java.binaries=. \

               -Dsonar.projectKey=shopping-cart '''

Here → Dsonar. url we need to give the sonarqube url 

             Dsonar. login we need to give the sonarqube token

Now apply and save the Jenkins pipeline job. Before running the jenkins job we need to configure the JDK, maven, sonarscan inside the jenkins for that we need to click Manage Jenkins → Tools → click Add JDK type the name(JDK version) → jdk11 → click install automatically → click install from adoptium.net and choose jdk-11.0.21+9

For Maven configuration type the name maven, click install automatically and choose version 3.9.2.
For the sonarqube scanner configuration, type the name sonar-scanner click install automatically choose version 4.8.0 click apply, and save.
Now run the pipeline it took some time to build and wait for some time to finish all the stages. Once the pipeline is successful you can able to check the code quality test result in the sonarqube. Login to the sonarqube and click the project you can able to find your project name called shopping-cart.
Now we need to build the application as a docker image, we need to add another stage for building the application and create the docker image. Before doing the pipeline changes we need to install the below plugins. Search docker and install the plugin name called Docker, Docker Pipeline, docker-build-step, CloudBees Docker Build and Publish
Now we need to configure the docker as a tool inside the jenkins, click manage jenkins → tools → docker installation → click Add docker → give the name as docker → choose Download from docker.com → click install automatically and choose version as latest and click apply and save.
Now we can change our pipeline code next stage was building the application using Maven.

stage('Build application') {
            steps {
             sh "mvn clean install -DskipTests=true"
      }
}

Now we need the syntax for building and pushing docker images to the dockerhub.
Click pipeline syntax below the pipeline inside the configuration.
Choose with DockerRegistry: sets up Docker registry endpoint.
In the Registry credentials click add to add your docker hub credentials.
Choose Docker installation as Docker and click Generate Pipeline Script.


Now we can use that syntax in the pipeline job and add the build, tag, and docker push commands inside the pipeline.

stage('Build & Push Docker Image') {

            steps {

               script{

                   withDockerRegistry(credentialsId: '*********************', toolName: 'docker') {

                         sh "docker build  -t shopping:latest -f docker/Dockerfile ."

                         sh "docker tag shopping:latest 12041995/shopping:latest"

                         sh "docker push 12041995/shopping:latest "

              

                   }

                }

            }   

        }

Now run the pipeline and wait for some minutes for docker building and pushing the images.
Once the Jenkins pipeline is successful, we can check our Dockerhub for the Docker images.
Now the docker image is ready we can deploy that image into the environment. Now you need to create the Pipeline for CD  or add another stage for deployment.
Inside the Jenkinsfile just add the stage called Docker deploy to container and add the below steps. Under the script copy from the previous pipeline Build & Push Docker Image stage.

stage('Docker Deploy to Container') {

            steps {

                script {

                    withDockerRegistry(credentialsId: '96832a6f-5f6f-45ca-93b4-118281d51ece', toolName: 'docker') {

                        sh "docker run -d --name shopping-cart -p 8070:8070 12041995/shopping:latest " }

                }        

            }

        }


Now check the pipeline and see all the stages are green and in a successful state. Once deployment is completed we can able to check the application using port 8070 before that we can find our application container inside the server. The application container was running now we need to add an 8070 port in the AWS ec2-instance’s inbound rules and run the url →http://publicip:8070 in the browser

The application is running the default username password is admin

Now final Pipeline looks like this below


pipeline {

    agent any

    

    tools {

        jdk 'jdk11'

        maven 'maven'

    }


    environment {

        SCANNER_HOME= tool 'sonar-scanner'

    }  

    

    stages {

        stage('git checkout') {

            steps {

               git branch: 'main ', changelog: false, poll: false, url: 'git@github.com:maneshmohan124/project1-ekart.git'

            }

        }

        

        stage('compile') {

            steps {

               sh "mvn clean compile"

               

            }   

        }

        

        stage('Sonarqube Analysis') {

            steps {

               sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.url=http://3.142.76.226:9000/ -Dsonar.login=squ_11c27aa22855c01984109884fc181d3025f7b22b -Dsonar.projectName=shopping-cart \

               -Dsonar.java.binaries=. \

               -Dsonar.projectKey=shopping-cart '''

               

            }   

        }

        

        stage('Build application') {

            steps {

               sh "mvn clean install -DskipTests=true"

            }   

        }

        

        stage('Build & Push Docker Image') {

            steps {

               script{

                   withDockerRegistry(credentialsId: '******************', toolName: 'docker') {

                         sh "docker build -t shopping:latest -f docker/Dockerfile ."

                         sh "docker tag shopping:latest 12041995/shopping:latest"

                         sh "docker push 12041995/shopping:latest "

              

                   }

                }

            }   

        }

        

        stage('Docker Deploy to Conatiner') {

            steps {

                script {

                    withDockerRegistry(credentialsId: '96832a6f-5f6f-45ca-93b4-118281d51ece', toolName: 'docker') {

                        sh "docker run -d --name shopping-cart -p 8070:8070 12041995/shopping:latest " }

                }        

            }

        }

    }

}

----------------------------------------!!!! Happy Learning with Techiev !!!!!!!!----------------------------------

-------------------------Subscribe our Youtube Channel by clicking the below link---------------------- ----------------------------!!https://www.youtube.com/@techieview729!!---------------------
































Name

AWS,14,Devops,24,linux,10,
ltr
item
Techie View: Devops Project no 1 - Automated CI/CD Pipeline for Java Application: Leveraging GitHub, Maven, SonarQube, and Jenkins in AWS
Devops Project no 1 - Automated CI/CD Pipeline for Java Application: Leveraging GitHub, Maven, SonarQube, and Jenkins in AWS
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA8K0IJfM6r_6tbjHlyKJxZDt1yFc7HXtGR4od9cRD83sKISTDpmSTbjg3cek27ySocDuPSXiQH5XlkVg8vAYwZ3UHoR2s3xvtp0o2LfMRSe3dS_wzuMQXg04CNPE3lC2ZabE5VIdWPZ2d2YZx38SXqiVZmPnJQmBGTA1nU4XziFmlk9kUWksW4Q58L0s_/w640-h232/project%20.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA8K0IJfM6r_6tbjHlyKJxZDt1yFc7HXtGR4od9cRD83sKISTDpmSTbjg3cek27ySocDuPSXiQH5XlkVg8vAYwZ3UHoR2s3xvtp0o2LfMRSe3dS_wzuMQXg04CNPE3lC2ZabE5VIdWPZ2d2YZx38SXqiVZmPnJQmBGTA1nU4XziFmlk9kUWksW4Q58L0s_/s72-w640-c-h232/project%20.png
Techie View
https://www.techiev.com/2023/11/devops-project-no-1-automated-cicd.html
https://www.techiev.com/
https://www.techiev.com/
https://www.techiev.com/2023/11/devops-project-no-1-automated-cicd.html
true
7013663511659419322
UTF-8
Loaded All Posts Not found any posts VIEW ALL View Full Article Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy