Day 26 : Jenkins Declarative Pipeline

Day 26 : Jenkins Declarative Pipeline

DevOps Learning

Let’s break down the essentials of Jenkins pipelines.

  1. What Is a Pipeline?

    • A pipeline in Jenkins is like a recipe for building, testing, and deploying your software.

    • It’s a sequence of steps (or jobs) that guide your code from development to production.

  2. Declarative vs. Scripted Pipelines:

    1. Declarative Pipeline Example:

      • Declarative pipelines are designed to be straightforward and easy to read. They follow a structured syntax. Here’s a simple example:

          pipeline {
              agent any
              stages {
                  stage('Build') {
                      steps {
                          sh 'echo Building...'
                      }
                  }
                  stage('Test') {
                      steps {
                          sh 'echo Testing...'
                      }
                  }
                  stage('Deploy') {
                      steps {
                          sh 'echo Deploying...'
                      }
                  }
              }
          }
        
      • In this declarative pipeline:

        • We define three stages: Build, Test, and Deploy.

        • Each stage contains a single step (a shell command).

    2. Scripted Pipeline Example:

      • Scripted pipelines provide more flexibility but require knowledge of Groovy. Here’s an equivalent scripted example:

          node {
              stage('Build') {
                  echo 'Building...'
              }
              stage('Test') {
                  echo 'Testing...'
              }
              stage('Deploy') {
                  echo 'Deploying...'
              }
          }
        
      • In this scripted pipeline:

        • We use the node block to specify the machine executing the pipeline.

        • Each stage directly contains steps (using echo for simplicity).

Key Differences:

  • Syntax:

    • Declarative: Structured, with specific keywords like pipeline, stages, and steps.

    • Scripted: More free-form, using blocks like node and direct commands.

  • Ease of Use:

    • Declarative: Easier for beginners due to its succinct syntax.

    • Scripted: Offers more power but requires understanding Groovy.

  • Flexibility:

    • Declarative: Opinionated; enforces best practices.

    • Scripted: Highly flexible; allows complex logic.

  1. Why Use a Pipeline?

    • Imagine your CD (Continuous Delivery) pipeline as part of your application code.

    • Benefits:

      • Version Control: Define your pipeline in a text file (Jenkinsfile) and store it alongside your app’s code.

      • Consistency: Every branch and pull request gets the same pipeline treatment.

      • Code Review: Review and iterate on your pipeline just like any other code.

Remember, Jenkins pipelines are all about automating your software delivery process. Whether you’re building a web app, a mobile app, or something else entirely, pipelines help you ship your code with confidence! 🚀.

TASK:

  • Create a New Job, this time select Pipeline instead of Freestyle Project.

  • Create a Declarative pipeline to run FlaskApp on a docker container.

Solution:

  • Create Pipeline.

  • Follow the configuration as mentioned.

    • Click Save and Build Now.

Thank you for reading😉.