Jenkins

Jenkins Pipeline

Jenkins Pipeline

In Jenkins, a pipeline is a group of events or jobs which are interlinked with one another in a sequence.

Jenkins pipeline is a way to define your build process, continuous integration, and continuous delivery (CI/CD) workflows as code. It allows you to script and automate the steps involved in building, testing, and deploying applications, making the entire process more manageable, repeatable, and version-controlled.The pipeline in Jenkins is expressed as a Groovy script and is typically stored alongside your codebase in a file named Jenkinsfile. This file contains the definition of your pipeline, which includes the various stages, steps, and actions that Jenkins will execute when running the build or deployment process.

Jenkins is an open source continuous integration server that provides the ability to continuously perform automated builds and tests. Several tasks can be controlled and monitored by Jenkins, including pulling code from a repository, performing static code analysis, building your project, executing unit tests, automated tests and/or performance tests, and finally, deploying your application. These tasks typically conform a continuous delivery pipeline.

Jenkins pipelines offer numerous benefits, including:

  • Version control of your CI/CD process alongside the codebase.
  • Reusability of pipeline code across projects.
  • Visualization of the build pipeline, showing the progress of each stage.
  • The ability to pause and resume the pipeline at any stage for manual intervention.
  • Integration with other Jenkins plugins and tools for additional functionality.

By defining your CI/CD process as a pipeline, you can ensure consistency, repeatability, and automation, which leads to more efficient and reliable software development and deployment workflows.

Continuous Delivery Pipelines and How it Works?

In a Jenkins pipeline, every job or event has some sort of dependency on at least one or more events.

The picture above represents pipelines are a series of automated steps and actions that facilitate the process of continuously delivering software changes to production or other target environments in a reliable, efficient, and consistent manner. These pipelines are a core element of a CI/CD (Continuous Integration/Continuous Delivery) system and enable development teams to automate the building, testing, and deployment of their applications.

Pipelines are a suite of Jenkins plugins. Pipelines can be seen as a sequence of stages to perform the tasks just detailed, among others, thus providing continuous releases of your application. The concept “continuous” is relative to your application and/or environment:

Jenkins Pipeline Concepts

Pipeline: The pipeline is a set of instructions given in the form of code for continuous delivery and consists of instructions needed for the entire build process. With pipeline, you can build, test, and deliver the application.

Node: The machine on which Jenkins runs is called a node. A node block is mainly used in scripted pipeline syntax.

Stage: A stage block contains a series of steps in a pipeline. That is, the build, test, and deploy processes all come together in a stage. Generally, a stage block is used to visualize the Jenkins pipeline process.

Step: A step is nothing but a single task that executes a specific process at a defined time. A pipeline involves a series of steps.

Why Use Jenkin’s Pipeline?

Jenkins is an open continuous integration server which has the ability to support the automation of software development processes. You can create multiple automation jobs with the help of use cases, and run them as a Jenkins pipeline.

Here are the reasons why you use should use Jenkins pipeline:

  • Jenkins pipeline is implemented as a code which allows multiple users to edit and execute the pipeline process.
  • Pipelines are robust. So if your server undergoes an unforeseen restart, the pipeline will be automatically resumed.
  • You can pause the pipeline process and make it wait to resume until there is an input from the user.
  • Jenkins Pipelines support big projects. You can run multiple jobs, and even use pipelines in a loop.

What is a JenkinsFile?

A Jenkinsfile is a text file that contains the definition of a Jenkins pipeline. It is written in Groovy, a scripting language that runs on the Java Virtual Machine (JVM). Jenkinsfile allows you to define your entire CI/CD pipeline as code, making it version-controlled, repeatable, and easy to maintain.In a Jenkinsfile, you define the stages, steps, and actions that Jenkins will execute when running your build, test, and deployment processes. The pipeline can include various stages, such as build, test, deploy, and more, and each stage can have multiple steps that represent individual tasks or actions.

Jenkinsfile are typically stored alongside your codebase in the root directory or a version control repository. When Jenkins detects changes in the Jenkinsfile or the codebase, it automatically triggers the pipeline, ensuring that the entire CI/CD process is executed consistently and transparently.

The benefits of using JenkinsFile are:

  • You can create pipelines automatically for all branches and execute pull requests with just one JenkinsFile.
  • You can review your code on the pipeline
  • You can audit your Jenkins pipeline
  • This is the singular source for your pipeline and can be modified by multiple users.

JenkinsFile can be defined by either Web UI or with a JenkinsFile.

Declarative versus Scripted pipeline syntax:

There are two types of syntax used for defining your JenkinsFile.

  1. Declarative
  2. Scripted

Declarative:

The declarative pipeline provides a more structured and simpler syntax for defining pipelines. It is designed to be easy to read and understand, especially for those new to Jenkins and CI/CD concepts.

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                // Commands to build the project
            }
        }
        stage('Test') {
            steps {
                // Commands to run tests
            }
        }
        stage('Deploy') {
            steps {
                // Commands to deploy the application
            }
        }
    }
}

Scripted:

The scripted pipeline uses a more flexible and complex scripting syntax, allowing for advanced customization and conditional logic. It provides more control and is suitable for complex build processes.

node {
    stage('Build') {
        // Commands to build the project
    }
    stage('Test') {
        // Commands to run tests
    }
    stage('Deploy') {
        // Commands to deploy the application
    }
}

Install Build Pipeline Plugin in Jenkins

With the build pipeline plugin, you can create a pipeline view of incoming and outgoing jobs, and create triggers which require manual intervention.

1.The settings for the plugin can be found under Manage Jenkins > Manage Plugins.

If you have already installed the plugin, it is shown under the installed tab.

2. If you do not have the plugin previously installed, it shows up under the Available tab.

Once you have successfully installed the build pipeline plugin in your Jenkins, follow these steps to create your Jenkins pipeline.

How to Create Your Jenkins Pipeline

With the introduction of the Pipeline, Jenkins added an embedded Groovy engine, making Groovy the scripting language in the Pipeline´s DSL.

Here are the steps you need to take to setup a Jenkins Pipeline.

1. First, log on to your Jenkins server and select “New Item” from the left panel:

create your jenkins pipeline

2. Next, enter a name for your pipeline and select “Pipeline” from the options. Click “Ok” to proceed to the next step:

use your jenkins scripted pipeline

3. You can now start working your Pipeline script:

pipeline script in jenkins

The red box in the middle is where you can start writing your script, which will be explained now.

References:

https://jenkins.io/doc/book/pipeline/

https://jenkins.io/doc/pipeline/tour/hello-world/

Categories: Jenkins

4 replies »

Leave a Reply