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" β
String Interpolation
echo "Hello ${name}" β
Lists & Maps
def servers = ["web1", "web2"] def config = [env: "prod", region: "us"] β
Conditionals & Loops
if (env == "prod") { echo "Production deploy" } servers.each { s -> echo "Deploying to ${s}" } β
Functions
def greet(name) { echo "Hi ${name}" } β
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 stages { } & stage { }
Defines the steps in your CI/CD lifecycle.
stages { stage('Build') { steps { echo "Building..." } } } steps { }
Actual shell commands or Jenkins steps.
steps { sh 'npm install' echo "Done" } environment
Set environment variables.
environment { NODE_ENV = "production" VERSION = "${env.BUILD_ID}" } when
Conditional execution of stages.
when { branch 'main' } post
Defines actions after success/failure/always.
post { success { echo "βοΈ Success" } failure { echo "β Failed" } always { cleanWs() } } 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?') } credentials
Access stored secrets securely.
withCredentials([usernamePassword(credentialsId: 'git-creds', usernameVariable: 'USER', passwordVariable: 'PASS')]) { sh 'git push https://${USER}:${PASS}@repo.git' } tools
Preinstalled tools like JDK, Maven, etc.
tools { maven 'Maven 3.8.1' } script
For advanced Groovy logic inside declarative pipelines.
script { def result = "build_${env.BUILD_NUMBER}" echo result } π 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() } } } π― 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)