#90DaysOfDevops | Day 26

Rajendra PatilRajendra Patil
4 min read

🔹What is Pipeline?

In the context of Jenkins, a "pipeline" refers to a series of automated steps or jobs that are organized in a sequence to achieve a specific task or workflow. Pipelines are a fundamental concept in Jenkins, used for defining and automating the steps required to build, test, and deploy software applications.

There are two main ways to define and create pipelines in Jenkins: Declarative and Scripted.

Declarative Pipeline:

Declarative is a more recent and user-friendly way to define pipelines in Jenkins.

It is designed to provide a simpler and more structured syntax for defining pipelines.

Declarative pipelines use a set of predefined, high-level directives and keywords to define stages and steps in a pipeline.

These pipelines are easier to read, write, and maintain, making them a preferred choice for many Jenkins users, especially those who are new to pipeline scripting.

Declarative pipelines encourage best practices and provide good defaults, making them a safer and more controlled way to define pipelines.

Declarative Pipeline Example:

pipeline {
        agent any

        stages {
            stage('Build') {
                steps {
                    sh 'echo "Building the application"'
                }
            }
            stage('Test') {
                steps {
                    sh 'echo "Running tests"'
                }
            }
            stage('Deploy') {
                steps {
                    sh 'echo "Deploying the application"'
                }
            }
        }
          post { 
          // In this block you define expressions of build status or build status changes.
          always{
              // Actions to perform irrespective of build success or fail

          }
          success {
              // Actions to perform on successful build
              echo 'Build succeeded!'
          }
          failure {
              // Actions to perform on build failure
              echo 'Build failed!'
          }
      }
    }

In this Declarative Pipeline example:

We specify that the pipeline can run on any available agent.

We define three stages: Build, Test, and Deploy.

Each stage contains a single step, which is a shell command (sh) that prints a message.

Declarative pipelines use a predefined structure with pipeline, stages, and steps directives to simplify pipeline definition.

Scripted Pipeline:
Scripted pipelines were the original way to define pipelines in Jenkins and are built using Groovy, a powerful scripting language.

Scripted pipelines offer more flexibility and control over the pipeline's logic, making them suitable for complex and custom workflows.

In scripted pipelines, you write custom Groovy scripts to define the pipeline's stages and steps.

This flexibility comes at the cost of increased complexity, as scripted pipelines can become harder to read and maintain, especially for users who are not familiar with Groovy.

Scripted pipelines are still widely used when intricate customization or advanced logic is required for a pipeline.

Scripted Pipeline Example:

node {
        stage('Build') {
            echo 'Building the application'
            // Additional build steps can be added here
        }
        stage('Test') {
            echo 'Running tests'
            // Additional test steps can be added here
        }
        stage('Deploy') {
            echo 'Deploying the application'
            // Additional deployment steps can be added here
        }
    }

In this Scripted Pipeline example:

We start with a node block, which specifies where the pipeline should run.

Within each stage, we use the echo command to print messages.

Scripted pipelines provide more flexibility to define custom logic and execute arbitrary code within each stage.

The Importance of Having a Pipeline
By writing the pipeline's definition in a text file called a Jenkinsfile, and committing it to the project's source control repository, we're treating our CD pipeline just like any other part of the application's codebase.

Task-01: Task: Create a New Job using Pipeline Hello World Example

This example demonstrates the basic process of creating and executing a pipeline in Jenkins. Here's a more detailed breakdown of each step:

Step 1: Navigate to the Jenkins Home Page and Create a New Job
1.Open a web browser and navigate to the Jenkins home page.

2.Log in if required.

3.Click on "New Item" to create a new job.

4.Give a job a name and select "Pipeline" as the job type.

Image description

Step 2: Configure the Project with a Valid Description

In the job configuration page, scroll up to the top section where we can edit the job's details.

Add a valid description for the project.

Image description

Step 3: Follow the Official Jenkins Hello World Example

In the configuration page of the new job, scroll down to the "Pipeline" section.

Select the "Pipeline script" option.

Choose the "Pipeline script from SCM" option and provide the necessary repository details. (In this case, you're following the "Hello World" example, so you might choose to use a simple script or select Hello World or a Jenkinsfile in your repository.)

Image description

Step 4: Click on Save and Then Build Now
After saving our pipeline configuration, we can manually trigger a build by clicking "Build Now." This will initiate the pipeline execution based on the code we've written.

Image description

Step 5: Check the Full Stage View for Execution Time

Image description

Step 6: Verify Hello World Using Console Output

Image description

Happy Learning......

0
Subscribe to my newsletter

Read articles from Rajendra Patil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rajendra Patil
Rajendra Patil