Jenkins

 

Quality Thought Videos by Sanjay Videos by Sunil IntelliQit
1. installing jenkins class notes 16/dec/2019 Jenkins introduction Jenkins intro & installation, java, git, maven, jenkins  Intro jenkins & CICD,
2. jenkins project class notes 17/dec/2019 Jenkins Installation on Linux 3 Tier-Tomcat Install, CICD stage 1,2,3  installation jenkins & tomcat,
3. configuring jenkins class notes 18/dec/2019 Jenkins Installation on Windows CICD stage 4, 5 continuous testing and delivery  Freestyle project with CICD stage1,2,3
4. building code & unit testing class notes 19/dec/2019 Jenkins Installation on Docker Container Jenkins user administration  contd. Freestyle project with CICD Stage4,5
5. maven installation class notes 20/dec/2019 Integrate Jenkins with Git-Hub Master Slave Configuration  user administration and jenkins job scheduling
6. testing, upstream/downstream, plugins class notes 22/dec/2019 morning Project with Ansible scriptive pipeline with groovy script  Master Slave setup
7. Build Triggers, distributed/master-node class notes 22/dec/2019 evening Different jobs on CI and CD Multi branch pipeline  Scriptive Pipeline with jenkins file using groovy
8. Adding Ubuntu node to Jenkins Master class notes 23/dec/2019 Jenkins Pipeline with Groovy Script Email Notification  Declarative Pipeline project
9. Adding windows node to Jenkins Master class notes 24/dec/2019      Declarative Pipeline post condition project
10. jenkins pipeline class notes 25/dec/2019      Multibranch project
11. jenkins pipeline class notes 26/dec/2019      
12. scripted pipeline class notes 29/dec/2019 morning      
13. integ artifactory and sonarqube class notes 29/dec/2019 evening      
14. infra provisioning        
15. git flow        

 

 

 


 

Project with Ansible: Role of Jenkins:

        -hosts:all

          tasks:

            -copy:

                src: /opt/index.html

                dest: /vat/www/html


 

 

 

 


 CICD Pipeline using scripted method with groovy script (jenkins file):

Syntax of groovy script:

node ( 'master/slave')
{
         stage(' Stage in CI-CD')
         {
            Groovy code for implementing the stage
         }
}
  1. Stage1: Continuous Download:
  2. Stage2: Continuous Build:
  3. Stage3: Continuous Deploy:
  4. Stage4: Continuous Test:
  5. Stage5: Dontinuous Delivery:

Plugin: Install  Build Pipeline

Allocate Node:

  1. New item : scriptedpipeline1 > select pipeline
  2. Go to pipeline tab and write script.  click pipeline syntax, it open snippet generator browser tab (generate code), sample step search= node,
  3. Node:Allocate node (where you want job to run on master or slave node), define in label
  4. Label : master & click generate script.
  5. copy script and paste it in job script tab.

Stage1: Define Stage: Continuous Download

  1. In snippet generator search stage, enter stage name (continuous download), click generate code
  2. copy code and paste it in job.
node('master')
{
     stage('continuous download')
       {
          // some block
      }
}
  1. In snippet generator search git and enter gitclone path and click generate code.  (https://github.com/sunildevops77/maven.git)
  2. copy and paste it in job.
node('master')
{
     stage('continuous download')
      {
              git 'https://github.com/sunildevops77/maven.git'
     }
}
  1. Apply and save.
  2. Go to dashboard and run
  3. Continuous download stage is completed.

Stage2: Continuous Build:

  1. In snippet generator search SH shell script.
  2. write mvn package and click generate
  3. copy code , go to job configure> pipeline code> paste
node('master')
{
         stage('continuous download')
            {
                git 'https://github.com/sunildevops77/maven.git'
            }
                 stage('continuous build')
                      {
                              sh 'mvn package'
                       }
}
  1. Apply and save, build the code.
  2. it will run both continuous download and continuous build.
  3. artifact is created, click log to check location.

Stage3 - Continuous Deployment:

Note: To perform deployment using pipeline, need to create passwordless communication between master(development) and qa.  In freestyle project we install deploy to container plugins.

  1. Go to qa server
  2. #whoami (ubuntu)
  3. #sudo passwd ubuntu: enter password
  4. #sudo vi /etc/ssh/sshd_config (PassworAuthentication yes) save and exit
  5. #sudo service ssh restart.
  6. Go to dev server
  7. #ssh-keygen
  8. #ssh-copy-id ubuntu@privateIP_qa (enter password of ubuntu user in qa)
  9. #ssh ubuntu@privateip_qa (no password prompted)
  10. to copy file within a system we use cp command while to copy from one linux machine to another machine use scp.  #scp sourcefile destfile.
  11. Deployment is nothing but copying artifact file (.war)
  12. Get the location of artifact file (.war) in the dev server by checking log file.
    Building war: /home/ubuntu/.jenkins/workspace/scriptedpipeline1/webapp/target/webapp.war
  13. Location of file in qa server where it needs to be copied /var/lib/tomcat8/webapps
  14. #scp /home/ubuntu/.jenkins/workspace/scriptedpipeline1/webapp/target/webapp.war ubuntu@privateip_qa:/var/lib/tomcat8/webapps/qaenv  *(webapp.war file is copied to qa server with qaenv name)
  15. Go to snippet generator and generate code for the scp command by using sh shell script.
  16. copy code sh 'scp /home/ubuntu/.jenkins/workspace/scriptedpipeline1/webapp/target/webapp.war ubuntu@172.31.10.167:/var/lib/tomcat8/webapps/qaenv.war'  and paste it in job.
node('master')
{
       stage('continuous download')
         {
              git 'https://github.com/sunildevops77/maven.git'
        }
                      stage('continuous build')
                           {
                                 sh 'mvn package'
                           }
                                          stage('continuous deployment')
                                              {
                                            sh 'scp /home/ubuntu/.jenkins/workspace/scriptedpipeline1/webapp/target/webapp.war ubuntu@172.31.10.167:/var/lib/tomcat8/webapps/qaenv.war'
                                              }
}

Stage4 Continuous Testing:

node('master')
{
           stage('continuous download')
              {
                 git 'https://github.com/sunildevops77/maven.git'
              }
                     stage('continuous build')
                     {
                           sh 'mvn package'
                     }
                       stage('continuous deployment')
                           {
                                   sh 'scp /home/ubuntu/.jenkins/workspace/scriptedpipeline1/webapp/target/webapp.war ubuntu@172.31.10.167:/var/lib/tomcat8/webapps/qaenv.war'
                           }
                             stage('continuous testing')
                                {
                                        sh 'echo "Testing is passed, deploy to production server"'
                                 }
}

Stage5 Continuous Delivery:

node('master')
{
     stage('continuous download')
        {
            git 'https://github.com/sunildevops77/maven.git'
        }
            stage('continuous build')
                {
                     sh 'mvn package'
                }
                     stage('continuous deployment')
                      {
                          sh 'scp /home/ubuntu/.jenkins/workspace/scriptedpipeline1/webapp/target/webapp.war ubuntu@172.31.10.167:/var/lib/tomcat8/webapps/qaenv.war'
                      }
                             stage('continuous testing')
                                {
                                      sh 'echo "Testing is passed, deploy to production server"'
                                }
                                         stage('continuous delivery')
                                          {
                                         sh 'scp /home/ubuntu/.jenkins/workspace/scriptedpipeline1/webapp/target/webapp.war ubuntu@172.31.13.252:/var/lib/tomcat8/webapps/prdenv.war'
                                          }
}

Jenkinsfile: The groovy script copied and paste it ina file called jenkinsfile, which is uploaded to git hub repository along with code files given by developer.  This file will control CICD stages automatically.

 

CICD Pipeline with Declarative method using jenkins file:

Dev Server

pipeline
{
        agent any
            stages
            {
                  stage('ContinuousDownload')
                       {
                  steps
                       {
                               git 'https://github.com/aziz27uk/intelliqitdev.git'
                        }
                       }
                  stage('ContinuousBuild')

                       {
                  steps
                        {
                                 sh label 'mvn package'
                       }
                       }

                  stage('ContinuousDeployment')
                       {
                  steps
                            {
                               'scp /home/ubuntu/.jenkins/workspace/Intelliqit/declarative-pipeline/webapp/target/webapp.war ubuntu@172.31.13.170:/var/lib/tomcat8/webapps/qaenv.war'
                             }
                       }

                  stage('ContinuousTesting')
                       {
                  steps
                            {
                               git 'https://github.com/aziz27uk/intelliqitqa.git'

                                sh 'java -jar /home/ubuntu/.jenkins/workspace/Intelliqit/declarative-pipeline/testing.jar'
                             }
                       }

                 stage('ContinuousDelivery')

                  steps

                          {

                             'scp /home/ubuntu/.jenkins/workspace/Intelliqit/declarative-pipeline/webapp/target/webapp.war ubuntu@172.31.6.247:/var/lib/tomcat8/webapps/prdenv.war'

                             }

                        }
               }
}

Post Conditions or PostBuild feature in Declarative Pipeline: send email if any of 4 stages get failed and if all 4 stages successful then perform 5th stage.

 

pipeline
{
        agent any
            stages
            {
                  stage('ContinuousDownload')
                       {
                  steps
                       {
                               git 'https://github.com/aziz27uk/intelliqitdev.git'
                        }
                       }
                  stage('ContinuousBuild')

                       {
                  steps
                        {
                                 sh label 'mvn package'
                       }
                       }

                  stage('ContinuousDeployment')
                       {
                  steps
                            {
                               'scp /home/ubuntu/.jenkins/workspace/Intelliqit/declarative-pipeline/webapp/target/webapp.war ubuntu@172.31.13.170:/var/lib/tomcat8/webapps/qaenv.war'
                             }
                       }

                  stage('ContinuousTesting')
                       {
                  steps
                            {
                               git 'https://github.com/aziz27uk/intelliqitqa.git'

                                sh 'java -jar /home/ubuntu/.jenkins/workspace/Intelliqit/declarative-pipeline/testing.jar'
                             }
                       }

                Post
                     {
                         success
                           {
                                'scp /home/ubuntu/.jenkins/workspace/Intelliqit/declarative-pipeline/webapp/target/webapp.war ubuntu@172.31.6.247:/var/lib/tomcat8/webapps/prdenv.war'
                           }
                         Failure
                          {
                                  mail bcc: ' ', body: 'Jenkins CICD Failed in one of the stage. ', cc: ' ', from: ' ', replyTo: ' ', subject: 'CICD failed', to: 'aziz27uk@gamil.com'
                          }
             }
}

email notification received for jenkins cicd stage failed

Drawback: There is only one failure for any of all 4 stages, failure could be in stage 1 to 4.

Loopback Activity: Failure on any stage and send email on the failure of particular stage:

click here to get the code.

 

 


 Multibranch Pipeline:

Steps performed by developer to upload code and jenkins file to git hub repository.

developer code files

developer commit a

Developers received new functionality request from client, developer creates a branch and write code for new functionality.

commits in branch

CICD pipeline stage1 and stage2 has been performed, open Jenkinsfile  to check.

master branch jenkins file

When developer commits branch, need to push it to git hub.

When developers make changes in one branch code as per clients request and CICD stages will be performed only on that branch where code has been modified and CICD will not be executed on rest of the branches.

branch sources

CICD pipeline stage 1 continuous download

code amended in master branch

CICD trigger in master branch


Email Notification:

Integrating Jenkins with Gmail SMTP server:

Test email notification for stage gets failed: