Tags: jenkins, ci-cd, devops, automation, pipelines, groovy
Level: intermediate
TL;DR: Jenkins is an open-source automation server for building, testing, and deploying software. It uses a controller-agent model with declarative or scripted Jenkinsfiles for pipeline-as-code automation.
Jenkins uses a controller-agent architecture where the controller manages configurations and agents execute build steps.
Declarative Pipelines use structured pipeline {} blocks offering strict validation, built-in stages, and cleaner syntax.
Scripted Pipelines use procedural Groovy inside node {} blocks for complex and dynamic build logic.
A Jenkinsfile stored in source control ensures pipelines version alongside application code.
Multibranch Pipelines automatically discover branches and Pull Requests containing a Jenkinsfile and create jobs.
Post-actions (always, success, failure, cleanup) handle artifact archival, notifications, and environment teardown.
Shared Libraries provide shared Groovy code repositories across multiple pipelines to eliminate duplication.
Credentials Binding safely injects credentials, tokens, and SSH keys into build environments with automatic log masking.
Ephemeral agent provisioners spin up on-demand containers via Docker or Kubernetes and destroy them after job completion.
Triggers automate build starts via webhooks, polling, scheduled cron syntax, or upstream job completions.
Q: What is the difference between Declarative and Scripted Pipelines in Jenkins?R: I use Declarative Pipelines for standard CI/CD workflows because their strict syntax provides native stage visualization and built-in error checking. I reserve Scripted Pipelines for legacy setups or rare cases requiring arbitrary Groovy logic.
Q: How do you keep credentials safe inside a Jenkinsfile?R: I store credentials inside Jenkins Credentials Manager and inject them at runtime with withCredentials() blocks or environment bindings. Jenkins automatically masks those secrets from raw console output.
Q: How do you avoid performance bottlenecks on the Jenkins controller?R: I set controller executors to zero and dispatch all builds to ephemeral Docker or Kubernetes agents. I also limit build log retention and run regular workspace cleanups.
Running resource-intensive builds directly on the controller node instead of offloading to isolated worker nodes.
Committing plain-text secrets, passwords, or cloud provider tokens into repository Jenkinsfiles.
Installing obsolete or unmaintained third-party plugins that create dependency conflicts and security risks.
Omitting cleanWs() in post-action blocks, leading to agent disk exhaustion over repeated runs.
Running unoptimized loops or heavy non-CPS Groovy scripts inside pipelines, which saturates controller memory.
// Declarative Jenkinsfile with Docker agent and stages
pipeline {
agent {
docker {
image 'node:20-alpine'
args '-u root'
}
}
environment {
CI = 'true'
}
stages {
stage('Install & Test') {
steps {
sh 'npm ci'
sh 'npm test'
}
}
stage('Deploy') {
when {
branch 'main'
}
steps {
withCredentials([string(credentialsId: 'PROD_TOKEN', variable: 'AUTH_TOKEN')]) {
sh 'sh ./scripts/deploy.sh'
}
}
}
}
post {
always {
cleanWs()
}
failure {
echo 'Build failed. Check test artifacts.'
}
}
}
Bash
# Restart Jenkins safely from CLI or URL
curl -X POST http://jenkins.internal:8080/safeRestart -u admin:token
[ ] Configuring Kubernetes dynamic pod agents via Jenkins Kubernetes Plugin
[ ] Setting up Jenkins Shared Libraries (vars/ global variables structure)
[x] Writing Declarative Pipeline stages with when directives and post handlers
[x] Injecting credentials securely using Jenkins Credentials Binding
Jenkins User Documentation: Pipeline Syntax and Getting Started
Jenkins Plugins Index: Credentials Plugin and Kubernetes Plugin