Day 26 Task: Jenkins Declarative Pipeline

Pooja BhavaniPooja Bhavani
3 min read

One of the most important parts of your DevOps and CICD journey is a Declarative Pipeline Syntax of Jenkins

Some terms for your Knowledge

What is Pipeline - A pipeline is a collection of steps or jobs interlinked in a sequence.

Declarative: Declarative is a more recent and advanced implementation of a pipeline as a code.

Scripted: Scripted was the first and most traditional implementation of the pipeline as a code in Jenkins. It was designed as a general-purpose DSL (Domain Specific Language) built with Groovy.

Why you should have a Pipeline

The definition of a Jenkins Pipeline is written into a text file (called a Jenkinsfile) which in turn can be committed to a project’s source control repository.
This is the foundation of "Pipeline-as-code"; treating the CD pipeline as a part of the application to be versioned and reviewed like any other code.

Creating aJenkinsfile and committing it to source control provides a number of immediate benefits:

  • Automatically creates a Pipeline build process for all branches and pull requests.

  • Code review/iteration on the Pipeline (along with the remaining source code).

Pipeline syntax

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                //
            }
        }
        stage('Test') {
            steps {
                //
            }
        }
        stage('Deploy') {
            steps {
                //
            }
        }
    }
}

Task-01

  • Step 1: Ensure you have access to your Jenkins server's web interface and Log in to your Jenkins server with the appropriate credentials.
sudo apt update
sudo apt install openjdk-17-jre
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins

  • Step 3: write the description "This is hello world" Scroll down to the "Pipeline" section. In the "Definition" dropdown, select "Pipeline script" or "Pipeline script from SCM," depending on your preference and where your Jenkinsfile will be stored. For this example, let's select "Pipeline script." In the "Script" section, you'll define your Declarative Pipeline script. Follow the official Jenkins Hello World example, which typically looks like this:
  pipeline {
      agent any
      stages {
          stage('Build') {
              steps {
                  echo 'Hello, World!'
              }
          }
      }
  }

Click the "Apply and Save" button to save your pipeline job configuration.

  • Step 4: In the left-hand menu, click on "Build Now" to manually trigger a build of your pipeline. After triggering the build, you can view the pipeline execution by clicking on the build number under the "Build History" section.

  • Step 5: Within the build execution, you can check the "Console Output" to see the output of your Declarative Pipeline. It should display "Hello, World!"

0
Subscribe to my newsletter

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

Written by

Pooja Bhavani
Pooja Bhavani