Day 26 Task: Jenkins Declarative Pipeline

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 Yuvraj Nath Chouhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yuvraj Nath Chouhan
Yuvraj Nath Chouhan

🚀 Aspiring DevOps & DevSecOps Engineer | Cloud DevOps (Azure & AWS) | Automation & Security Champion | Passionate Learner🌐 Hi, I’m Yuvraj! 🔥 I'm embarking on an exciting journey in the world of DevOps & DevSecOps, armed with hands-on expertise and a drive to excel. From automating tasks to building robust CI/CD pipelines, I’m dedicated to turning complex challenges into efficient and secure solutions. What I Bring to the Table: ●Linux & Shell Scripting 🖥️ Automating processes and mastering command-line operations. ●Git & Git Branching 🔀 Seamless version control for effective collaboration. ●Build Tools 🔧 (Maven & npm) Crafting stable builds for quality software. ●CI/CD Tools 🚄 (Jenkins, GitHub Actions, Azure DevOps, GitLab CI/CD) Ensuring smooth & automated deployments. ●Security in DevOps 🛡️ Embedding security at every stage to safeguard development processes. ●Artifact Management 📦 (Nexus & Azure Artifacts) Managing software versions efficiently. ●Containerization & Orchestration 🐳⎈ (Docker & Kubernetes) Deploying scalable, agile applications. ●Infrastructure as Code (IaC) 📜 (Terraform & Ansible) Automating environment provisioning. ●Monitoring & Troubleshooting 🔍 Keeping systems in check with proactive issue resolution. ●Real-world & Corporate Projects 🌟 Hands-on experience with practical implementations. ✨ Always eager to learn, collaborate, and innovate! ✨ I’m ready to transform my academic knowledge and project experiences into real-world impact. Let’s connect and explore how we can drive meaningful change together! 🤝