Day 26 Task: Jenkins Declarative Pipeline

Day 26 Task: Jenkins Declarative Pipeline

What is a Pipeline?

In Jenkins, a pipeline is a set of automated processes that allow developers to build, test, and deploy code efficiently. It's represented as a series of steps, where each step performs a specific task.

Types of Pipelines:

  1. Scripted Pipeline:

    • Syntax: Written in Groovy scripting language.

    • Flexibility: Offers greater flexibility and control over the pipeline flow.

    node {
        stage('Build') {
            // Build stage
        }
        stage('Test') {
            // Test stage
        }
        stage('Deploy') {
            // Deployment stage
        }
    }
  1. Declarative Pipeline:

    • Syntax: Structured format with predefined sections like agent, stages, and steps.

    • Simplicity: Provides a simpler and more opinionated syntax for defining pipelines.

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

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).

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