Jenkins: Shared Libraries, Plugins & Best Practices
Shared Libraries
Shared libraries allow you to extract common pipeline logic into reusable Groovy code stored in a separate repo. Import with @Library annotation.
// In Git repo: jenkins-shared-libs/
// vars/deployApp.groovy — global variable (callable as a step)
def call(Map config) {
def env = config.environment ?: 'staging'
def image = config.image
sh "kubectl set image deployment/${config.app} app=${image} -n ${env}"
sh "kubectl rollout status deployment/${config.app} -n ${env}"
}
// vars/notify.groovy
def slack(String channel, String message, String color = 'good') {
slackSend channel: channel, color: color, message: message
}
// src/com/example/Docker.groovy — class (imported explicitly)
package com.example
class Docker implements Serializable {
def steps
Docker(steps) { this.steps = steps }
def build(String name, String tag) {
steps.sh "docker build -t ${name}:${tag} ."
}
}
// Usage in Jenkinsfile:
@Library('jenkins-shared-libs@main') _
pipeline {
stages {
stage('Deploy') {
steps {
deployApp(app: 'my-app', image: "my-app:${env.BUILD_NUMBER}", environment: 'production')
notify.slack('#deploys', "Deployed my-app ${env.BUILD_NUMBER}")
}
}
}
}Essential Plugins
Pipeline: Declarative — declarative pipeline syntax (core)
Blue Ocean — modern UI for pipelines (optional, still useful for visualization)
Git / GitHub Branch Source — SCM integration, multibranch pipelines
Credentials Binding — inject credentials as env vars
Docker Pipeline — docker.build(), docker.withRegistry()
Kubernetes — run agents as Kubernetes pods
Slack Notification — slackSend() step
JUnit — publish test results (junit plugin)
HTML Publisher — publish HTML coverage/test reports
Workspace Cleanup — cleanWs() step
Timestamper — add timestamps to console output
AnsiColor — colorize terminal output
Job DSL — generate jobs from code (seed jobs)
Best Practices
Always use Declarative over Scripted — clearer syntax, better error messages, syntax validation
Jenkinsfile in the repo — treat pipeline as code; version it alongside the application
Parallel stages for independent tasks — lint + test + security scan in parallel saves significant time
Fail fast: run cheapest/fastest checks first (lint before integration tests)
Clean workspace: use cleanWs() in post { always } to prevent disk fill-up on agents
Pin plugin versions: use a pinned Plugin Installation Manager file to ensure reproducible builds
Use credentials plugin — never hardcode secrets in Jenkinsfile; they'll be visible in git history
Timeouts everywhere — add timeout() to pipeline and long-running steps to prevent hung builds
Artifacts: archive build artifacts with fingerprinting for traceability across jobs
Multibranch pipeline: automatically creates jobs for every branch/PR in the repo
Multibranch Pipeline
// Automatically scans all branches + PRs in a repo
// Each branch gets its own build history and workspace
pipeline {
agent any
stages {
stage('Test') {
steps { sh 'npm test' }
}
stage('Deploy') {
when {
anyOf {
branch 'main'
branch 'develop'
}
}
steps {
script {
def env = env.BRANCH_NAME == 'main' ? 'production' : 'staging'
sh "./deploy.sh ${env}"
}
}
}
}
post {
// GitHub/GitLab PR status checks
success { githubNotify status: 'SUCCESS', context: 'Jenkins CI' }
failure { githubNotify status: 'FAILURE', context: 'Jenkins CI' }
}
}Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free