Jenkins Workflow

Jenkins workflow | Full tutorial from beginner to advance in 2022

Jenkins is one of the most popular CICD tools, which is entirely open-source. In this blog, we will understand what Jenkins workflow is. We will also understand the basics of the Jenkins pipeline and different ways to define a pipeline. So let’s get started.

What is Jenkins

what is jenkins

Jenkins is an open-source CI-CD tool that software developer uses for continuous integration and continuous development. Jenkins is written in java and follows the complete life cycle of software from development to testing to deployment.

Jenkins is one of the most popular and widely used applications whose popularity grows day by day. Companies can accelerate their software development process by using Jenkins, as Jenkins can automate build and tests rapidly.

There are many ways to install Jenkins in your system

  • Installation of a physical machine
  • Installed via docker container
  • Installed on top of Kubernetes
  • Installed on Kubernetes as an operator(Jenkins operator)

What is Jenkins workflow

Jenkins workflow is the old name for the Jenkins pipeline. Pipelines are the Jenkins job which a user-defined programmatically using the groovy script

what is Jenkins workflow

To create a pipeline goto Jenkins > New item > Pipeline

Give a proper name and click on OK

What is Jenkins pipeline

In Jenkins, a pipeline is simply a collection of events or jobs that might be interlinked together in a sequence.

As per the official pipeline definition, Jenkins Pipeline is a suite of plugins that 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.

There are many advantages of defining Jenkins job via pipeline:

  • Jenkins pipeline enables you to define the Jenkins job via pipeline which is easy to maintain and review.
  • Jenkins pipelines are robust and pipelines will be secure even if the server undergoes an unpredicted restart.
  • With Jenkins Pipelines, you can add CI-CD to big projects as well as with Jenkins pipelines you can run multiple jobs.

Jenkins pipeline syntax

Jenkins support two types of pipeline declaration

  • Declarative Pipeline
  • Scripted pipeline

Both declarative, as well as scripted pipelines, should define a predefined syntax to define the pipeline. The Jenkins scripted pipeline should start with a node, whereas Jenkins declarative pipeline should start with a pipeline block.

Some of the widely used sections in the Jenkins pipelines are:-

  • Agent
  • post
  • Stages
  • steps

Jenkins pipeline agent

The agent session in the Jenkins pipeline specifies where the entire Jenkins or Jenkins stage will run. The agent session must be defined on top of pipelines but are optional in the stage label. The agent in the Jenkins pipeline can be:

  • Any
  • none
  • label
  • node
  • docker
  • dockerfile
  • kubernetes

More information about the Jenkins agents can be found here

Jenkins pipeline stages

The stage session is where you define your work in the Jenkins pipeline. The Jenkins stages block consists of one or more stage blocks. Below is a sample Jenkins pipeline with a single-stage

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

You can check my below blogs to learn more about the stages block

Jenkins pipeline Multiple stages

Jenkins pipeline syntax generator

Jenkins provides a very powerful and valuable tool to get the syntax for your Jenkins pipeline. The syntax generator tool can be found under

http://<jenkins_url>/directive-generator/

Once you go to the above URL, the below UI will open

Jenkins pipeline syntax generator

The syntax generator tool is straightforward to use. You can go ahead and try to generate the syntax for your Jenkins pipeline based on your use case.

Let’s understand how we can use the syntax generator tool to generate the when condition pipeline.

In the simple directive, select when a condition, and inside the when condition select anyof and checkmark the Evaluate before option and then click on Generative Declarative directive to generate the pipeline syntax.

Jenkins pipeline syntax generator

The syntax generator tool has generated the below syntax, which we can use in our pipeline declaration. This tool displays the code and points us to the official documentation link.

Jenkins Declarative Pipeline

The Jenkins declarative pipeline was recently added to the Jenkins pipeline. It follows a very simple syntax on top of the Pipeline sub-systems.

All Declarative Pipelines must be enclosed within a pipeline block

pipeline {
    /* insert Declarative Pipeline here */
}

The declarative pipeline is relatively new, so there are limitations on the size of code within the pipeline{} block.

The various components of the JenkinsDeclarative Pipeline are:

  • Agent
  • post
  • stages
  • steps

Jenkins scripted pipeline

The scripted pipeline is a general-purpose DSL build on groovy. Most functionality of groovy is made available to users of scripted pipelines. The other advantage of scripted pipelines is that they are very flexible and expressive.

LikeDeclarative Pipelines, scripted pipelines also have stages and steps definitions.

You can check this link to understand the syntax difference betweendeclarative andscripted pipelines.

Jenkins pipeline example

We will see the basic JenkinsDeclarative andscripted pipeline examples in this session.

Jenkins declarative pipeline example groovy

The Jenkinsdeclarative pipeline follows the below syntax:

pipeline {
    agent none
    stages {
        stage('Build') {
            steps {
                echo 'Hello World!!'
            }
        }
        stage('Deploy') {
            when {
                beforeInput true
                branch 'production'
            }
            input {
                message "Deploy to production?"
                id "simple-input"
            }
            steps {
                echo 'Deploying'
            }
        }
    }
}

Jenkins scripted pipeline example.

Jenkins scripted pipeline uses domain-specific language like groovy to define your Jenkins job in the form of the pipeline. The Jenkins scripted pipeline follows the below syntax.

node {
    stage('Test') {
        if (env.BRANCH_NAME == 'master') {
            echo 'execute on the master branch'
        } else {
            echo 'execute elsewhere'
        }
    }
}

Please read my blog on Jenkins scripted pipeline to get a complete understanding of the scripted pipeline.

Jenkins pipeline multiple agents

So far, we have used Jenkins with a single agent. But users can define the Jenkins pipeline to run on multiple agents. Building pipelines in multiple agents can benefit advanced use cases such as executing builds/tests across various platforms.

Below is the Jenkins pipeline with multiple agents defined for each stage.

The agent is none for the builds stage. None means using any available agent.

In the Linux test step, select an agent whose label matches Linux, and in thewindows test step, select an agent whose label matches windows

pipeline {
    agent none
    stages {
        stage('Build') {
            agent any
            steps {
                echo 'Build stage'
            }
        }
        stage('Test on Linux') {
            agent { 
                label 'linux'
            }
            steps {
                echo 'Testing script on linux agent'
            }
        }
        stage('Test on Windows') {
            agent {
                label 'windows'
            }
            steps {
            	echo 'Testing script on Windows agent'
            }
        }
    }
}

Conclusion

I hope you like this small tutorial on Jenkins workflow. There has been a lot of new functionality added to Jenkin’s latest version. I recommend you check my other blogs on Jenkins to get familiar with recent development.

Please do let me know if you are facing any issues while following along

More to read

Create python virtual environment via Jenkins

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top