Day 26 Task: Jenkins Declarative Pipeline

Day 26 Task: Jenkins Declarative Pipeline

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

  • Some terms for your Knowledge

What is a 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 a Jenkinsfile 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 {
        agent any
        stages {
            stage('Build') {
                steps {
                    //
                }
            }
            stage('Test') {
                steps {
                    //
                }
            }
            stage('Deploy') {
                steps {
                    //
                }
            }
        }
    }

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

  • In case of any issues feel free to post on any Groups, Discord or Telegram

  • Here are the step-by-step instructions to complete Task-01 for Day 26 which involves creating a Jenkins Declarative Pipeline job and following the official Jenkins Hello World example:

    1. Access Your Jenkins Server: Ensure you have access to your Jenkins server's web interface.

    2. Log In: Log in to your Jenkins server with the appropriate credentials.

    3. Create a New Jenkins Pipeline Job:

      • Click on "New Item" or "Create a new job" from the Jenkins dashboard.
    4. Provide a Name:

      • Enter a name for your pipeline job in the "Enter an item name" field like "Hello-world"

      • Select the "Pipeline" option.

    5. Configure Pipeline:

      • 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."

    6. Define the Declarative Pipeline:

      • 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!'
                      }
                  }
              }
          }
        

        This simple pipeline defines one stage called "Build" that echoes "Hello, World!" when executed.

    7. Save the Job Configuration:

      • Click the "Save" button to save your pipeline job configuration.
    8. Build the Pipeline:

      • Go back to the Jenkins dashboard and find your newly created pipeline job.

      • Click on the job to open it.

    9. Build the Pipeline:

      • In the left-hand menu, click on "Build Now" to manually trigger a build of your pipeline.
    10. View the Pipeline Execution:

      • After triggering the build, you can view the pipeline execution by clicking on the build number under the "Build History" section.
    11. Check the Console Output:

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

By following these steps, you'll have created a Jenkins Declarative Pipeline job that executes a simple "Hello, World!" example. This is a foundational step in understanding how to create and use Jenkins pipelines for continuous integration and continuous delivery (CI/CD) purposes.


Happy Learning

Thanks For Reading! :)

-Sriparthu💝💥