Jenkins in Action

Let’s learn Jenkins with handson

Agenda

  • Why Jenkins?

  • Jenkins Plugins

  • Jenkins Pipelines

  • Managing Jenkins

What is Jenkins?

  • Self-contained, open source automation server which can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software

Install and Configure Jenkins

  • Jenkins can be installed through native system packages, Docker, or even run standalone by any machine with a Java Runtime Environment (JRE) installed

Using docker

 docker run -u 0 --privileged -d  -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home  -v $(which docker):/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock jenkins/jenkins:lts
  • Note extra volumes added for docker to use host docker daemon this is done for the sake of demo, in production it will run on agents on which docker is installed

Go to http://localhost:8080

Let’s explore Jenkins environment

Plugins

  • Pipeline Plugins
  • Docker, Docker Pipeline
  • BlueOcean
  • lot more

Jenkins Agents

  • Jenkins architecture is designed for distributed build environments
  • Jenkins controller administers the Jenkins agents and orchestrates their work, including scheduling jobs on agents and monitoring agents
  • Agents may be connected to the Jenkins controller using either local or cloud computers
  • Jenkins agents can be launched in physical machines, virtual machines, Kubernetes clusters, and with Docker images

Sample Job Run

Jenkins Pipeline

  • Jenkins Pipeline is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins
  • Pipeline provides an extensible set of tools for modeling simple-to-complex delivery pipelines “as code” via the Pipeline DSL syntax
  • Jenkins Pipeline definition is written into a text file (called a Jenkinsfile) which in turn can be committed to a project’s source control repository
  • Foundation of “Pipeline-as-code”; treating the CD pipeline a part of the application to be versioned and reviewed like any other code

Jenkinsfile Benefits

  • Automatically creates a Pipeline build process for all branches and pull requests
  • Code review/iteration on the Pipeline (along with the remaining source code)
  • Audit trail for the Pipeline
  • Single source of truth for the Pipeline, which can be viewed and edited by multiple members of the project
  • While the syntax for defining a Pipeline, either in the web UI or with a Jenkinsfile is the same, it is generally considered best practice to define the Pipeline in a Jenkinsfile and check that in to source control

Declarative versus Scripted Pipeline

  • A Jenkinsfile can be written using two types of syntax - Declarative and Scripted
  • Declarative and Scripted Pipelines are constructed fundamentally differently
  • Declarative Pipeline

  • Declarative Pipeline is a more recent feature of Jenkins Pipeline which:
    • provides richer syntactical features over Scripted Pipeline syntax, and
    • is designed to make writing and reading Pipeline code easier
  • Scripted Pipeline

  • Like Declarative Pipeline, is built on top of the underlying Pipeline sub-system
  • Unlike Declarative, Scripted Pipeline is effectively a general-purpose DSL built with Groovy
  • Most functionality provided by the Groovy language is made available to users of Scripted Pipeline, which means it can be a very expressive and flexible tool

Explore Jenkins Pipeline

  • Simple Pipeline
pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}
  • Docker as Agent
pipeline {
    agent { docker { image 'ruby:2.6.6-alpine' }}
    stages {
        stage('Version check') {
            steps {
                sh 'ruby --version'
            }
        }
    }
}

Using a image built for specific purpose

Use contents from this folder for building image

cd to calculator_as_interpreter in https://github.com/slashpai/docker_k8s_training/tree/main/docker_apps

pipeline {
    agent { docker { image 'calculator_interpreter:v1' }}
    stages {
        stage('Sum 2 + 3') {
            steps {
                sh 'ruby /app/calculator.rb 2 + 3'
            }
        }

        stage('Diff 2 - 3') {
            steps {
                sh 'ruby /app/calculator.rb 2 - 3'
            }
        }
    }
}
  • Parameters in Pipeline
pipeline {
    agent any
    parameters {
        string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')

        text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')

        booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')

        choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')

        password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')
    }
    stages {
        stage('Example') {
            steps {
                echo "Hello ${params.PERSON}"

                echo "Biography: ${params.BIOGRAPHY}"

                echo "Toggle: ${params.TOGGLE}"

                echo "Choice: ${params.CHOICE}"

                echo "Password: ${params.PASSWORD}"
            }
        }
    }
}

Parallel Stages

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
        stage('Parallel Stages') {
            parallel {
                stage('parallel 1') {
                    steps {
                        echo 'Hello from Parallel Stage 1'
                    }
                }
                stage('parallel 2') {
                    steps {
                        echo 'Hello from Parallel Stage 2'
                    }
                }
            }
        }
    }
}

More options: https://www.jenkins.io/doc/book/pipeline/syntax/

Pipleline Examples

https://github.com/jenkinsci/pipeline-examples

BlueOcean Dashboard

Credentials

  • There are numerous 3rd-party sites and applications that can interact with Jenkins, for example, artifact repositories, cloud-based storage systems and services, and so on
  • A systems administrator of such an application can configure credentials in the application for dedicated use by Jenkins
  • Once a Jenkins manager (i.e. a Jenkins user who administers a Jenkins site) adds/configures these credentials in Jenkins, the credentials can be used by Pipeline projects to interact with these 3rd party applications

Helpful Plugins for System admin

https://github.com/jenkinsci/ssh-steps-plugin

Managing Jenkins

Script Console

Usecase: https://support.cloudbees.com/hc/en-us/articles/360051376772-How-can-I-purge-clean-the-build-queue-?page=3

Jenkins.instance.queue.clear()

Helpful scripts

https://github.com/cloudbees/jenkins-scripts

References

Training Summary

  • Why Jenkins?

  • Jenkins Pipelines

  • Helpful plugins

  • Managing Jenkins

Thank you!

Any questions?