DEV Community

DevOps Man
DevOps Man

Posted on

πŸ’₯ Master Jenkins Pipelines in One Go – With a Universal Jenkinsfile Template

Whether you're a DevOps engineer, backend developer, or automation enthusiast, mastering Jenkins pipelines is a must. In this guide, we’ll break down everything you need to know to write powerful, flexible Jenkins pipelines using Groovy β€” all in one place. You'll also get a universal Jenkinsfile that you can use across projects and tweak to your needs.


πŸš€ What is a Jenkinsfile?

A Jenkinsfile is a text file that contains the definition of a Jenkins pipeline and is checked into your source control (GitHub, GitLab, Bitbucket, etc). Instead of configuring jobs in the Jenkins UI, you define everything as code – which makes it version-controlled, repeatable, and collaborative.


🧠 Declarative vs Scripted Pipeline: What’s the Difference?

Feature Declarative Scripted
Syntax Structured (block-based) Freestyle (Groovy code)
Readability Easier to read and write More flexible, less readable
Groovy Knowledge Minimal required Moderate to advanced Groovy needed
Recommended For Most users/projects Complex, dynamic logic

πŸ“š Groovy Concepts You Need to Know for Jenkins Pipelines

Jenkins pipelines are written in Groovy, but you only need a subset of it:

βœ… Variables

def name = "Jenkins" 
Enter fullscreen mode Exit fullscreen mode

βœ… String Interpolation

echo "Hello ${name}" 
Enter fullscreen mode Exit fullscreen mode

βœ… Lists & Maps

def servers = ["web1", "web2"] def config = [env: "prod", region: "us"] 
Enter fullscreen mode Exit fullscreen mode

βœ… Conditionals & Loops

if (env == "prod") { echo "Production deploy" } servers.each { s -> echo "Deploying to ${s}" } 
Enter fullscreen mode Exit fullscreen mode

βœ… Functions

def greet(name) { echo "Hi ${name}" } 
Enter fullscreen mode Exit fullscreen mode

βœ… Closures
Used behind the scenes with blocks like steps {} and stage {}.


🧱 Jenkins Declarative Pipeline Concepts (Explained)

pipeline { }
Root block that wraps everything.

agent
Defines where the pipeline runs (any, docker, specific label).

agent any 
Enter fullscreen mode Exit fullscreen mode

stages { } & stage { }
Defines the steps in your CI/CD lifecycle.

stages { stage('Build') { steps { echo "Building..." } } } 
Enter fullscreen mode Exit fullscreen mode

steps { }
Actual shell commands or Jenkins steps.

steps { sh 'npm install' echo "Done" } 
Enter fullscreen mode Exit fullscreen mode

environment
Set environment variables.

environment { NODE_ENV = "production" VERSION = "${env.BUILD_ID}" } 
Enter fullscreen mode Exit fullscreen mode

when
Conditional execution of stages.

when { branch 'main' } 
Enter fullscreen mode Exit fullscreen mode

post
Defines actions after success/failure/always.

post { success { echo "βœ”οΈ Success" } failure { echo "❌ Failed" } always { cleanWs() } } 
Enter fullscreen mode Exit fullscreen mode

parameters
Accept runtime input to your pipeline.

parameters { string(name: 'DEPLOY_ENV', defaultValue: 'staging', description: 'Choose env') booleanParam(name: 'RUN_TESTS', defaultValue: true, description: 'Run tests?') } 
Enter fullscreen mode Exit fullscreen mode

credentials
Access stored secrets securely.

withCredentials([usernamePassword(credentialsId: 'git-creds', usernameVariable: 'USER', passwordVariable: 'PASS')]) { sh 'git push https://${USER}:${PASS}@repo.git' } 
Enter fullscreen mode Exit fullscreen mode

tools
Preinstalled tools like JDK, Maven, etc.

tools { maven 'Maven 3.8.1' } 
Enter fullscreen mode Exit fullscreen mode

script
For advanced Groovy logic inside declarative pipelines.

script { def result = "build_${env.BUILD_NUMBER}" echo result } 
Enter fullscreen mode Exit fullscreen mode

🌐 Universal Jenkinsfile Template (Copy-Paste & Customize)

pipeline { agent any options { timeout(time: 15, unit: 'MINUTES') buildDiscarder(logRotator(numToKeepStr: '10')) skipDefaultCheckout() } parameters { string(name: 'DEPLOY_ENV', defaultValue: 'staging', description: 'Target environment') booleanParam(name: 'RUN_TESTS', defaultValue: true, description: 'Run tests before build') } environment { APP_NAME = "universal-app" BUILD_TAG = "${env.BUILD_NUMBER}-${new Date().format('yyyyMMdd-HHmm')}" } stages { stage('Init') { steps { echo "πŸš€ Starting pipeline for ${APP_NAME}" checkout scm } } stage('Install Dependencies') { steps { script { if (fileExists('package.json')) { echo "Node project detected" sh 'npm install' } else if (fileExists('requirements.txt')) { echo "Python project detected" sh 'pip install -r requirements.txt' } else { echo "Unknown project type" } } } } stage('Test') { when { expression { return params.RUN_TESTS } } steps { script { try { sh 'npm test || echo "Tests skipped/failing gracefully"' } catch (e) { echo "Test failure ignored for now" } } } } stage('Build') { steps { script { echo "πŸ› οΈ Building ${APP_NAME} - ${BUILD_TAG}" sh 'mkdir -p build && echo "Build successful" > build/status.txt' } } } stage('Deploy') { when { anyOf { branch 'main' expression { params.DEPLOY_ENV == 'production' } } } steps { echo "πŸš€ Deploying to ${params.DEPLOY_ENV}" sh './deploy.sh ${params.DEPLOY_ENV} || echo "Deploy script not found"' } } stage('Parallel Quality Checks') { parallel { stage('Lint') { steps { echo "πŸ” Linting code..." sh 'sleep 3' } } stage('Security Scan') { steps { echo "πŸ” Running security scan..." sh 'sleep 3' } } } } } post { success { echo "βœ… Pipeline completed successfully" } failure { echo "❌ Pipeline failed" } always { echo "🧹 Cleaning up workspace" cleanWs() } } } 
Enter fullscreen mode Exit fullscreen mode

🎯 Final Thoughts
With this article, you now:

  • Understand how Jenkins pipelines work

  • Know the key Groovy concepts used inside Jenkins

  • Can write and reuse a universal Jenkinsfile

  • Are ready to build, test, deploy, and automate like a DevOps pro πŸš€

Top comments (0)