Jenkins Declarative Pipeline

Jenkins Declarative Pipeline

Jenkins Pipeline is a comprehensive set of plugins designed to facilitate the implementation and automation of continuous delivery and continuous integration (CI/CD) pipelines within Jenkins. A pipeline in Jenkins is a set of instructions for orchestrating software applications, building, testing, and deployment. It allows you to define the entire build process, from source code to deployment, as code in a script.

Jenkins Pipeline supports two main syntaxes for defining pipelines: Declarative and Scripted Pipeline.

  1. Declarative Pipeline: Uses a structured, more human-readable syntax. Declarative pipelines define pipelines using a domain-specific language (DSL) with a predefined structure. This structure includes stages, steps, and other key elements that help define the Pipeline's flow. Declarative pipelines are typically written in a Jenkinsfile, a text file describing the entire build process. It is also easier to read and write, especially for simple use cases and standard build processes.

    Example of a Declarative Pipeline:

     pipeline {
         agent any
         stages {
             stage('Build') {
                 steps {
                     //
                 }
             }
             stage('Test') {
                 steps {
                     //
                 }
             }
             stage('Deploy') {
                 steps {
                     //
                 }
             }
         }
     }
    
  2. Scripted Pipeline: Uses a more flexible, Groovy-based scripting syntax and allows for more complex and dynamic build processes using Groovy scripting. Scripted Pipeline is suitable for advanced use cases requiring fine-grained control and flexibility. It requires more scripting knowledge compared to the Declarative Pipeline.

    Example of a Scripted Pipeline:

     node {
         stage('Build') {
             // Code to build the project
         }
         stage('Test') {
             // Code to run tests
         }
         stage('Deploy') {
             // Code to deploy the application
         }
     }
    

    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

    Console Output:

    Now, to complete the example using a declarative pipeline