DEV Community

Alex Aslam
Alex Aslam

Posted on

Jenkins 101: Your First Pipeline Made Easy (Declarative vs. Scripted)

Hey there, fellow coder! 👋 Let’s talk about something we’ve all faced: the chaos of manual builds. You know the drill—pushing code, running tests by hand, and crossing your fingers as you deploy. It’s like baking a cake but having to preheat the oven, mix the batter, and frost it every single time you want a slice. 🎂

Enter Jenkins, the granddaddy of CI/CD tools. It’s here to automate the boring stuff so you can focus on what matters: building awesome things. But let’s be real—Jenkins can feel intimidating at first. Declarative? Scripted? What’s the difference?

Don’t sweat it! By the end of this guide, you’ll have your first pipeline up and running, and you’ll finally understand which syntax to use (and when). Let’s dive in!


What’s a Jenkins Pipeline?

A pipeline is just a fancy word for a recipe your code follows from commit to deployment. It automates steps like:

  • ✅ Running tests
  • 🛠️ Building artifacts
  • 🚀 Deploying to servers

Think of it as your code’s personal assistant—no more missed steps or human errors.


Declarative vs. Scripted: What’s the Deal?

Jenkins offers two ways to write pipelines:

1. Declarative Pipelines (The “Chill” Option)

Declarative pipelines are like IKEA instructions—structured, opinionated, and hard to mess up. You tell Jenkins what to do, not how to do it.

Example: A simple pipeline to build and test a Node.js app:

pipeline {  
    agent any  // "Run this anywhere you have a worker"  
    stages {  
        stage('Build') {  
            steps {  
                echo 'Installing dependencies...'  
                sh 'npm install'  
            }  
        }  
        stage('Test') {  
            steps {  
                echo 'Running tests...'  
                sh 'npm test'  
            }  
        }  
    }  
}  
Enter fullscreen mode Exit fullscreen mode

Why you’ll love it:

  • Clean, readable syntax.
  • Built-in error handling and retries.
  • Perfect for straightforward workflows.

2. Scripted Pipelines (The “Mad Scientist” Option)

Scripted pipelines use raw Groovy code—unlimited power, but you’re responsible for the chaos.

Example: The same build, but with loops and custom logic:

node {  
    stage('Build') {  
        echo 'Installing dependencies...'  
        sh 'npm install'  
    }  
    stage('Test') {  
        def tests = ['unit', 'integration']  
        tests.each { test ->  
            echo "Running ${test} tests..."  
            sh "npm run test:${test}"  
        }  
    }  
}  
Enter fullscreen mode Exit fullscreen mode

Why you’ll love it:

  • Flexibility to do anything (conditionals, loops, etc.).
  • Great for complex, dynamic workflows.

Which One Should YOU Use?

Declarative Scripted
✅ Simple, readable syntax ✅ Ultimate flexibility
✅ Built-in error recovery ✅ Full Groovy scripting power
❌ Less customizable ❌ Steeper learning curve

Rule of thumb:

  • Start with Declarative for 90% of use cases.
  • Use Scripted only if you need loops, complex logic, or arcane wizardry.

Setting Up Your First Pipeline

Step 1: Install Jenkins

  1. Download Jenkins from jenkins.io.
  2. Run it locally or on a server (Docker fans: docker run -p 8080:8080 jenkins/jenkins:lts).

Step 2: Create a New Pipeline Job

  1. Open Jenkins → New Item → Pipeline.
  2. Under Pipeline, select Pipeline script and paste your Declarative/Scripted code.

Step 3: Hit “Build Now”

Watch your pipeline run live! 🎉

![Jenkins pipeline stages in action]


Common “Wait, Why Isn’t This Working?!” Moments

  1. Permission Errors:

    • Fix: Ensure Jenkins has access to tools like npm or docker.
  2. Syntax Typos:

    • Fix: Use the Pipeline Syntax Generator (Jenkins’ built-in cheat sheet).
  3. Missing Plugins:

    • Fix: Install plugins like NodeJS, Docker Pipeline, or Blue Ocean for a smoother UI.

Pro Tips for Jenkins Newbies

  1. Version Control Your Pipelines: Save your Jenkinsfile in your repo to track changes.
  2. Use the Blue Ocean Plugin: It’s Jenkins… but pretty.
  3. Start Small: Automate one task (like testing), then add stages later.

Final Thought: Embrace the Automation

Jenkins might feel like a relic sometimes, but it’s still a powerhouse for CI/CD. Whether you choose Declarative’s simplicity or Scripted’s flexibility, you’re unlocking a world where deployments happen while you sleep.

Your Homework:

  1. Write a Declarative pipeline that runs your tests.
  2. Add a Scripted stage to send a Slack alert on failure.
  3. Bask in the glory of automation. 😎

Stuck? Drop a comment below—let’s troubleshoot together! 🛠️

Top comments (0)