Day 26 Task: Jenkins Declarative Pipeline

🔹What is Pipeline?

In the context of Jenkins, a "pipeline" refers to a series of automated steps or jobs that are organized in a sequence to achieve a specific task or workflow. Pipelines are a fundamental concept in Jenkins, used for defining and automating the steps required to build, test, and deploy software applications.

There are two main ways to define and create pipelines in Jenkins: Declarative and Scripted.

Declarative Pipeline:

  • Declarative is a more recent and user-friendly way to define pipelines in Jenkins.

  • It is designed to provide a simpler and more structured syntax for defining pipelines.

  • Declarative pipelines use a set of predefined, high-level directives and keywords to define stages and steps in a pipeline.

  • These pipelines are easier to read, write, and maintain, making them a preferred choice for many Jenkins users, especially those who are new to pipeline scripting.

  • Declarative pipelines encourage best practices and provide good defaults, making them a safer and more controlled way to define pipelines.

    Declarative Pipeline Example:

      pipeline {
          agent any
    
          stages {
              stage('Build') {
                  steps {
                      sh 'echo "Building the application"'
                  }
              }
              stage('Test') {
                  steps {
                      sh 'echo "Running tests"'
                  }
              }
              stage('Deploy') {
                  steps {
                      sh 'echo "Deploying the application"'
                  }
              }
          }
      }
    

    In this Declarative Pipeline example:

    • We specify that the pipeline can run on any available agent.

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

    • Each stage contains a single step, which is a shell command (sh) that prints a message.

    • Declarative pipelines use a predefined structure with pipeline, stages, and steps directives to simplify pipeline definition.

Scripted Pipeline:

  • Scripted pipelines were the original way to define pipelines in Jenkins and are built using Groovy, a powerful scripting language.

  • Scripted pipelines offer more flexibility and control over the pipeline's logic, making them suitable for complex and custom workflows.

  • In scripted pipelines, you write custom Groovy scripts to define the pipeline's stages and steps.

  • This flexibility comes at the cost of increased complexity, as scripted pipelines can become harder to read and maintain, especially for users who are not familiar with Groovy.

  • Scripted pipelines are still widely used when intricate customization or advanced logic is required for a pipeline.

    Scripted Pipeline Example:

      node {
          stage('Build') {
              echo 'Building the application'
              // Additional build steps can be added here
          }
          stage('Test') {
              echo 'Running tests'
              // Additional test steps can be added here
          }
          stage('Deploy') {
              echo 'Deploying the application'
              // Additional deployment steps can be added here
          }
      }
    

    In this Scripted Pipeline example:

    • We start with a node block, which specifies where the pipeline should run.

    • Within each stage, we use the echo command to print messages.

    • Scripted pipelines provide more flexibility to define custom logic and execute arbitrary code within each stage.

🔹Why you should have a Pipeline?

  1. Automation: Pipelines automate the repetitive and manual aspects of your software development process, such as building, testing, and deployment. This reduces human error, speeds up the development cycle, and frees up developers to focus on more creative and critical tasks.

  2. Consistency: Pipelines enforce consistency in your development and release processes. Every code change goes through the same set of stages, ensuring that all code is built, tested, and deployed in a standardized way. This consistency helps catch issues early in the development process.

  3. Reliability: Pipelines improve the reliability of your software by running automated tests at various stages. This helps identify and fix bugs and issues before they reach production, leading to more stable and dependable software.

  4. Visibility: Pipelines provide visibility into the status and progress of your software development process. You can easily see which stage a code change is in, whether it passed or failed tests, and where issues might be occurring. This transparency facilitates collaboration and troubleshooting.

  5. Scalability: As your project grows, pipelines can scale with it. You can add more stages or parallelize tasks to accommodate larger development teams or more complex software architectures.

  6. Traceability: Pipelines create a clear audit trail of code changes and their associated actions. You can track who made changes, what was changed, and when it happened, which is essential for compliance, debugging, and accountability.

  7. Efficiency: By automating time-consuming tasks like building and testing, pipelines make your development process more efficient. Developers spend less time waiting for builds and can focus on writing code.

  8. Deployment Safety: Pipelines can include automated deployment steps with rollback capabilities. This ensures that new code releases can be deployed with confidence, knowing that you can quickly revert to a previous version in case of issues.

  9. Continuous Integration and Continuous Delivery (CI/CD): Pipelines are a core component of CI/CD practices. They enable frequent integration of code changes into a shared repository, automated testing, and the rapid and reliable delivery of code changes to production environments.

  10. Adaptability: Pipelines are highly adaptable. You can customize them to meet the specific needs of your project, incorporating security checks, performance testing, and other essential steps.

🔹Task-01

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

    1. From the Jenkins dashboard, click on "New Item" to create a new project.

    2. Enter a project name and select "Pipeline" Click "OK" to create the project.

    3. Inside the Pipeline section Type the declarative pipeline script for Hello World and click on save.

    4. click on Build Now.