DevOps Project: Jenkins CI/CD for Kubernetes Deployments

  DevOps Tech Hub

116 Followers

Jenkins CI/CD Pipeline


Jenkins CI/CD Pipeline is the scripted way to create automated deployments. Below diagram depicts the Pipeline flow between the stages.

Jenkins Pipeline can be written in two modes:

  1. Scripted Pipeline
  2. Declarative Pipeline

Here I have used Declarative Pipeline syntax to configure Jenkins Pipeline to have application deployments to the kubernetes cluster.

Pipeline as Code (Declarative):

pipeline {

  agent any

  tools {

  maven 'maven'

  }

    stages {


      stage ('Checkout SCM'){

        steps {

          checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'git', url: 'https://dptrealtime@bitbucket.org/dptrealtime/jenkins.git']]])

        }

      }

      stage ('Build')  {

          steps {

            sh "cd  java-source"

            sh "mvn package"

          }

      }

     stage ('SonarQube Analysis') {

        steps {

              withSonarQubeEnv('sonar') {

                sh "cd  java-source"

        sh 'mvn -U clean install sonar:sonar'

              }

            }

      }


    stage ('Artifactory configuration') {

            steps {

                rtServer (

                    id: "jfrog",

                    url: "http://3.101.18.188:8082/artifactory",

                    credentialsId: "jfrog"

                )


                rtMavenDeployer (

                    id: "MAVEN_DEPLOYER",

                    serverId: "artifactory",

                    releaseRepo: "libs-release",

                    snapshotRepo: "libs-snapshot"

                )


                rtMavenResolver (

                    id: "MAVEN_RESOLVER",

                    serverId: "artifactory",

                    releaseRepo: "libs-release",

                    snapshotRepo: "libs-snapshot"

                )

            }

    }


    stage ('Deploy Artifacts') {

            steps {

                rtMavenRun (

                    tool: "maven", // Tool name from Jenkins configuration

                    pom: 'java-source/pom.xml',

                    goals: 'clean install',

                    deployerId: "MAVEN_DEPLOYER",

                    resolverId: "MAVEN_RESOLVER"

                )

         }

    }


    stage ('Publish build info') {

            steps {

                rtPublishBuildInfo (

                    serverId: "jfrog"

             )

        }

    }


    stage('Copy Dockerfile & Playbook to Ansible Server') {

            steps {

                  sshagent(['sshkey']) {

                        sh "scp -o StrictHostKeyChecking=no Dockerfile admin@54.164.221.87:/home/admin"

                        sh "scp -o StrictHostKeyChecking=no create-container-image.yaml admin@54.153.113.169:/home/admin"

                    }

                }

        } 

    stage('Build Container Image') {

            steps {

                  sshagent(['sshkey']) {

                        sh "ssh -o StrictHostKeyChecking=no admin@54.153.113.169 -C \"sudo ansible-playbook create-container-image.yml\""

                    }

                }

        } 

    stage('Copy Deployent & Service Defination to K8s Master') {

            steps {

                  sshagent(['sshkey']) {

                        sh "scp -o StrictHostKeyChecking=no create-k8s-deployment.yaml admin@54.164.221.87:/home/admin"

                        sh "scp -o StrictHostKeyChecking=no nodePort.yaml admin@54.153.113.169:/home/admin"

                    }

                }

        } 


    stage('Waiting for Approvals') {

        steps{


                input('Test Completed ? Please provide  Approvals for Prod Release ?')

              }

    }     

    stage('Deploy Artifacts to Production') {

            steps {

                  sshagent(['sshkey']) {

                        sh "ssh -o StrictHostKeyChecking=no admin@52.53.197.193 -C \"sudo kubectl apply -f create-k8s-deployment.yaml\""

                        sh "ssh -o StrictHostKeyChecking=no admin@52.53.197.193 -C \"sudo kubectl apply -f nodePort.yaml\""

                    }

                }

        } 

   } 

}

Now, Pipeline is deployed and Continuous Integration and Continuous Deployment flow is deployed to build and deploy application. 



Previous