Jenkins Pipeline

What is Jenkins pipeline?

The Jenkins pipeline in CI/CD automates many tasks and makes the CI/CD pipeline tasks reliable, efficient, repeatable, and high in quality. It defines a complete list of events that happen in the code lifecycle. Starting from the build, to testing and deployment.

Instead, you were creating and configuring jobs on the Jenkins users interface you would write a file or script that build, and it's configuration in a Jenkinsfile. So, Jenkinsfile is pipeline as a code it's a scripted pipeline. A Jenkinsfile that stores the entire workflow as code, and it can be checked into a Source Code Management (SCM) on your local system.

Jenkinsfile

Jenkins Pipeline is defined using a text file called the Jenkinsfile. Additionally, the pipeline implements as code using Groovy Domain-specific language(DSL); it is stored into an SCM like GitHub for example and can be updated by many ways as using Jenkins master UI or directly from the SCM with a text editor. As it is centralized, it allows developers an easy to access it to edit update and check it.

SCM

Source code management, tracks changes of your code stored in a source code repository, example git or mercurial, it allows you to store a Jenkinsfile and your code on it and to update it from one single point.

Why should you use Jenkins pipeline?

  • Jenkins Pipelines supports big projects. You can run multiple jobs and even use pipelines in a loop.

  • Pipelines are robust. So if your server undergoes an unforeseen restart, the pipeline will be automatically resumed.

  • Whenever the new code is committed to git, a pipeline can automatically integrate that code and start building pipelines again itself.

  • The Jenkins pipeline can be stopped for the inputs from the user.

  • It supports larger projects that may involve a high CPU job, provided the Jenkins infrastructure is scalable enough to support it.

Pipeline structure components

  • Pipeline is structured in sections, called stages

  • Each stage includes steps

  • steps include the actual tests/commands to run

  • An agent defines where the programs and scripts execute

Pipeline

This is a user-defined block that contains all the processes such as building, testing, deploying, etc. It is a collection of all the stages in a Jenkinsfile. All the stages and steps are defined within this block. It is the key block for a declarative pipeline syntax.

pipeline {

}

Node

A node is a machine which is part of the Jenkins environment and is capable of executing a Pipeline.

Also, a node block is a key part of Scripted Pipeline syntax.

node {

}

Agent

It instructs Jenkins to allocate an executor for the builds. It is defined as an entire pipeline or a specific stage.

It has the following parameters:

Any: Runs pipeline/ stage on any available agent

None: applied at the root of the pipeline, it indicates that there is no global agent for the entire pipeline & each stage must specify its own agent

Label: Executes the pipeline/stage on the labelled agent

Docker: Uses docker container as an execution environment for the pipeline or a specific stage

pipeline {

agent {

docker {

image 'ubuntu'

}

}

}

Stage

A stage block defines a conceptually distinct subset of tasks performed through the entire pipeline (e.g. "Build", "Test", and "Deploy" stages), which is used by many plugins to visualize or present Jenkins Pipeline status or progress.

pipeline{

agent any

stages {

stage ('Build'){

}

stage ('test'){

}

stage ('Deploy'){

}

}

}

Step

A single task. Fundamentally, a step tells Jenkins what to do at a particular point in time (or "step" in the process). For example, to execute the shell command make, use the sh step: sh "make". When a plugin extends the Pipeline DSL, that typically means the plugin has implemented a new step.

pipeline{

agent any

stages {

stage ('Build'){

steps {

echo 'building the code application...'

}

}

}

}


Two types of pipelines

  • Scripted pipeline: Sequential execution, using Groovy expressions for flow control

  • Declarative pipeline: It uses a framework to control execution (pre-defined commands to ease pipeline creation)