Day 26 Jenkins Declarative Pipeline

Day 26 Jenkins Declarative Pipeline

Day 26 of #90daysofdevops

Hey Techies! Welcome to this blog

In this blog, we are going to start the Jenkins Declarative Pipeline.

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 Syntax

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                //
            }
        }
        stage('Test') {
            steps {
                //
            }
        }
        stage('Deploy') {
            steps {
                //
            }
        }
    }
}

In this example:

  • agent: Specifies where the pipeline will run.

  • stages: Represents different phases of the build process.

  • steps: Contains the specific actions to be performed within each stage.

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

Now, let's tackle the specific task outlined:

Create a New Job

  1. Open your Jenkins instance and click on "New Item."

  2. Enter a name for your job (e.g., "HelloWorldPipeline") and choose "Pipeline" as the type.

  3. Click on "OK" to create the job.

Follow the Official Jenkins Hello World Example

  1. In your pipeline job configuration, scroll down to the "Pipeline" section.

  2. Choose "Pipeline script" in the Definition dropdown.

  3. Save the configuration.

Complete the Example using Declarative Pipeline

Now, let's modify the pipeline script to use Declarative syntax.

  pipeline {
      agent any
      stages {
          stage('Build') {
              steps {
                  echo 'Hello World'
              }
          }
      }
  }

Save and Run the Pipeline

  1. Save the pipeline script.

  2. Trigger a build manually or wait for any configured triggers.

  3. Observe the pipeline progress in the Jenkins dashboard.

Check the Full Stage View for Execution Time

  1. Once the pipeline execution starts, we can navigate to the "Full Stage View" to see the different stages of our pipeline and the time it takes for each stage to complete.

Verify Hello World Using Console Output

  1. After the pipeline has been completed, we can check the "Console Output" to see the detailed logs of each step's execution.

  2. If everything went as expected, we should see a "Success" status in the console output, indicating that the "Hello World" pipeline was executed successfully.

That's it! You've just completed the task. 🎉

Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Happy Learning!

~kritika :)

Connect with me: linkedin.com/in/kritikashaw