Showing posts with label Jenkins. Show all posts
Showing posts with label Jenkins. Show all posts

Sunday, 8 November 2020

Creating CI/CD Pipeline for Deploying into the kubernetes using Jenkins.

Create the Pipeline script for the Following purpose.


1. Checkout the Code from Git.


2. Build it using the Maven.


3. Create the Docker Image.


4. Push it to the Docker Hub.


5. Deploy the Docker Image to the Kubernetes.


I am using Minikube and docker for the desktop in Windows 10 Machine.



1. Install the Plugin Kubernetes Continuous Deploy.


2. Configure the KubeConfig from Dashboard.


Navigate to Manage Jenkins > Manage Credentials > Jenkins > Global Credentials > Add Credentails 


In-Kind Select the Kubernetes  Configuration and Define the location from the Kube config.


C:\Users\Syed\.kube\config Then Save it.




3. Make Sure the YAML File is available in the location provided.


4.Create the Pipeline with the following syntax.


pipeline {

  environment {

    registry = "syedghouse14/greet-user-repo"

    registryCredential = 'Docker-Hub'

    dockerImage = ''

    dockerfile="${workspace}\\GreetUser\\Dockerfile"

    pomfile="${workspace}\\GreetUser\\pom.xml"

    JAR_FILE="target/*.jar"

  }

  agent any

  stages {

    stage('Cloning Git') {

      steps {

        git 'https://github.com/Syed-SearchEndeca/gretuser.git'

        

      }

    }

    stage ('Build') {

steps {

withMaven(maven : 'apache-maven-3.6.3') {

bat "mvn clean package -f ${pomfile}"

}

}

}

    stage('Building image') {

      steps{

        script {

          dockerImage = docker.build(registry + ":$BUILD_NUMBER",

          "--file ${dockerfile} --build-arg JAR_FILE=target/*.jar .")

        }

      }

    }

    stage('Deploy Image') {

      steps{

        script {

          docker.withRegistry( '', registryCredential ) {

            dockerImage.push()

          }

        }

      }

    }

 

    stage('Deploy on kubernetes') {

            steps {

                script {

                kubernetesDeploy(configs: "**/*.yaml", kubeconfigId: "KubeConfig")

            }

            }

        }

  }

}


The Project is available in the GIT download from here


Happy Learning !!!!

Downgrade the Jenkins Pipeline Plugin

 This is considered the important thing to be known for the developers who are working on the Jenkins and will be useful in most cases.


We will encounter some issues where some changes will not work as expected in that case if you were asked to downgrade the Jenkins plugin in order to make compatible to work with the Jenkins CI/CD.


Follow the below steps.


1.Navigate to https://plugins.jenkins.io/


2.Search the Plugin you need to search in the search box.


3. In My case Jackson 2 API




4. Select the corresponding plugins and click the archives. 


5. Select the version to which you need to downgrade to and download the hip file.




6. Navigate to Dashborad> Manage Plugins > Manage Plugins >Advance and select Choose File with the hip file download and Upload the plugin.




7. Here Select the Restart the Jenkins box. This will automatically be selected and gets downgraded after the restart.




8 In case if this does not happen then we need to Navigate to Dashboard > Manage Plugins > Manag Plugins > Installed and select the downgrade option.






9. Make Sure in some cased the dependent plugins will not start because of the version compatible issues. In those cases correct it by following the above approach.


Happy Reading.!!!!


Sunday, 1 November 2020

Creating Jenkins pipeline to Build and deploy the Docker Image

 To Understand the Jenkins pipeline and trigger build refer to my previous here


Prerequisite.


Install the Following plugins in Jenkins. navigating to ManageJenkins > ManagPlugins




Step1: Create the Docker credentials in Jenkins.


Navigate to Dashboard and  Select Manage Jenkins



Step2: Select Manage credentials.




Step:3 Create Credentials.

Select Stores scoped to Jenkins > Global Credentials > Add Credentials 

Make sure you give some Id to the credentials.


Step:4 Create Pipeline.


pipeline {

  environment {

    registry = "syedghouse14/greet-user-repo"

    registryCredential = 'Docker-Hub'

    dockerImage = ''

    dockerfile="${workspace}\\GreetUser\\Dockerfile"

    pomfile="${workspace}\\GreetUser\\pom.xml"

  }

  agent any

  stages {

    stage('Cloning Git') {

      steps {

        git 'https://github.com/Syed-SearchEndeca/gretuser.git'

        

      }

    }

    stage ('Build') {

steps {

withMaven(maven : 'apache-maven-3.6.3') {

bat "mvn clean package -f ${pomfile}"

}

}

}

    stage('Building image') {

      steps{

        script {

          dockerImage = docker.build registry + ":$BUILD_NUMBER",

          "--file ${dockerfile} ."

        }

      }

    }

    stage('Deploy Image') {

      steps{

        script {

          docker.withRegistry( '', registryCredential ) {

            dockerImage.push()

          }

        }

      }

    }

    stage('Remove Unused docker image') {

      steps{

        bat "docker rmi $registry:$BUILD_NUMBER"

      }

    }

  }

}


Step:5 Execute Success



Things to Consider


Make sure you pass the credentials created in the pipeline.

Pass the Docker File explicitly


 "--file ${dockerfile} ."


You will encounter the following error and resolution for that.

Step 4/5 : COPY GreetUser-0.0.1-SNAPSHOT.jar app.jar
COPY failed: stat /var/lib/docker/tmp/docker-builder619200988/GreetUser-0.0.1-SNAPSHOT.jar: no such file or directory

In One of the step it fails, For this edit the docker file as below.


Initial Docker file.


FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]


Modified Docker file.


FROM openjdk:8-jdk-alpine

RUN addgroup -S spring && adduser -S spring -G spring

USER spring:spring

ENTRYPOINT ["java","-jar","target/*.jar"]

Here I skipped the Copy Step Instead added the target directly. Hope this helps.

Happy Learning !!!!



Creating Jenkins Pipeline for the Maven Build

Step1: New Job in Jenkins

Create a Job in Jenkins

Login into Jenkins and select the New item from the Dashboard.


Step2: Create a Pipeline

Name the Item as Maven-Pipeline and Select the Pipeline from the drop-down.

Select Ok.


Step3: Adding Pipeline Code

Select the pipeline tab and add the below entries.




pipeline {
environment {
registry = "syedghouse14/greet-user-repo"
registryCredential = 'Docker-Hub'
dockerImage = ''
dockerfile="${workspace}\\GreetUser\\Dockerfile"
pomfile="${workspace}\\GreetUser\\pom.xml"
}
agent any
stages {
stage('Cloning Git') {
steps {
git 'https://github.com/Syed-SearchEndeca/gretuser.git'
}
}
stage ('Build') {
steps {
withMaven(maven : 'apache-maven-3.6.3') {
bat "mvn clean package -f ${pomfile}"
}
}
}
}
}

then select Save.

Step4: Select Build Now.




Step5: Success




Things to understand here.

I don't have the pom file defined directly hence defining like the below.

steps {
withMaven(maven : 'apache-maven-3.6.3') {
bat "mvn clean package -f ${pomfile}"
}

Here 

pomfile="${workspace}\\GreetUser\\pom.xml"

Where else the ${workspace} is my Jenkins workspace pointing to 

"C:\Users\Syed\AppData\Local\Jenkins\.jenkins\workspace\GreetUser"

Happy Learning !!!