Day26 - 90DaysOfDevOps Jenkins Declarative Pipeline

Day26 - 90DaysOfDevOps Jenkins Declarative Pipeline

What is pipeline?

The pipeline is a series of steps or stages that software code passes through, with each stage serving a specific purpose in the software development lifecycle. The goal of a pipeline is to automate and streamline the process of building, testing, and deploying software, reducing manual intervention and improving efficiency.

What Scripted vs Declarative pipeline in Jenkins?

Jenkins Pipeline provides two distinct approaches for defining and executing automated tasks: scripted and declarative pipelines.

Scripted Pipeline

Scripted pipelines are the original and more flexible approach to Jenkins Pipeline. They allow for writing arbitrary Groovy code within the pipeline script, granting complete control over the execution flow.

Key characteristics of scripted pipelines:

  • Utilizes Groovy language for defining pipeline steps and logic

  • Offers maximum control over the execution flow

  • Can be quite complex and difficult to maintain

node {
    checkout scm
    sh 'mvn clean install'
    sh 'mvn test'
}

Declarative Pipeline

Declarative pipelines introduce a more structured and declarative way of defining pipeline stages, tasks, and parameters. They utilize a more human-readable syntax and break down complex processes into manageable stages.

Key characteristics of declarative pipelines:

  • Employs a declarative syntax for defining pipeline stages and tasks

  • Offers a more readable and maintainable structure

  • Well-suited for standard software development workflows

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }

        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
    }
}

What is agent in Jenkins?

Agent in Jenkins is an remote machine or container that executes the tasks specified in a Jenkins pipeline. Agents are responsible for doing the actual work of building, testing, and deploying software.

There are two main types of agents in Jenkins:

  • Self-hosted agents: These agents are installed and managed locally on a specific machine. They are typically used for continuous integration (CI) jobs, where the code is constantly being built and tested.

  • Remote agents: These agents are hosted on remote machines or cloud instances. They are typically used for continuous delivery (CD) jobs, where the code is automatically deployed to production.

In addition to these two main types, there are also several other types of agents, such as:

  • Docker agents: These agents use Docker containers to execute tasks. This allows for a more consistent and portable environment, as the tasks are isolated from the host machine.

  • Kubernetes agents: These agents use Kubernetes clusters to execute tasks. This can be a more scalable and efficient solution for large-scale deployments.

  • Slave agents: These are older-style agents that are no longer recommended for use. They are not as flexible or scalable as self-hosted or remote agents.


Task-01

  • Create a New Job, this time select Pipeline instead of Freestyle Project.

  • Follow the Official Jenkins Hello world example

  • Complete the example using the Declarative pipeline

\==>

Login to Jenkins : http://localhost:8080 OR http://publicipaddress:8080

Then select (NEW_ITEM) create Jenkins declarative pipeline.

Give the name of declarative pipeline and select (PIPELINE) from Jenkins dashboard.

In (GENERAL) give some description about your pipeline !!

select (PIPELINE) then write your first pipeline !!!

then (APPLY) and (SAVE) changes !!

Then click (BUILD NOW) , it will build your pipeline !!!!!

go to (CONSOLE OUTPUT) to check your script processes and finished status.


Hope this article will prove to be valuable, helping you to understand Jenkins .