Jenkins Declarative Pipeline: A Step-by-Step Guide for Day 26

ยท

5 min read

Imagine a world where every time you make a change to your code, everythingโ€”building, testing, and deployingโ€”happens automatically, with no manual effort. Sounds like magic? It's Jenkins Pipelines! ๐Ÿช„

Jenkins acts like your automation sidekick, and Jenkins Pipelines allow you to define your entire workflow in a simple file called a Jenkinsfile. You commit this file alongside your code, and boomโ€”your entire build process is automated, versioned, and ready for any changes. ๐Ÿ”„

There are two types of pipelines: Declarative (modern and easy to use) and Scripted (the older, more complex version). Weโ€™ll focus on the Declarative pipeline because itโ€™s super simple and efficient! ๐Ÿ’ก

Why do you need a pipeline? Hereโ€™s why:

  • Automation: Automatically build and test your project on every branch or pull request.

  • Versioning: Treat your pipeline like code, versioned and reviewed.

  • Efficiency: Save time and reduce errors by automating repetitive tasks. โฑ๏ธ

We'll walk you through setting up a Jenkins job that runs your pipeline, and we'll use a super simple "Hello World" example to get you started. You'll learn how to create a Jenkinsfile (the blueprint for your pipeline), commit it to your project, and watch Jenkins take care of the rest. ๐ŸŒŸ

By the end of this tutorial, you'll be all set to say goodbye to manual steps and hello to continuous integration and delivery! So, letโ€™s get started!


Prerequisites:

Before we jump in, make sure you have the following:

  1. Jenkins Installed: You need a running Jenkins instance. If you don't have it, you can install it on your system (Jenkins supports Windows, macOS, and Linux).

  2. A GitHub or Git Repository: Youโ€™ll need a project repository with code to work with (any small project will do).

  3. Basic Understanding of Jenkins: A simple understanding of Jenkins jobs and pipelines would be helpful, though not mandatory.

  4. Jenkins Pipeline Plugin Installed: Make sure the Jenkins Pipeline plugin is installed in your Jenkins instance to create pipeline jobs.

  5. Basic Knowledge of Groovy: Since Jenkins pipelines often use Groovy scripts, a little knowledge about it will come in handy. But donโ€™t worry if you're not a Groovy expert! We'll keep things simple.

Once you're ready with these, we can kickstart the process and create a powerful pipeline to automate your tasks! Let's go! ๐ŸŒŸ


What is a Jenkins Pipeline?๐Ÿค”

Think of a pipeline like an assembly line in a factory. It's a sequence of steps (or jobs) that get executed one after the other. Each step builds on the previous one. For Jenkins, a pipeline automates the entire process of building, testing, and deploying your code.

There are two types of pipelines:

  1. Declarative Pipeline: This is the new, easier way to define your pipeline using a clear structure and syntax.

  2. Scripted Pipeline: The traditional way, using Groovy code, which is more flexible but can be a bit complicated.

Why Should You Use a Pipeline?๐Ÿค”

By defining your Jenkins pipeline in a file (called a Jenkinsfile), you get several advantages:

  • Version Control: You can track your pipeline like any other code, making it easier to collaborate and review.

  • Automation: Pipelines can automatically run for every change (like creating new branches or pull requests), reducing manual work and ensuring consistency.

Now, let's dive into the task of creating your first Declarative Pipeline in Jenkins!


Pipeline Syntax Explained

Here's the basic syntax for a Jenkins Declarative Pipeline:

pipeline {
    agent any  // Can run on any available agent
    stages {    // Collection of stages (jobs)
        stage('Build') {
            steps {
                // Define steps for build (e.g., compile, package, etc.)
            }
        }
        stage('Test') {
            steps {
                // Define steps for tests (e.g., unit tests, integration tests)
            }
        }
        stage('Deploy') {
            steps {
                // Define deployment steps (e.g., push to server, cloud)
            }
        }
    }
}

Explanation of Syntax:

  • pipeline: Top-level block defining the whole pipeline.

  • agent any: Specifies that the pipeline can run on any available Jenkins agent.

  • stages: A collection of stage blocks, each representing a job in the pipeline.

    • stage('Build'): The first stage where code is compiled or built.

    • stage('Test'): The second stage where tests are executed.

    • stage('Deploy'): The last stage where the code is deployed.


Task-01: Creating Your First Jenkins Pipeline

Letโ€™s create a Jenkins Pipeline Job using the Declarative pipeline! ๐ŸŽ‰

Steps to Follow:

  1. Create a New Jenkins Job:

    • Go to Jenkins Dashboard.

    • Click on New Item.

    • Choose Pipeline (not Freestyle project).

    • Enter a name for your job (e.g., MyFirstPipeline).

    • Click OK.

  2. Configure the Pipeline:

    • Scroll down to the Pipeline section.

    • In the Definition dropdown, select Pipeline script.

    • Copy-paste the following Declarative pipeline code:

Jenkinsfile (Declarative Pipeline)
/* Requires the Docker Pipeline plugin */
pipeline {
    agent { docker { image 'node:22.12.0-alpine3.20' } }
    stages {
        stage('build') {
            steps {
                sh 'node --version'
            }
        }
    }
}
  1. Run the Pipeline:

    • Save your configuration and click on Build Now.

    • Watch your first Pipeline run

๐ŸŽ‰ Congratulations! You've just created your first Jenkins pipeline using Declarative syntax! Now you can automate the entire process of building, testing, and deploying your code.


And thatโ€™s a wrap! ๐ŸŽ‰ Youโ€™ve learned the building blocks of Jenkins pipelinesโ€”from understanding the difference between Declarative and Scripted to how pipelines function as code. Youโ€™re now equipped to start automating the delivery of your applications with the ease and flexibility of Jenkins.

By following this example, youโ€™ve created a simple pipeline, and with the knowledge gained, you can go on to create more complex and robust pipelines for your projects.

Remember, the more you play around with Jenkins, the more youโ€™ll master the art of continuous integration and continuous delivery. Keep experimenting, keep learning, and soon youโ€™ll be running pipelines like a pro!

So, grab that Jenkinsfile and start automating your workflowsโ€”because the future of DevOps is all about pipelines! ๐ŸŽฌ


๐Ÿ”— Follow my journey here:
๐Ÿ‘‰ LinkedIn
๐Ÿ‘‰ Hashnode
๐Ÿ‘‰ GitHub

Until next time, keep automating and innovating! ๐Ÿ’ก๐Ÿš€

Happy Learning! โœจ

ย