Jenkins- Create Pipelines

By: zigmoid
Posted on: 04/25/2025

Welcome to the land of pipelines—where your code gets tested, built, and deployed without you lifting a finger after your git push. This isn’t just automation. It’s orchestration. Think symphony, not solo.

In Jenkins, Pipelines are the sexy, scalable, scriptable way to define your build process. This guide will walk you through creating your first pipeline, explain how it works, and drop some pro-tips to level up your game.


🛠️ What is a Jenkins Pipeline?

A Pipeline in Jenkins is a collection of stages that define your CI/CD process. It’s written in a Groovy-based DSL (Domain Specific Language), which lets you define your build, test, and deployment steps as code.

This means:

  • No more clicking through the Jenkins UI like it’s Minesweeper.
  • Full version control of your CI/CD process.
  • Reusability, readability, and way less “WTF does this job even do?”

🧱 Types of Jenkins Pipelines

1. Declarative Pipeline

This is the friendlier, more structured way to define a pipeline. It’s the go-to for most users.

groovyCopyEditpipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing...'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying...'
            }
        }
    }
}

2. Scripted Pipeline

This one is more powerful but less strict. Think “Groovy playground.” It’s good for complex workflows or conditional logic.

groovyCopyEditnode {
    stage('Build') {
        echo 'Building...'
    }
    stage('Test') {
        echo 'Testing...'
    }
    stage('Deploy') {
        echo 'Deploying...'
    }
}

Unless you have very specific needs, stick to Declarative. It’s cleaner, and Jenkins plays nicer with it.


⚙️ Setting Up Your First Pipeline in Jenkins

Here’s how to set up a pipeline in Jenkins like a boss:

1. 🔧 Install Jenkins (if you haven’t already)

2. 📦 Install Required Plugins

Make sure these are installed:

  • Pipeline
  • Git
  • Blue Ocean (optional, but gives you a slick UI)

3. 🛠️ Create a New Pipeline Job

  1. Go to Jenkins DashboardNew Item
  2. Choose Pipeline, give it a name, and hit OK
  3. Scroll down to the Pipeline section
  4. Choose Pipeline script or Pipeline script from SCM if you’re loading it from GitHub

4. ✍️ Write Your Pipeline (Jenkinsfile)

Here’s a real-world-ish sample pipeline:

groovyCopyEditpipeline {
    agent any

    environment {
        BUILD_ENV = 'dev'
    }

    stages {
        stage('Checkout') {
            steps {
                git url: 'https://github.com/your/repo.git', branch: 'main'
            }
        }

        stage('Build') {
            steps {
                sh './gradlew build'
            }
        }

        stage('Test') {
            steps {
                sh './gradlew test'
            }
        }

        stage('Package') {
            steps {
                sh './gradlew assemble'
                archiveArtifacts artifacts: '**/build/libs/*.jar', fingerprint: true
            }
        }

        stage('Deploy') {
            steps {
                echo "Deploying to ${BUILD_ENV} environment"
                sh './scripts/deploy.sh'
            }
        }
    }
}

This pipeline:

  • Pulls your code from GitHub
  • Builds it using Gradle
  • Runs tests
  • Packages the app and archives the artifacts
  • Deploys to a dev environment

Boom. One file to rule them all.


🎛️ Pipeline Features You Should Totally Use

  • Parallel stages groovyCopyEditstage('Tests') { parallel { stage('Unit') { steps { sh './gradlew test' } } stage('Integration') { steps { sh './gradlew integrationTest' } } } }
  • Post actions groovyCopyEditpost { success { echo 'All good!' } failure { echo 'Something exploded.' } always { cleanWs() } }
  • Parameters groovyCopyEditparameters { string(name: 'BRANCH_NAME', defaultValue: 'main', description: 'Git Branch to Build') }
  • Environment Variables
    • Define them globally or per stage.
    • Use env.MY_VAR to access them in scripts.

💡 Pro Tips for Pipeline Greatness

  1. Version control your Jenkinsfile – store it in the root of your repo.
  2. Name your stages well – makes reading build logs easier.
  3. Keep it DRY – use shared libraries for common logic across pipelines.
  4. Use tools like Blue Ocean or Jenkins’ built-in visualization to see your pipeline like a flowchart.
  5. Always include error handling in deploy stages—because deployments fail. Often. Violently.

🔚 Wrap-Up: You Now Speak Pipeline

Creating pipelines in Jenkins isn’t just about automation—it’s about creating repeatable, reliable, and readable processes that your future self (and your teammates) will thank you for.

Once you nail pipelines, Jenkins goes from “meh, another tool” to “yo, this is actually dope.”