DevOps(Day-26) : Jenkins Declarative Pipeline

DevOps(Day-26) : Jenkins Declarative Pipeline

TABLE OF CONTENTS

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

Pipeline and it's types

What is a Pipeline?

A pipeline is a set or collection of processes that help developers and operations to work smartly to build and deploy the code to the production environment.

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.

Difference between Declarative and Scripted pipeline.

Declarative limits what is available to the user with a more strict and pre-defined structure, making it an ideal choice for simpler continuous delivery pipelines. Scripted provides very few limits, insofar that the only limits on structure and syntax tend to be defined by Groovy itself, rather than any Pipeline-specific systems, making it an ideal choice for power users and those with more complex requirements.

As the name implies, Declarative Pipeline encourages a declarative programming model, Whereas Scripted Pipelines follow a more imperative programming model.

Why you should have a Pipeline?

The definition of a Jenkins Pipeline is written into a text file called Jenkins file 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 a Jenkins file and committing it to source control provides several 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).

Syntax

Task: Realtime example

Let's create a Pipeline project.

  1. Navigate to the Jenkins home page. Create a new Job and select Pipeline.

  2. Now configure the project with valid description.

  3. Now, navigate to the Pipeline section to write the groovy pipeline code.

    Here we are writing a simple code to print "Hello World"

    COPY

      pipeline {
          agent any
    
          stages {
              stage('Jenkins for DevOps') {
                  steps {
                      echo 'Hello World'
                  }
              }
          }
      }
    
  4. Click on Save and then build now.

    You can check the details regarding the time of execution.

  5. Let's see the output.

    We have completed our first real-time example of building a pipeline through a declarative pipeline script.

Thanks for reading my article. Have a nice day.

WRITTEN BY Biswaraj Sahoo --AWS Community Builder | DevOps Engineer | Docker | Linux | Jenkins | AWS | Git | Terraform | Docker | kubernetes

Empowering communities via open source and education. Connect with me over linktree: linktr.ee/biswaraj333