Jenkins- Create Pipelines
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)
- Head to https://www.jenkins.io/download/
- Follow the install instructions (Docker users, you can pull the official image and go nuts)
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
- Go to Jenkins Dashboard → New Item
- Choose Pipeline, give it a name, and hit OK
- Scroll down to the Pipeline section
- 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 groovyCopyEdit
stage('Tests') { parallel { stage('Unit') { steps { sh './gradlew test' } } stage('Integration') { steps { sh './gradlew integrationTest' } } } }
- Post actions groovyCopyEdit
post { success { echo 'All good!' } failure { echo 'Something exploded.' } always { cleanWs() } }
- Parameters groovyCopyEdit
parameters { 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
- Version control your Jenkinsfile – store it in the root of your repo.
- Name your stages well – makes reading build logs easier.
- Keep it DRY – use shared libraries for common logic across pipelines.
- Use tools like Blue Ocean or Jenkins’ built-in visualization to see your pipeline like a flowchart.
- 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.”