Skip to main content

Jenkins

Jenkins is an open-source, self-hosted automation server that runs CI/CD pipelines defined in a Jenkinsfile, extended through a large plugin ecosystem.

Understanding Jenkins

What Is Jenkins and Why Is It Still Used

Jenkins is one of the oldest and most widely deployed CI/CD servers. Teams choose it for full control over infrastructure, a plugin for almost any tool, and the ability to run entirely on-premises, which matters in regulated environments that cannot send builds to a hosted service. The trade-off is operational: you patch, scale, and secure it yourself.

Architecture

A controller schedules jobs and serves the UI, while agents execute the builds. Agents can be static machines or dynamic Kubernetes pods created per build.

flowchart TD A[Controller: UI and scheduling] --> B[Agent: Java builds] A --> C[Agent: Node builds] A --> D[Agent: Docker builds] classDef ctl fill:#326CE5,color:#FFFFFF classDef agent fill:#475569,color:#FFFFFF class A ctl class B,C,D agent

Declarative Jenkinsfile

GROOVY
pipeline {
agent none
environment {
ECR_REGISTRY = '123456789.dkr.ecr.ap-south-1.amazonaws.com'
IMAGE_NAME = 'payment-api'
}
stages {
stage('Test') {
parallel {
stage('Unit tests') {
agent { docker { image 'node:20-alpine' } }
steps {
sh 'npm ci'
sh 'npm test'
junit 'test-results/*.xml'
}
}
stage('Lint') {
agent { docker { image 'node:20-alpine' } }
steps {
sh 'npm ci'
sh 'npm run lint'
}
}
}
}
stage('Build image') {
agent { label 'docker' }
steps {
sh "docker build -t ${ECR_REGISTRY}/${IMAGE_NAME}:${GIT_COMMIT} ."
}
}
stage('Deploy production') {
agent { label 'k8s-deployer' }
when { branch 'main' }
input {
message 'Deploy to production?'
ok 'Deploy'
submitter 'tech-leads'
}
steps {
sh "./deploy.sh production ${GIT_COMMIT}"
}
}
}
post {
failure {
slackSend channel: '#deployments', color: 'danger',
message: "FAILED: ${env.JOB_NAME} #${env.BUILD_NUMBER}"
}
always {
cleanWs()
}
}
}

Using agent none at the top and declaring an agent per stage lets stages run in parallel on different machines or containers.

Practical Commands

Bash
# Trigger a parameterised build with the CLI
java -jar jenkins-cli.jar -s http://jenkins.internal -auth rahul:API_TOKEN \
build payment-api-pipeline -p ENVIRONMENT=staging
# Read a build log
java -jar jenkins-cli.jar -s http://jenkins.internal console payment-api-pipeline 42
# Restart safely after running builds finish
java -jar jenkins-cli.jar -s http://jenkins.internal safe-restart

Troubleshooting

Symptom Check What to Look For
Build stuck in queue Agent status and labels Agents offline or label mismatch
Credentials not found Credentials store ID ID differs from the Jenkinsfile
Groovy syntax error Replay and edit Typo in the pipeline script
Workspace conflicts Concurrent builds Shared workspace between builds
Tip

Use the Pipeline Syntax snippet generator at /pipeline-syntax/ on your Jenkins URL to produce correct step code for any plugin.

Remember

Keep the Jenkinsfile in the repository with the code, and keep builds off the controller so the UI stays responsive.

Security

Store secrets in the Jenkins Credentials Store and bind them with credentials() or withCredentials. Never hardcode them in a Jenkinsfile, which anyone with repo access can read. Keep Jenkins and plugins patched.

Common Mistake

Writing scripted pipelines by default. Declarative syntax is validated before running and is easier to read. Use scripted only when you truly need arbitrary Groovy.

Frequently Asked Questions

Why do teams still choose self-hosted Jenkins over hosted CI?

Mainly its plugin ecosystem and full control of infrastructure, which suits legacy systems and regulated environments. The cost is that your team owns patching, scaling agents, and securing the controller.

Should builds run on the Jenkins controller?

No. Run builds on agents, either static or dynamic Kubernetes pods. A runaway build on the controller can slow or crash Jenkins for every team.

What is the difference between declarative and scripted pipelines?

Declarative has a structured, validated syntax with stages, agents, and post blocks. Scripted is raw Groovy, flexible but harder to read and validate, so declarative is the recommended default.

Found a mistake, an outdated step or a broken link?