Jenkins Pipeline code CI part using Code commit, AWS ECR, Jenkins, maven, sonarqube, docker

·

3 min read

Today I successfully developed an end-to-end CI/CD pipeline for deploying Java applications using Amazon EC2, AWS code commit, Maven, SonarQube, Jenkins, Docker, ECR

The project involves building and deploying a Java application using a CI/CD pipeline. Here are the steps involved:

Version Control: The code is stored in a version control system such aws code commit. The code is organized into branches such as the main or development branch.
Continuous Integration: Jenkins is used as the CI server to build the application. Whenever there is a new code commit, Jenkins automatically pulls the code from the code commit and builds it using Maven.
Code Quality: SonarQube is used to analyze the code and report on code quality issues such as bugs, vulnerabilities, and code smells. The SonarQube analysis is triggered as part of the Jenkins build pipeline.
Containerization: Docker is used to containerize the Java application. The Dockerfile is stored in the Git repository along with the source code. The Dockerfile specifies the environment and dependencies required to run the application.
Container Registry: The Docker image is pushed to ECR, a public or private Docker registry. The Docker image can be versioned and tagged for easy identification.

Overall, this project demonstrates how to integrate various tools commonly used in software development to streamline the development process, improve code quality, and automate deployment.

JENKINS PIPELINE

pipeline{
    agent{
        docker{
            image 'abhishekf5/maven-abhishek-docker-agent:v1'
            args '--user root -v /var/run/docker.sock:/var/run/docker.sock' //·mount·Docker·socket·to·access·the·host's·Docker·daemon
        }
    }
    environment {
        AWS_ACCOUNT_ID="386288228052"
        AWS_DEFAULT_REGION="eu-west-1" 
        IMAGE_REPO_NAME="inspectorv2"
        IMAGE_TAG="${BUILD_NUMBER}"
        REPOSITORY_URI = "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${IMAGE_REPO_NAME}"
        //REPOSITORY_URI = "public.ecr.aws/q8g4y1x9/${IMAGE_REPO_NAME}"
        //git config --global credential.helper "!aws.cmd codecommit credential-helper $@"
        //git config --global credential.UseHttpPath true
    }
    stages {
        stage('Checkout') {
            steps {
                sh 'echo passed'
                //git config --global credential.helper "!aws.cmd codecommit credential-helper $@"
                //git config --global credential.UseHttpPath true
                checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: 'CICD-GITusercredforcodecommit1', url: 'https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/CICD-Pipeline']])

            }
        }
        stage('Build and Test') {
            steps{

                //build the project and create JAR file
                sh 'cd java-maven-sonar-argocd-helm-k8s/spring-boot-app && ls -ltr'
                sh 'cd java-maven-sonar-argocd-helm-k8s/spring-boot-app && mvn -X clean package'
                sh 'cd java-maven-sonar-argocd-helm-k8s/spring-boot-app && ls -ltr'
            }
        }
        stage('Static Code Analysis') {
            environment {
                SONAR_URL = "http://34.241.36.33:9000"
                      }
            steps {
                withCredentials([string(credentialsId: 'sonarqube', variable: 'SONAR_AUTH_TOKEN')]) {
                    sh 'cd java-maven-sonar-argocd-helm-k8s/spring-boot-app && mvn sonar:sonar -Dsonar.login=$SONAR_AUTH_TOKEN -Dsonar.host.url=${SONAR_URL}'
                    }
                    }
        } 

        stage('Building image') {
            steps{
                script {
                    //dockerImage = docker.build "${IMAGE_REPO_NAME}:${IMAGE_TAG}"
                    //sh 'echo $dockerImage'

                    sh 'cd java-maven-sonar-argocd-helm-k8s/spring-boot-app && docker build -t ${IMAGE_REPO_NAME}:${IMAGE_TAG}  .'
                }
            }
        }
        stage('Pushing to ECR') {
            steps{ 

                withDockerRegistry(credentialsId: 'ecr:eu-west-1:CICD-ECRUser', url: 'https://<<ACCOUNTID>>.dkr.ecr.eu-west-1.amazonaws.com') {
                    script {
                        sh "docker tag ${IMAGE_REPO_NAME}:${IMAGE_TAG} ${REPOSITORY_URI}:$IMAGE_TAG"
                        sh "docker push  <<accountid>>.dkr.ecr.eu-west-1.amazonaws.com/${IMAGE_REPO_NAME}:${IMAGE_TAG}"
                        sh 'echo "pushed successfulyyy"'
                    }
                 }
            }
        }
        stage('Update Deployment File') {
                steps {
                    script {

                       // sh 'git remote add codecommit https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/CICD-Pipeline'

                        sh '''

                        BUILD_NUMBER=${BUILD_NUMBER}

                        ls -ltr
                        pwd

                        sed -i "s/replaceImageTag/${BUILD_NUMBER}/g" /var/lib/jenkins/workspace/UltimateCICD/java-maven-sonar-argocd-helm-k8s/spring-boot-app-manifests/deployment.yaml
                        git config --global user.email "kakarlayogitha@gmail.com"
                        git config --global user.name "yogitha"

                        git add java-maven-sonar-argocd-helm-k8s/spring-boot-app-manifests/deployment.yaml
                        git commit -m "Update deployment image to version ${BUILD_NUMBER}"
                        git push codecommit main
                        '''
                        }
                        }
                        }                  


}
}

Resources:-

https://lnkd.in/eaqjYfPu

https://lnkd.in/eG5HKrnB

Thank you Abhishek Veeramalla for this project and your detailed YouTube Video.

Thank you so much Sunita sonawane for your detailed blog which motivated me to try this. I have used AWS code commit instead of GitHub and ecr instead if docker hub