Jenkins: Pipelines and Jobs

By now, you should have a pretty good grasp of Jenkins fundamentals. But let’s be real—just running simple jobs isn’t enough. If you want to automate like a pro, you need pipelines. Pipelines let you define complex workflows as code, making your CI/CD processes repeatable, scalable, and way less prone to human error (or, as I call it, “Friday afternoon deployment disasters”).

In this module, we’ll tackle the different types of Jenkins pipelines, job automation strategies, and integration with GitHub. By the end, you’ll be setting up CI/CD pipelines that practically run themselves—leaving you more time to drink coffee and pretend you’re working.

Declarative and Scripted Pipelines

Introduction to Jenkins Pipeline as Code

Pipelines are the backbone of Jenkins automation. Instead of clicking around in the UI like a caveman, you define your build, test, and deploy processes in a Jenkinsfile. This makes your CI/CD setup version-controlled, reusable, and way less fragile.

Differences Between Declarative and Scripted Pipelines

FeatureDeclarative PipelineScripted Pipeline
Syntax ComplexitySimpler, YAML-likeMore flexible, Groovy-based
ReadabilityEasier to read & maintainCan be complex & harder to debug
Use CaseStandard CI/CD workflowsAdvanced logic & dynamic pipelines

Writing Basic Jenkinsfile Examples

Declarative Pipeline Example:

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

Scripted Pipeline Example:

node {
    stage('Build') {
        sh 'make build'
    }
    stage('Test') {
        sh 'make test'
    }
    stage('Deploy') {
        sh 'make deploy'
    }
}

Best Practices for Choosing the Right Pipeline Type

  • Use Declarative Pipelines for most cases—it’s easier to read and debug.
  • Choose Scripted Pipelines when you need complex logic, loops, or dynamic stages.
  • Keep pipelines modular—break large scripts into separate files for better maintainability.

Pipeline Stages and Steps

Defining Stages and Steps in a Pipeline

A pipeline is a series of stages, and each stage has steps. Think of it like a factory assembly line: one stage compiles, another tests, another deploys.

Using agent, stage, and steps Blocks in Jenkinsfile

  • agent any: Defines where the pipeline runs.
  • stage('Name'): Groups steps logically.
  • steps {}: Contains commands to execute.

Configuring Workspace and Resource Allocation

  • Use agent { label 'docker' } to run pipelines on specific nodes.
  • Allocate memory and CPU limits for resource-heavy builds.

Handling Conditional Execution with when Blocks

Execute steps based on conditions:

stage('Deploy') {
    when {
        branch 'main'
    }
    steps {
        echo 'Deploying to production!'
    }
}

Jenkins Plugins Management

Overview of Essential Jenkins Plugins for Pipelines

Jenkins without plugins is like a phone without apps—kinda useless. Some must-have plugins include:

  • Pipeline Plugin (core pipeline functionality)
  • Git Plugin (source control integration)
  • Blue Ocean (modern UI for pipeline visualization)
  • Credentials Binding (secure handling of secrets)

Installing and Managing Plugins via the UI and CLI

Using the UI:

  1. Go to Manage Jenkins > Manage Plugins.
  2. Search for the plugin and install it.

Using the CLI:

jenkins-cli.jar -s http://localhost:8080/ install-plugin pipeline-stage-view

Handling Plugin Updates and Compatibility Issues

  • Always test plugin updates in a staging environment.
  • Check for breaking changes before updating.

Multi-branch Pipelines and GitHub Integration

Setting Up Multi-branch Pipeline Jobs

Multi-branch pipelines automatically create jobs for each branch in a repository, making them ideal for teams with feature branches.

Configuring Jenkins to Detect New Branches Automatically

  1. Go to New Item > Multi-branch Pipeline.
  2. Select GitHub or Git as the source.
  3. Set the repository URL and scan for branches.

Connecting Jenkins with GitHub, GitLab, and Bitbucket

Use webhooks to trigger builds on repo changes.

curl -X POST http://jenkins.local:8080/github-webhook/

Webhooks and Automatic Build Triggers

  • Set up GitHub Webhooks to notify Jenkins on push events.
  • Use SCM Polling as a fallback (not as fast but reliable).

Hands-on Exercises

Let’s put theory into practice. Try these:

  • Create both a declarative and scripted pipeline.
  • Implement a multi-stage CI/CD pipeline with testing and deployment.
  • Set up GitHub integration and trigger builds on commits.
  • Install and explore essential Jenkins plugins for pipeline enhancement.

References

Because Jenkins will break, and you’ll need help:

By the end of this module, you’ll be building powerful, automated CI/CD pipelines like a pro. Now, go forth and let Jenkins do the work while you take all the credit. 🚀