Jenkins

Jenkins Pipeline

Hello there! In this tutorial, we will understand Jenkins pipeline and will create a simple pipeline.

1. Overview of Jenkins

Jenkins is a game-changing open-source automation server that makes the lives of developers easier. It allows developers to automate parts of the software development process such as building, testing, and deploying code, so they can quickly catch bugs and issues early on. What’s more, with a vast range of plugins available, Jenkins can easily integrate with other tools and services, extending its functionality even further. This tool is accessible to developers of all levels, thanks to its user-friendly web-based interface and easy installation process.

1.1 Key features

Jenkins has an impressive set of features, making it a must-have tool for any development project. Here are just a few of its key features:

  • Continuous Integration and Continuous Deployment (CI/CD): Jenkins enables developers to quickly and continuously build and test their code changes, allowing for faster feedback and more efficient collaboration.
  • Distributed Builds: Jenkins can distribute build tasks across multiple machines, improving performance and reducing build times.
  • Plugin Support: Jenkins has a large number of plugins available that can extend its functionality to perform a wide range of tasks, including integrating with other tools and services.
  • Extensibility: Jenkins is highly extensible and can be customized to meet the needs of individual teams, making it adaptable to a wide range of use cases and scenarios.
  • Easy Installation: Jenkins can be easily installed on any platform and can be set up quickly and easily.
  • Web-based Interface: Jenkins provides a user-friendly web-based interface that makes it easy to manage jobs and monitor build results.
  • Pipeline Support: Jenkins has powerful pipeline support that allows developers to define complex workflows and dependencies, streamlining the development process.
  • Automation: Jenkins can automate many tasks involved in the software development process, from building and testing to deployment and monitoring, freeing up valuable time for developers.
  • Scalability: Jenkins is highly scalable and can be used to manage large and complex software projects, so it grows with your business.
  • Open-source: Jenkins is open-source software, which means that it is free to use and can be modified and customized by anyone with programming knowledge, making it highly versatile.

1.2 Overview of Jenkins pipelines

Jenkins Pipeline is a powerful feature of Jenkins, a popular open-source automation server, that enables continuous delivery and integration through scripting. A pipeline in Jenkins is a set of interconnected stages that define the steps required to build, test, and deploy an application. It provides a way to define and orchestrate complex workflows, allowing users to model their entire software delivery process as a code that can be versioned and managed like any other code. With Jenkins Pipeline, users can express their build pipeline as a series of stages and steps, allowing them to automate their software delivery process and improve their efficiency and reliability.

Jenkins Pipeline supports multiple ways of defining pipelines, including a Declarative Pipeline and a Scripted Pipeline. Overall, Jenkins Pipeline is a powerful tool for automating software delivery workflows, enabling users to define, version, and manage their entire delivery process as code.

1.2.1 What is a Scripted pipeline?

A scripted Pipeline is one of the two ways to define pipelines in Jenkins, the other being a Declarative Pipeline. Scripted Pipeline is a Groovy-based syntax that allows users to write scripts to define their pipeline steps, with maximum flexibility and power.

In Scripted Pipeline, users define their pipeline using Groovy code, which is executed on the Jenkins master or slave nodes. Scripted Pipeline allows users to define their pipeline using a wide range of control structures, including loops, conditionals, and functions, giving them a lot of power and flexibility to define complex workflows. Scripted Pipeline also allows users to use the full power of the Jenkins API, including accessing build parameters, triggering downstream builds, and setting custom environment variables. This makes Scripted Pipeline suitable for complex workflows that require advanced features, such as parallel stages, conditional execution, and error handling.

However, Scripted Pipeline can be more difficult to read and maintain than Declarative Pipeline, as it requires users to write Groovy code and handle more low-level details. Additionally, a Scripted Pipeline does not have the same level of built-in validation and error checking as a Declarative Pipeline, which can make debugging more challenging.

1.2.1.1 Creating a Scripted pipeline

Scripted pipeline syntax

node {
  stage('Checkout') {
    git 'https://github.com/myusername/myrepo.git'
  }
  
  stage('Build') {
    sh 'mvn clean package'
  }
  
  stage('Test') {
    sh 'mvn test'
  }
  
  stage('Deploy') {
    sh 'ansible-playbook deploy.yml'
  }
}

In this example, we have a Scripted Pipeline that defines a build pipeline with four stages: Checkout, Build, Test, and Deploy. In the Checkout stage, we use the git command to clone a repository from GitHub. In the Build stage, we use the mvn command to build the application using Maven. In the Test stage, we use the mvn command to run the tests for the application. Finally, in the Deploy stage, we use the ansible-playbook command to deploy the application to our servers.

1.2.2 What is a Declarative pipeline?

Declarative Pipeline is one of the two ways to define pipelines in Jenkins, the other being a Scripted Pipeline. Declarative Pipeline is a simpler, more opinionated syntax that allows users to define their pipeline using a declarative syntax, similar to a configuration file.

In Declarative Pipeline, users define their pipeline using a declarative syntax that is easier to read and maintain than Scripted Pipeline. Declarative Pipeline provides a set of predefined steps that can be used to define a pipeline, making it easier for users to create pipelines without having to write complex scripts. Declarative Pipeline also provides built-in validation and error checking, making it easier to detect and fix errors in the pipeline definition. It also supports more advanced features, such as parallel execution, post-conditions, and agent directives.

However, Declarative Pipeline is more opinionated than Scripted Pipeline, which means that it provides less flexibility and power. Users must follow a strict syntax and cannot use all the features of the Jenkins API directly. Additionally, Declarative Pipeline requires users to use specific syntax for certain steps, which can be more restrictive than Scripted Pipeline.

1.2.2.1 Creating a Declarative pipeline

Declarative pipeline syntax

pipeline {
  agent any
  
  stages {
    stage('Checkout') {
      steps {
        git 'https://github.com/myusername/myrepo.git'
      }
    }
    
    stage('Build') {
      steps {
        sh 'mvn clean package'
      }
    }
    
    stage('Test') {
      steps {
        sh 'mvn test'
      }
    }
    
    stage('Deploy') {
      steps {
        sh 'ansible-playbook deploy.yml'
      }
    }
  }
}

In this example, we have a Declarative Pipeline that defines a build pipeline with four stages: Checkout, Build, Test, and Deploy. The pipeline block is used to define the pipeline. The agent any statement specifies that the pipeline can run on any available agent.

The stages block is used to define the pipeline stages. Each stage has a name and a set of steps to execute. In the Checkout stage, we use the git step to clone a repository from GitHub. In the Build stage, we use the sh step to building the application using Maven. In the Test stage, we use the sh step to run the tests for the application. Finally, in the Deploy stage, we use the sh step to deploy the application to our servers using Ansible.

This Declarative Pipeline is written in a simpler, more declarative syntax that is easier to read and maintain than Scripted Pipeline. It defines a series of stages, each with one or more steps that execute commands or scripts. As the Pipeline executes, the results of each stage are recorded, and any failures or errors are reported.

1.3 Difference between Scripted and Declarative pipeline

There are several key differences between Scripted and Declarative Pipeline in Jenkins:

  • Syntax: Scripted Pipeline uses Groovy syntax, which allows for more flexibility and power but can be more complex and harder to read. Declarative Pipeline uses a simpler, more declarative syntax that is easier to read and maintain.
  • Validation and Error Checking: Declarative Pipeline provides built-in validation and error checking, which can help users detect and fix errors in their pipeline definition. Scripted Pipeline does not have the same level of built-in validation and error checking.
  • Features: Scripted Pipeline allows users to use the full power of the Jenkins API, including advanced features such as loops, conditionals, and functions. Declarative Pipeline provides a more limited set of predefined steps, which can make it easier for users to create pipelines without having to write complex scripts.
  • Flexibility: Scripted Pipeline provides users with maximum flexibility and power to define complex workflows, while Declarative Pipeline is more opinionated and restrictive in its syntax and feature set.
  • Maintenance: Declarative Pipeline is easier to read and maintain than Scripted Pipeline because it uses a simpler syntax and provides built-in validation and error checking.

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

2. Summary

In conclusion, Jenkins Pipeline is a powerful and flexible tool for building and deploying applications using a variety of automation tools and techniques. It allows users to define their build and deployment processes as code, which makes it easier to version control, test, and share with other members of the team. Additionally, Jenkins Pipeline supports both Scripted and Declarative Pipeline syntax, which provides users with the flexibility to choose the pipeline syntax that best suits their needs. Whether you are new to Jenkins or an experienced user, understanding how to create and manage pipelines is an essential skill for building, testing, and deploying applications with speed and efficiency. You can download the sample files from the Downloads section.

3. Download the Project

This was a tutorial on pipelines in Jenkins.

Download
You can download the files used in this example here: Overview of pipelines, creating a simple pipeline

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button