3 Methods to Create a Jenkins Pipeline: Classic UI, Blue Ocean, and Git

Jenkins, a cornerstone of modern DevOps, streamlines the automation of software build, testing, and delivery processes. This comprehensive guide details three distinct methods for creating Jenkins pipelines: leveraging the Classic UI, the streamlined Blue Ocean interface, and employing direct Git integration. Mastering these approaches empowers you to optimize your CI/CD workflows and achieve superior software delivery efficiency.

Building Jenkins Pipelines with the Classic UI

The classic Jenkins interface, while perhaps less visually appealing than Blue Ocean, provides a robust and highly configurable method for defining pipelines. This section outlines the step-by-step process.

Defining the Pipeline Script

The foundation of a Jenkins pipeline built within the Classic UI is a well-structured pipeline script. This script, typically written in Groovy, defines each stage of your build process. Consider the following example:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('Deploy') {
            steps {
                sh 'kubectl apply -f deployment.yaml'
            }
        }
    }
}

This script outlines three stages: build, test, and deploy. Each stage encapsulates specific commands executed within the pipeline. The agent any directive specifies that the pipeline can run on any available Jenkins agent.

Configuring Build Parameters

For enhanced flexibility, incorporate build parameters into your pipeline script. This allows you to dynamically adjust pipeline behavior based on user input or environment variables.

pipeline {
    agent any
    parameters {
        string(name: 'DEPLOY_ENV', defaultValue: 'dev', description: 'Deployment Environment')
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('Deploy') {
            steps {
                sh "kubectl apply -f deployment-${params.DEPLOY_ENV}.yaml"
            }
        }
    }
}

This modified script introduces a string parameter DEPLOY_ENV, allowing selection of the deployment environment (e.g., ‘dev’, ’test’, ‘prod’). The script uses this parameter to select the appropriate deployment YAML file.

Creating a New Pipeline Job

Once your pipeline script is ready, navigate to your Jenkins dashboard, click “New Item,” and provide a descriptive name for your new pipeline job. Select “Pipeline” as the job type and click “OK.”

Configuring the Pipeline Job

Within the job configuration, paste your Groovy pipeline script into the “Pipeline” section under “Definition.” Configure other settings like build triggers (e.g., Git polling) and post-build actions as needed. Save your changes, and you’re ready to execute your pipeline.

Leveraging Jenkins Blue Ocean for Pipeline Creation

Jenkins Blue Ocean offers a visually intuitive interface for designing and managing pipelines. Its drag-and-drop functionality significantly simplifies the pipeline creation process.

Visual Pipeline Construction

Blue Ocean provides a visual editor where you can construct your pipeline by dragging and dropping stages, steps, and parameters. This eliminates the need for manual Groovy scripting, making it more accessible to users with less programming experience.

Pipeline Stages and Steps

Each stage in the visual editor represents a distinct phase in your pipeline (e.g., build, test, deploy). Within each stage, you can add steps, specifying individual tasks or commands. Blue Ocean offers pre-built steps for common tasks, further simplifying the configuration process.

Integrated Visualization and Feedback

Blue Ocean’s strength lies in its integrated visualization capabilities. It presents a clear, concise representation of your pipeline’s execution, including progress, successes, and failures. This immediate feedback significantly speeds up debugging and troubleshooting.

Creating a Blue Ocean Pipeline

To create a new pipeline in Blue Ocean, select “New Pipeline” from the dashboard. Blue Ocean automatically detects and configures various sources (e.g., Git repositories). Follow the on-screen instructions, selecting your source and configuring any necessary parameters.

Utilizing Git for Pipeline Definition

For teams practicing GitOps, defining Jenkins pipelines directly within the Git repository provides version control and collaboration benefits. This approach integrates seamlessly with established development workflows.

Defining the Jenkinsfile

The core of this method is the Jenkinsfile, a Groovy script located within your Git repository. This file defines the pipeline’s structure and execution logic. The Jenkinsfile should reside at the root of your project repository.

Jenkins Configuration

Configure your Jenkins job to pull the pipeline definition from the specified Git repository. Jenkins will automatically detect and utilize the Jenkinsfile located in the repository. This eliminates the need to manually enter or manage the pipeline script within the Jenkins UI.

Git Branching Strategies

Effectively manage different pipeline versions by using Git branching. Develop and test pipeline changes in separate branches, then merge them into the main branch after thorough validation. This ensures stability and reduces the risk of production disruptions.

Pipeline as Code

This approach embodies the concept of “Pipeline as Code,” a core DevOps practice. Treating your pipeline definition as code enables you to leverage version control, code review processes, and automated testing methodologies.

Benefits of Git Integration

  • Version Control: Track changes to your pipeline definitions over time, facilitating rollback and auditability.
  • Collaboration: Facilitate collaborative pipeline development among team members.
  • Reproducibility: Ensure consistent and reliable pipeline execution across different environments.
  • Automation: Seamlessly integrate pipeline changes with your continuous integration and delivery processes.

By mastering these three methods – the Classic UI, Blue Ocean, and Git integration – you will significantly enhance your ability to build, manage, and optimize Jenkins pipelines, driving efficiency and improving your overall software delivery lifecycle. Remember to carefully consider the specific needs and context of your projects when selecting the most appropriate approach.