Java CICD Project by using Jenkins.

Venkateswara Reddy - Dec 9 '23 - - Dev Community

Pre Requisites:
1.Jenkins
2.Maven
3.Tomcat
4.SonarQube
5.Nexus
6.Docker

start:

1.Ofter login the Jenkins 1st we have to install the plugin name called as jdk

Image description

2.Install these 2 plugins for jdk
3.click on create new job and enter name of the job then choose pipeline then create.
4.While configuring the Jenkins job always set discard old Builds as max 2. (best practice)
5.Under pipeline script choose hello world script then we can edit that script

Script for Git:

1.Github Access Token: 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
2.Click on pipeline syntax then next paze we have to select the step: git:Git & url: repo url & Branch: main/master & credentials enterusername and password by selecting the username and password and create one github-credntials.
3.Add that credentials under credentials then generate the script and copy and paste in the Jenkinsfile

Image description

Youtube video: https://youtu.be/AYohbnOqox0
4.While fetching data from github to Jenkins if the repository is public no need to provide any credentials.
5.If the repo is private we have to provide the credentials for that we have to create the access-token-key.
6.While generating Jenkins credentials we have to select username and password and under username we have to provide GitHub username only but under the password we have to provide the access token key what ever we generated. Then provide the decription and create the credentials for git-jenkins.

Image description

3.Whenever we give the private repo url we will get the error like this so we have to provide the credentials then this error will go.

MAVEN:

Package:
1.Whenever we are using new tool in our Jenkins we need to define that tool in the Jenkins by click on manage Jenkins global tool configuration.
2.Here we are configuring 3 tools those are java and SonarQube-scanner and maven.

Image description

3.Here under sonar-scanner we have to provide SonarQube name and maven name use default version for SonarQube and 3.60 for maven.
4.Under Jenkinfile we haveto add maven stage then run the build.
5.In maven if use command
“mvn clean” cleaning
“mvn test” unit test and
“mvn verify -DskipUnitTest” integration test.
“mvn package” package
6.So these are 2 types of build types in maven.

In the GitHub under src/ under test we have to write the test cases.
Based on that the ‘mvn test’ will run the test cases.
Under src/main/ code will be there.

Image description

This is here failed the 1 test. Actiually we wrote 1 test cases so its run 1 case and failed that test case.

SonarQube:

1.1st the SonarQube server should be upper running.
For SonarQube we have to install the plugin name called SonarQube scanner

Image description

2.Click on Configure system in Jenkins. Goto the SonarQube servers then click on add servers. Then we must enter name and URL and access token then we can configure.
3.While writing the Jenkins file we can use this configuration name or instead of configuration we can add entire script in the Jenkins script as well.
4.Totally we have to update in the 3 places:
configure system add all SQ details
Global tool configuration give SonarQube name & version automatically
Credentials by using the SQ token
YouTube video : https://youtu.be/wn9wWYAShag

Image description

Image description

Image description

1.Sonar-Jenkins(token): squ_94ff17e07af24fabdc5747b690380a84c7e32882
2.


pipeline {
    agent any
    
    tools{
        maven 'maven'
    }

    stages {
        stage('Github') {
            steps {
               git branch: 'main', credentialsId: '99b694d8-d815-4d70-afcc-5dcf2b4b0ae8', url: 'https://github.com/venkyy8/ShoppingCart-Project.git'
            }
        }
        
         stage('package') {
            steps {
               sh "mvn clean compile"
            }
         }
         stage('SonarQube analysis') {
           steps{
              withSonarQubeEnv('sonar-jenkins') { 
                 sh "mvn sonar:sonar"
                 }
             }
        }
        stage('Build Application') {
            steps {
               sh "mvn package"
            }
         }
    }
}

Enter fullscreen mode Exit fullscreen mode

4.In the SonarQube for pipeline syntax we can use like below.

Image description

5.Here credentials id what ever we provide while configuring the credentials that id will come here I have not provided any id so its default one no worries.
6.Step we have to choose SonarQube scanner then generate the code and add “mvn sonar:sonar” in that code. Then run the job.
7.In the sonar qube if we are using different server under global setting under sonarqube we have to change the url.

Image description

Image description

SonarQube Quality Gate:

1.Ofter static code check we have to do quality gate analysis.
2.Static code code only checking the code(send to sonarqube) but quality gate is to check the code meets the expectations or not.
3.For that we have to install the tools like as shown in the below those are quality gates.

Image description

4.Ofter installing these tools we have to generate or create script for jenkinsfile quality gate purpose.
5.Search for quality gate in the state of the pipeline syntax and give credentials then generate the code and paste in the Jenkins file.

Image description

8.Here actually we are sending the code to sonarqube for quality gate purpose.
9.When ever our code not meet the expectations the sonarqube will send back status to the Jenkins.
10.For that we have to configure webhook in the sonarqube.
11.Click on administration then configure then choose webhook and enter name of the webhook and under the url we have to provide the Jenkins url (http://197.89.76.54:8080/) (from chrome we can get this url) and ofter this / we have enter sonarqube-wehook/

Image description

6.Then click on create secret is not required.

Quality Gates differences:

1.From Jenkins to Sonarqube normally sending code for status of code that will check the status just
2.Even if we set the quality gate to that project in the Jenkins the step will pass but if the code not meet target that project will fail in the sonarqube but in Jenkins running success.
3.If we want to failure our Jenkins job in the Jenkins also when ever the code not meet the target in that time we have to set target gate. For that we need to create new stage in the Jenkins.

Youtube : https://youtu.be/Lh8jC4vDgsE

Attaching the quality gate to project:

1.We have to create one quality gate by adding the targets like coverage 80.
2.Usually the code coverage will come around 0.0% like but if we create the quality gate with any specific target then create QG.
3.Ofter creating the QG open the project then there we have to change the quality gate from default to custom gate what ever we created.

Image description

UNIT TEST TYPES:

1.Unit test
2.Integration tests
3.Sonar scanner analysis
4.Quality gates
5.Junit Reports
6.Surefire-reports
7.Test classes or cases.

Command for these tests:

1.Mvn test: unit tests
2.Mvn verify -DskipUnitTest : integration tests
3.Sonar scanner analysis

3.
stage('SonarQube analysis') {
           steps{
              withSonarQubeEnv('sonar-jenkins') { 
                 sh "mvn sonar:sonar"
                 }
Enter fullscreen mode Exit fullscreen mode

Quality gates:


stage("sonar Quality Gate") {
            steps {
                 script {
                     timeout(time: 1, unit: 'HOURS') {
                         def qg = waitForQualityGate()
                         if (qg.status != 'OK') {
                             error "Pipeline aborted due to quality gate failure: ${qg.status}"
                         } else {
                             echo "Success: ${qg.status}"
                         }
                     }

Enter fullscreen mode Exit fullscreen mode

5.JUnit is a unit testing open-source framework for the Java programming language.

Tomcat:

1.Search for plugin in the Jenkins ‘Deploy to container’

Image description

2.Here our .jar file created in Jenkins server but tomcat is in different server. So to copy from Jenkins to tomcat server for that we need SSH agent plugin in the Jenkins.
3.By using deploy to container plugin we can achieve that job but that is freestyle and manual job but for pipeline job we have to use ssh agent.
4.So for pipeline we have to install ssh agent in the plugins page.

Image description

ssh -i "virginia1.pem" ubuntu@ec2-34-230-72-86.compute-1.amazonaws.com
ssh -i "virginia1.pem" ubuntu@34.230.72.86
cat /opt/venky.pem >> ~/.ssh/authorized_keys

in place of “virgina1” we have to give path if we are not in that path.
command to connec to ec2 server
1.we have to create one credential for tomcat for that click on credentials then select SSH username with private key.

Image description

Image description

You tube video: https://youtu.be/G_UCeeb5EPc

2.for tomcat we have to create Jenkins groovy script. So click on pipeline syntax then here we have to choose the ssh agent then credentials then generate the code.

Image description

EX:2


stage('tomcat') {
            steps {
               sshagent(['tomcat-ID']) {
                sh "scp -o StrictHostKeyChecking=no /var/lib/jenkins/workspace/ci_cd/target/k8s-1.jar ubuntu@34.230.72.86:/opt/tomcat/webapps"
                }
            }
        }

Enter fullscreen mode Exit fullscreen mode

1.so when ever we stop and start the tomcat in the pipeline Jenkinsfile script under tomcat public ip place we have to change the ip address.


pipeline {
    agent any
    tools{
        maven 'maven'
    }
    stages {
        stage('Github') {
            steps {
               git branch: 'main', credentialsId: '99b694d8-d815-4d70-afcc-5dcf2b4b0ae8', url: 'https://github.com/venkyy8/cicd-0808.git'
            }
        }
        stage('unit test') {
            steps {
               sh "mvn clean test"
            }
        }
        stage('integration test') {
            steps {
               sh "mvn verify -DskipUnitTest"
            }
        }
        stage('Build Application') {
            steps {
               sh "mvn package compile install"
            }
        }
        stage('SonarQube analysis') {
          steps{
             withSonarQubeEnv('sonar-jenkins') { 
                sh "mvn sonar:sonar"
                }
            }
        }
        // stage("sonar Quality Gate") {
        //     steps {
        //         script {
        //             timeout(time: 1, unit: 'HOURS') {
        //                 def qg = waitForQualityGate()
        //                 if (qg.status != 'OK') {
        //                     error "Pipeline aborted due to quality gate failure: ${qg.status}"
        //                 } else {
        //                     echo "Success: ${qg.status}"
        //                 }
        //             }
        //         }
        //     }
        // }
        // stage('quality-gates') {
        //   steps{
        //         waitForQualityGate abortPipeline: false, credentialsId: 'f460628e-8b26-47c4-b381-f7c9b90c2c03'
        //     }
        // }
        // stage('tomcat') {
        //     steps {
        //       sshagent(['tomcat-ID']) {
        //         sh "scp -o StrictHostKeyChecking=no /var/lib/jenkins/workspace/ci_cd/target/k8s-1.jar ubuntu@34.230.72.86:/opt/tomcat/webapps"
        //         }
        //     }
        // }
    }
}
Enter fullscreen mode Exit fullscreen mode

NEXUS:

1.In Jenkins tools and plugins we need to install “nexus artifact uploader”.
2.We have to install the nexus in ec2 then after open that nexus click on settings and click on repositories.

CREATION OF REPO:

1.We can create new repository then type we can select the maven hosted type repo.

Image description

2.Then we have to enter the name of the repo then select the version type as mixed. Here mixed type means it will take jar war and all type of artifacts.

Image description

3.Policy we can chose as below.

Image description

4.Deployments policy is allow redeployments

Image description

5.Then we can click on create new repository.

USERS:
1.click on the users then enter all fields.

Image description

Image description

2.then we have to give admin roles.

Image description

3.then finally we can click on create local user.

NEXUS_JENKINS:

1.1st create credentials for nexus and Jenkins
2.Choose username and password and enter username as admin and password as password then create the credentials.


pipeline {
    agent any
    tools {
        maven 'maven'
    }
    environment {
        NEXUS_VERSION = "nexus3"
        NEXUS_PROTOCOL = "http"
        NEXUS_URL = "3.95.187.183:8081" // Note: Added 'http://' protocol
        NEXUS_REPOSITORY = "nexus-repo-maven-jenkins"
        NEXUS_CREDENTIAL_ID = "nexus-id"
    }
    stages {
        stage('Github') {
            steps {
                git 'https://github.com/venkyy8/java-web-app-docker.git'
            }
        }
        stage('unit test') {
            steps {
                sh "mvn clean test"
            }
        }
        stage('integration test') {
            steps {
                sh "mvn verify -DskipTests=true" // Corrected the option name
            }
        }
        stage('Build Application') {
            steps {
                sh "mvn package compile install"
            }
        }
        // stage('SonarQube analysis') {
        //     steps {
        //         script {
        //             withSonarQubeEnv('sonar-jenkins') {
        //                 sh "mvn sonar:sonar"
        //             }
        //         }
        //     }
        // }
        // stage('tomcat') {
        //     steps {
        //         sshagent(['tomcat-ID']) {
        //             sh "scp -o StrictHostKeyChecking=no /var/lib/jenkins/workspace/ci_cd/target/java-web-app*.war ubuntu@3.82.113.172:/opt/tomcat/webapps"
        //         }
        //     }
        // }
        stage('docker build') {
            steps {
                sh "docker build -t venkyy82/venky:${BUILD_NUMBER} ."
            }
        }
        stage('docker push') {
            steps {
                script {
                    withCredentials([string(credentialsId: 'docker2-id', variable: 'dockerpwd')]) {
                        sh "docker login -u venkyy82 -p ${dockerpwd}"
                        sh "docker push venkyy82/venky:${BUILD_NUMBER}"
                    }
                }
            }
        }
        stage("Publish to Nexus Repository Manager") {
            steps {
                script {
                    pom = readMavenPom file: "pom.xml";
                    filesByGlob = findFiles(glob: "target/*.${pom.packaging}");
                    echo "${filesByGlob[0].name} ${filesByGlob[0].path} ${filesByGlob[0].directory} ${filesByGlob[0].length} ${filesByGlob[0].lastModified}"
                    artifactPath = filesByGlob[0].path;
                    artifactExists = fileExists artifactPath;
                    if (artifactExists) {
                        echo "*** File: ${artifactPath}, group: ${pom.groupId}, packaging: ${pom.packaging}, version ${pom.version}";
                        nexusArtifactUploader(
                            nexusVersion: 'nexus3',
                            protocol: 'http',
                            nexusUrl: '3.95.187.183:8081',
                            groupId: 'pom.com.mt',
                            version: '1.0',
                            repository: 'nexus-repo-maven-jenkins',
                            credentialsId: 'nexus-id',
                            artifacts: [
                                [artifactId: 'pom.java-web-app',
                                classifier: '',
                                file: artifactPath,
                                type: pom.packaging],
                                [artifactId: 'pom.java-web-app',
                                classifier: '',
                                file: "pom.xml",
                                type: "pom"]
                            ]
                        );
                    } else {
                        error "*** File: ${artifactPath}, could not be found";
                    }
                }
            }
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

this is sprint boot application Jenkins file.


pipeline {
    agent any
    
    tools{
        maven "maven3.9.4"
    }

    stages {
        stage('Hello') {
            steps {
                git 'https://github.com/pramodbs543/spring-boot-mongo-docker.git'
            }
        }
        stage("maven build"){
            steps{
                sh "mvn clean package"
            }
        }
        stage("build docker image"){
            steps{
                sh "docker build -t chandandocker1995/springbootapp:${BUILD_NUMBER} ."
            }
        }
             stage("docker login"){
            steps{
              withCredentials([string(credentialsId: 'docker_cred', variable: 'docker_cred')]) {
    sh "docker login -u chandandocker1995 -p ${docker_cred}"
    sh "docker push chandandocker1995/springbootapp:${BUILD_NUMBER}"
              }
             }
        }
        stage("deploy to another server"){
            steps{
                sshagent(['secret_key']) {
    // sh "ssh -o StrictHostKeyChecking=no ubuntu@172.31.14.42 docker rm -f springbootcontainer || true"
    // sh "ssh -o StrictHostKeyChecking=no ubuntu@172.31.14.42 docker run -d -p 8081:8080 --name springbootcontainer chandandocker1995/springbootapp:1"
     sh "ssh -o StrictHostKeyChecking=no ubuntu@172.31.14.42 sudo rm -rf /opt/compose || true"
     sh "ssh -o StrictHostKeyChecking=no ubuntu@172.31.14.42 sudo mkdir /opt/compose"
     sh "ssh -o StrictHostKeyChecking=no ubuntu@172.31.14.42 sudo chmod 777 /opt/compose"
     sh "scp -o StrictHostKeyChecking=no /var/lib/jenkins/workspace/spring-boot-application/docker-compose.yml ubuntu@172.31.14.42:/opt/compose/compose.yml"
                       sh '''
    ssh -o StrictHostKeyChecking=no ubuntu@172.31.14.42 sudo apt-get update
    ssh -o StrictHostKeyChecking=no ubuntu@172.31.14.42 sudo docker-compose -f /opt/compose/compose.yml down || true
    ssh -o StrictHostKeyChecking=no ubuntu@172.31.14.42 sudo docker-compose -f /opt/compose/compose.yml up -d
    
'''       
            }
                
            }
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

3.We have to generate the script by using nexus artifacts generator step ofter that we have to enter the all details then generate the script.



stage("Publish to Nexus Repository Manager") {
    steps {
        script {
            nexusArtifactUploader(
                artifacts: [
                    [artifactId: 'java-web-app', classifier: '', file: '/target/java-web-app-1.0.war', type: 'war']
                ],
                credentialsId: 'nexus-id',
                groupId: 'com.mt',
                nexusUrl: '44.210.144.192',
                nexusVersion: 'nexus3',
                protocol: 'http',
                repository: 'nexus-repo-maven-jenkins',
                version: '1.0'
            )
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

3.Here is the link utube: https://youtu.be/p_Wo3aqUJto


stage("Publish to Nexus Repository Manager") {
           steps {
              script {
                nexusArtifactUploader(
                   artifacts: [
                     [artifactId: 'java-web-app', classifier: '', file: '/var/lib/jenkins/workspace/ci_cd/target/java-web-app-1.0.war', type: 'war']
                ],
                credentialsId: 'nexus-id',
                groupId: 'com.mt',
                nexusUrl: '44.210.144.192:8081',
                nexusVersion: 'nexus3',
                protocol: 'http',
                repository: 'nexus-repo-maven-jenkins',
                version: '1.0'
            )
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

6.Here under target path we have to give full path of the target file

Image description

Image description

@@@@@@@@@@@
com.mt
java-web-app
war
1.0
@@@@@@@@

PULL ARIFACTS FROM NEXUS TO EC2:

1.sh 'wget http://44.210.144.192:8081/repository/nexus-repo-maven-jenkins/com/mt/java-web-app/108/java-web-app-108.war'
2.if use shell script like this we can download this .war
3.

Docker:

1.In the Jenkins install the plugin called docker pipeline.

Image description


FROM openjdk:8-jdk-alpine
WORKDIR /app
#COPY ./target/*.jar /app.jar
#COPY /var/lib/jenkins/workspace/ci_cd/target/k8s-1.jar /app.jar
#COPY . /app.jar
#COPY k8s-1.jar .
COPY my-app-1.0-SNAPSHOT.war .
EXPOSE 9090
#CMD ["java", "-jar", "app.jar"]
#CMD ["java", "-jar", "k8s-1.jar"]
CMD ["java", "-jar", "my-app-1.0-SNAPSHOT.war"]

Enter fullscreen mode Exit fullscreen mode

2.Install the Docker build and publish and after installation we have to configure that tool. Under manage Jenkins under configure tools under docker give name of the docker and version we can give automatically.
3.To push the image to the docker hub which image we need to use is “docker pipeline”.

Image description

1.Here for pushing our docker image to docker hub we have to write the code for that we have to use this step and configure our docker credentials in the credentials section and choose that one here.
2.As shown in the above image we have to select the binding credentials then from the drop-down menu we have to select the secret text. Then next page will come as shown in the below.

Image description

3.Here under variable name we can give any name and ofter that click on that add and try to create our credential. There also we have to select secret text and under secret we have to provide password of docker and no need to provide username. Then ofter that id and description as our wish we can enter any name.
4.Then choose the what ever we created the secret test and then generate the pipeline syntax. Then copy and paste it in Jenkins file.


stage ('docker push') {
            steps {
                script {
                   withCredentials([string(credentialsId: 'docker2-id', variable: 'dockerpwd')]) {
                      sh 'docker login -u venkyy82 -p ${dockerpwd}'
               } 
            sh 'docker push venkyy82/venky'   
                }
            }
        }
Enter fullscreen mode Exit fullscreen mode

4.In the pipeline we have to write like this code.
5.Here ‘docker2-id’ is my credentials configuration and dockerpwd is while generating script I wrote that ‘dockerpwd’
6.Here in docker mainly we need several plugins for docker stsge those are:
Docker : 1st we need to install plugin for Deamon.
Docker-build-step: for build the docker images.
Cloudbees docker build and publish: for publish our images into docker hub
Docker pipeline : for pushing images continuously


pipeline {
    agent any
    tools {
        maven 'maven'
    }
    stages {
        stage('Github') {
            steps {
                git 'https://github.com/MithunTechnologiesDevOps/java-web-app-docker.git'
            }
        }
        stage('unit test') {
            steps {
                sh "mvn clean test"
            }
        }
        stage('integration test') {
            steps {
                sh "mvn verify -DskipUnitTest"
            }
        }
        stage('Build Application') {
            steps {
                sh "mvn package compile install"
            }
        }
        stage('docker build') {
            steps {
                sh "docker build -t venkyy82/venky:${BUILD_NUMBER} ."
            }
        }
        stage('docker push') {
            steps {
                script {
                    withCredentials([string(credentialsId: 'docker2-id', variable: 'dockerpwd')]) {
                        sh "docker login -u venkyy82 -p ${dockerpwd}"
                        sh "docker push venkyy82/venky:${BUILD_NUMBER}"
                    }
                }
            }
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

2.Here is the script to build our application docker file and push our docker build into docker hub.
3.https://github.com/MithunTechnologiesDevOps/java-web-app-docker.git this is the link for project.
4.Youtube link : https://youtu.be/hHZoV3LBIwE mithun technologis.
5.If we want to communicate Jenkins server with other server for like deploy tomcat and deploy our image into another server we need SSH AGENT.


pipeline {
    agent any
    tools {
        maven 'maven'
    }
    stages {
        stage('Github') {
            steps {
                git 'https://github.com/venkyy8/java-web-app-docker.git'
            }
        }
        stage('unit test') {
            steps {
                sh "mvn clean test"
            }
        }
        stage('integration test') {
            steps {
                sh "mvn verify -DskipTests=true"
            }
        }
        stage('SonarQube analysis') {
            steps {
                script {
                    withSonarQubeEnv('sonar-jenkins') {
                        sh "mvn sonar:sonar"
                    }
                }
            }
        }
        stage('Build Application') {
            steps {
                sh "mvn package compile install"
            }
        }
        stage('tomcat') {
            steps {
                sshagent(['tomcat-ID']) {
                    sh "scp -o StrictHostKeyChecking=no /var/lib/jenkins/workspace/ci_cd/target/java-web-app*.war ubuntu@3.82.113.172:/opt/tomcat/webapps"
                }
            }
        }
        stage("Publish to Nexus Repository Manager") {
            steps {
                script {
                    nexusArtifactUploader(
                        artifacts: [
                            [artifactId: 'java-web-app', classifier: '', file: '/var/lib/jenkins/workspace/ci_cd/target/java-web-app-1.0.war', type: 'war']
                        ],
                        credentialsId: 'nexus-id',
                        groupId: 'com.mt',
                        nexusUrl: '44.210.144.192:8081',
                        nexusVersion: 'nexus3',
                        protocol: 'http',
                        repository: 'nexus-repo-maven-jenkins',
                        version: "${BUILD_NUMBER}"
                    )
                }
            }
        }
        stage('docker build') {
            steps {
                sh "docker build -t venkyy82/venky:${BUILD_NUMBER} ."
            }
        }
        stage('docker push') {
            steps {
                script {
                    withCredentials([string(credentialsId: 'docker2-id', variable: 'dockerpwd')]) {
                        sh "docker login -u venkyy82 -p ${dockerpwd}"
                        sh "docker push venkyy82/venky:${BUILD_NUMBER}"
                    }
                }
            }
        }
        stage('push to  ecr') {
            steps {
                sh "aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/i4w3s2p5"
                //sh "docker build -t demo-ecr ."
                sh "docker tag venkyy82/venky:${BUILD_NUMBER} public.ecr.aws/i4w3s2p5/demo-ecr:${BUILD_NUMBER}"
                sh "docker push public.ecr.aws/i4w3s2p5/demo-ecr:${BUILD_NUMBER}"
            }
        }

    }
}



Enter fullscreen mode Exit fullscreen mode

DOCKER IMAGE PUSH INTO ECR:

1.Wherever the Jenkins working in the ec2 that instance should have permission to upload/communicate with ECR in aws.
2.‘Amazonec2containerregistryfullaccess’
3.So 1st we have to assign the role to the ec2 instance
4.Then create one ECR in the aws for hosting docker images.
5.8096 TCP port is required to open for firewall protection.


stage('docker build') {
            steps {
                sh "docker build -t venkyy82/venky:${BUILD_NUMBER} ."
            }
        }
        stage('docker push') {
            steps {
                script {
                    withCredentials([string(credentialsId: 'docker2-id', variable: 'dockerpwd')]) {
                        sh "docker login -u venkyy82 -p ${dockerpwd}"
                        sh "docker push venkyy82/venky:${BUILD_NUMBER}"
                    }
                }
            }
        }
        stage('push to  ecr') {
            steps {
                sh "aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/i4w3s2p5"
                //sh "docker build -t demo-ecr ."
                sh "docker tag venkyy82/venky:${BUILD_NUMBER} public.ecr.aws/i4w3s2p5/demo-ecr:${BUILD_NUMBER}"
                sh "docker push public.ecr.aws/i4w3s2p5/demo-ecr:${BUILD_NUMBER}"
            }
        }
Enter fullscreen mode Exit fullscreen mode

5.In the ec2 we should install aws cli then only the ec2 will communicate with ecr.

Image description

6.These are the ecr steps to to push our image to ecr.

Shared Library:

Uou tube reference: https://youtu.be/BLQ0PDjgN8w
1.In the Jenkins we have to set the settings.

Image description

Image description

2.Here shared library we have to give any random name.
3.Then under version we can give like v1 but better to enter branch name.
4.Then ofter let all default and then select source code as git and then enter the name of the repo url.
5.If credentials are there means we can enter here also.
6.When ever we mention this repository the shared library will start checking for the / /var directory.
7.Then let the default settings as it is and apply and save
8.This is configuration of shared library.

Creating Jenkins job:
Here under groovy syntax we have start writing with shared library like below.

@Library("my-shared-Library") _
pipeline {
agent any
tools {

Here we are using ‘_ ‘ why because all shared libraries are there in the same directory
Ofter that under pipline script we have to replace the script with just file name.

Qubernetes:

In Jenkins install the plugin called ‘kubernetes continuous Deploy’

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

1.Code commit
2.Test
3.Code analysys
4.Build
5.Deploy
6.Integration tests
7.Approval before sending the app to production
8.Deploy to production
9.Monitoring and logging
10.

1.Code Commit: Developers commit their code changes to a version control system (e.g., Git).
2.Build: The CI server automatically triggers a build process to compile the source code, run tests, and generate artifacts.
3.Test: Automated tests are executed to ensure that the code changes meet the required quality standards.
4.Code Analysis: Static code analysis tools check the code for potential issues, vulnerabilities, and adherence to coding standards.
5.Artifact Generation: The build process creates deployable artifacts or packages that are ready for deployment.
6.Deploy to Staging: The application is deployed to a staging environment that closely resembles the production environment.
7.Integration Tests: Additional tests are performed in the staging environment to verify the application's behavior in a more realistic setting.
8.Approval: Manual approval may be required at this stage to allow the promotion of the changes to the production environment.
9.Deploy to Production: Once approved, the application is automatically deployed to the production environment.
10.Monitoring and Logging: Monitoring tools collect data and logs from the production environment to track application performance and detect issues.
11.Continuous Monitoring: Continuously monitor the application in production to identify and address any issues that arise.
12.Feedback Loop: Collect feedback from users and stakeholders to inform future development iterations.

Jenkins:

1.Create one t2.large ec2 instance
2.Login the ec2 instance
3.Java is prerequisite for Jenkins. Why because Jenkins is developed by using java code.
sudo apt update
sudo apt install openjdk-11-jre

Installation:

1.Create 1 VPC and 2 Subnet and 1 SG and 1 RouteTable and 1 IGW.
2.

@@@@@@@@@@@@@@@@@@@@

Notes:

1.for sonarqube pre requisite is java
2.while 1st time login time sonarqube we should use admin as username and password also admin ofter next paze it will ask us to rename password.
3.to change the hostname in ec2 cli we can use the command 'hostname tomcat' but should be in the root user not in the sudo user. if we are in the sudo user we cant change the hostname ofter changing/using the commnad 'hostname tomcat' we no need to do shutdown/restart or close/open the ec2 cli just simply ofter using that command we have to use the command sudo su then the hostname will change.
4.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

. . . . . . . . . . . . .