Jenkins Installation and Pipeline Configuration
Complete guide to installing Jenkins, configuring Jenkins pipelines, managing credentials, agents, and plugins for CI/CD automation.
# Jenkins Installation and Pipeline Configuration
Introduction
Jenkins is an open-source automation server that enables continuous integration and continuous delivery. With over 1,800 plugins, Jenkins can automate virtually any aspect of software development, from building and testing to deploying and monitoring. This tutorial covers Jenkins installation, pipeline configuration, agent management, credential handling, and production best practices.
Learning Objectives
By the end of this tutorial, you will be able to:
- Install Jenkins on Linux, Docker, and Kubernetes
- Configure Jenkins pipelines (declarative and scripted)
- Manage Jenkins agents for distributed builds
- Handle credentials and secrets securely
- Install and configure essential Jenkins plugins
- Implement pipeline as code with Jenkinsfile
- Secure Jenkins with authentication and authorization
Technical Background
Jenkins Architecture
flowchart TB
User[User/Developer] -->|HTTP| Controller[Jenkins Controller]
Controller -->|JNLP/SSH| Agent1[Build Agent 1]
Controller -->|JNLP/SSH| Agent2[Build Agent 2]
Controller -->|JNLP/SSH| AgentN[Build Agent N]
Agent1 --> Docker[Docker/Container]
Agent2 --> K8s[Kubernetes Pod]
Controller --> SCM[(SCM Repository)]
Controller --> Registry[(Container Registry)]
Controller --> Cloud[Cloud Platform]
subgraph Controller Node
Controller
end
subgraph Agent Nodes
Agent1
Agent2
AgentN
endJenkins Pipeline Types
| Type | Description | Syntax | |------|-------------|--------| | Declarative | Structured, opinionated syntax | `pipeline { ... }` | | Scripted | Imperative, Groovy-based | `node { ... }` | | Blue Ocean | Visual pipeline editor | GUI-based |
Jenkins Components
| Component | Description | |-----------|-------------| | Controller | Central Jenkins instance that manages jobs and agents | | Agent | Worker node that executes build steps | | Executor | Single concurrent build slot on an agent | | Plugin | Extension that adds functionality to Jenkins | | Credential | Securely stored username/password, SSH key, or secret | | Job | Configured build or pipeline | | Build | Single execution of a job |
Prerequisites
- Linux server (Ubuntu 22.04 LTS recommended)
- Java 11 or 17 (Jenkins requires Java)
- Root or sudo access
- 4 GB RAM minimum (8 GB recommended)
- 50 GB disk space
- Network access to plugin repository
Hardware and Software Requirements
| Component | Requirement | |-----------|-------------| | Operating System | Ubuntu 22.04 LTS, RHEL 9, or Docker | | Java | OpenJDK 11 or 17 | | Jenkins | LTS version (latest stable) | | Browser | Modern web browser | | Git | For SCM integration | | Docker | For container-based builds |
Step-by-Step Implementation
Phase 1: Install Jenkins
#### Install on Ubuntu
# Update system
sudo apt update && sudo apt upgrade -y
# Install Java
sudo apt install openjdk-17-jdk -y
# Add Jenkins repository
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
# Install Jenkins
sudo apt update
sudo apt install jenkins -y
# Start and enable Jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
# Check status
sudo systemctl status jenkins#### Install Jenkins in Docker
# Create Jenkins Docker network
docker network create jenkins
# Run Jenkins container
docker run \
--name jenkins \
--network jenkins \
--restart unless-stopped \
--detach \
--privileged \
--volume jenkins-data:/var/jenkins_home \
--volume /var/run/docker.sock:/var/run/docker.sock \
--publish 8080:8080 \
--publish 50000:50000 \
jenkins/jenkins:lts
# Get initial admin password
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPasswordPhase 2: Initial Jenkins Configuration
#### Unlock Jenkins
1. Open browser: `http://<server-ip>:8080` 2. Retrieve initial admin password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword3. Enter password in unlock screen 4. Install suggested plugins 5. Create admin user 6. Configure Jenkins URL (e.g., `http://jenkins.example.com:8080`)
#### Configure Java and Git
1. Go to **Manage Jenkins** > **Global Tool Configuration** 2. Add JDK installation (JDK 17) 3. Add Git installation (path to git binary) 4. Save configuration
Phase 3: Create Jenkins Pipeline
#### Declarative Pipeline Example
// Jenkinsfile
pipeline {
agent any
environment {
REGISTRY = "docker.io/myorg"
IMAGE_NAME = "my-app"
DOCKER_CREDENTIALS = 'docker-hub-credentials'
}
agent {
label 'docker-agent'
}
options {
timestamps()
timeout(time: 1, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '10'))
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build') {
steps {
sh 'docker build -t ${REGISTRY}/${IMAGE_NAME}:${BUILD_NUMBER} .'
}
}
stage('Test') {
steps {
sh 'docker run --rm ${REGISTRY}/${IMAGE_NAME}:${BUILD_NUMBER} npm test'
}
post {
always {
junit '**/test-results/*.xml'
}
}
}
stage('Security Scan') {
steps {
sh '''
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
aquasec/trivy:latest \
image --exit-code 1 --severity HIGH,CRITICAL \
${REGISTRY}/${IMAGE_NAME}:${BUILD_NUMBER}
'''
}
}
stage('Push Image') {
steps {
script {
docker.withRegistry('https://index.docker.io/v1/', DOCKER_CREDENTIALS) {
def image = docker.image("${REGISTRY}/${IMAGE_NAME}:${BUILD_NUMBER}")
image.push("${BUILD_NUMBER}")
image.push('latest')
}
}
}
}
stage('Deploy Staging') {
when {
branch 'develop'
}
steps {
sh '''
kubectl set image deployment/my-app \
my-app=${REGISTRY}/${IMAGE_NAME}:${BUILD_NUMBER} \
-n staging
kubectl rollout status deployment/my-app -n staging --timeout=300s
'''
}
}
stage('Deploy Production') {
when {
branch 'main'
}
steps {
input message: 'Deploy to production?', ok: 'Deploy'
sh '''
kubectl set image deployment/my-app \
my-app=${REGISTRY}/${IMAGE_NAME}:${BUILD_NUMBER} \
-n production
kubectl rollout status deployment/my-app -n production --timeout=300s
'''
}
}
}
post {
always {
cleanWs()
}
failure {
mailtop(
subject: "Build Failed: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
to: "${env.CHANGE_AUTHOR_EMAIL}"
)
}
success {
echo 'Pipeline completed successfully!'
}
}
}#### Scripted Pipeline Example
// Scripted Jenkinsfile
node('docker-agent') {
stage('Checkout') {
checkout scm
}
stage('Build') {
sh 'docker build -t myorg/my-app:${BUILD_NUMBER} .'
}
stage('Test') {
try {
sh 'docker run --rm myorg/my-app:${BUILD_NUMBER} npm test'
} catch (err) {
currentBuild.result = 'FAILED'
throw err
}
}
stage('Deploy') {
if (env.BRANCH_NAME == 'main') {
input message: 'Deploy to production?'
sh 'kubectl set image deployment/my-app my-app=myorg/my-app:${BUILD_NUMBER} -n production'
}
}
}Phase 4: Configure Jenkins Agents
#### Install Agent on Linux
# On agent machine
sudo apt update
sudo apt install openjdk-17-jdk -y
sudo apt install docker.io -y
sudo usermod -aG docker jenkins
# Download agent JAR from Jenkins controller
curl -O http://jenkins-server:8080/jnlpJars/agent.jar
# Create agent user
sudo useradd -m -s /bin/bash jenkins-agent
sudo passwd jenkins-agent
# Start agent
java -jar agent.jar -jnlpUrl http://jenkins-server:8080/computer/agent1/slave-agent.jnlp \
-secret <secret-key> -workDir /home/jenkins-agent#### Configure Kubernetes Agent
# k8s/jenkins-agent.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
jenkins: agent
spec:
containers:
- name: jnlp
image: jenkins/inbound-agent:latest
args: ["${computer.jnlpmac}", "${computer.name}"]
resources:
limits:
cpu: "2"
memory: "4Gi"
requests:
cpu: "500m"
memory: "1Gi"// Jenkinsfile with Kubernetes agent
pipeline {
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: jnlp
image: jenkins/inbound-agent:latest
- name: docker
image: docker:24-dind
privileged: true
volumeMounts:
- name: docker-sock
mountPath: /var/run/docker.sock
volumes:
- name: docker-sock
hostPath:
path: /var/run/docker.sock
'''
}
}
stages {
stage('Build') {
steps {
container('docker') {
sh 'docker build -t myapp:latest .'
}
}
}
}
}Phase 5: Manage Credentials
#### Add Credentials in Jenkins
1. Go to **Manage Jenkins** > **Credentials** 2. Click **System** > **Global credentials** 3. Click **Add Credentials** 4. Select credential type:
5. Enter credential ID and value 6. Save
- Username with password
- SSH Username with private key
- Secret text
- Certificate
#### Use Credentials in Pipeline
// Username/password credential
withCredentials([usernamePassword(credentialsId: 'docker-hub',
usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh 'docker login -u $DOCKER_USER -p $DOCKER_PASS'
}
// SSH key credential
withCredentials([sshUserPrivateKey(credentialsId: 'ssh-key',
keyFileVariable: 'SSH_KEY'))] {
sh 'ssh -i $SSH_KEY user@server'
}
// Secret text credential
withCredentials([string(credentialsId: 'api-token', variable: 'API_TOKEN')]) {
sh 'curl -H "Authorization: Bearer $API_TOKEN" https://api.example.com'
}#### Bind Docker Credentials
docker.withRegistry('https://index.docker.io/v1/', 'docker-hub-credentials') {
def image = docker.image("myorg/myapp:${BUILD_NUMBER}")
image.push()
}Phase 6: Install Essential Plugins
#### Recommended Plugins
| Plugin | Purpose | |--------|---------| | Git | SCM integration | | Pipeline | Declarative/scripted pipeline support | | Docker | Docker build and push | | Kubernetes | Kubernetes deployment | | Kubernetes CLI | kubectl integration | | Credentials Binding | Secure credential injection | | Timestamper | Add timestamps to build output | | Build Discarder | Clean old builds | | Email Extension | Notification emails | | Slack Notifier | Slack notifications | | Blue Ocean | Modern pipeline UI |
#### Install Plugins via CLI
# Download Jenkins CLI
curl -O http://jenkins-server:8080/jnlpJars/jenkins-cli.jar
# Install plugins
java -jar jenkins-cli.jar -s http://jenkins-server:8080 \
install-plugin git pipeline docker kubernetes credentials-binding \
timestamper slack email-ext -auth admin:API_TOKEN
# Restart Jenkins
java -jar jenkins-cli.jar -s http://jenkins-server:8080 \
safe-restart -auth admin:API_TOKENPhase 7: Secure Jenkins
#### Enable Security
1. Go to **Manage Jenkins** > **Configure Global Security** 2. Enable security 3. Select security realm:
4. Select authorization:
- Jenkins own user database
- LDAP
- Active Directory
- GitHub authentication
- Logged-in users can do anything
- Matrix-based security
- Project-based Matrix Authorization
#### Configure CSRF Protection
CSRF protection is enabled by default. For API access, use crumb:
# Get crumb
CRUMB=$(curl -s 'http://admin:token@jenkins:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
# Use crumb in API call
curl -X POST -H "$CRUMB" \
'http://admin:token@jenkins:8080/job/my-job/build'#### Configure HTTPS
# Nginx reverse proxy for Jenkins
server {
listen 443 ssl;
server_name jenkins.example.com;
ssl_certificate /etc/letsencrypt/live/jenkins.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/jenkins.example.com/privkey.pem;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Expected Output
- Jenkins server running and accessible
- Jenkinsfile configured for CI/CD pipeline
- Build agents configured for distributed execution
- Credentials securely stored and used
- Essential plugins installed
- Jenkins secured with authentication and HTTPS
Verification Procedures
| Check | Method | Expected Result | |-------|--------|-----------------| | Jenkins accessible | Open http://server:8080 | Login page displayed | | Pipeline execution | Create job with Jenkinsfile | Pipeline runs successfully | | Agent connection | Check agent status | Agent online and ready | | Credential injection | Run pipeline with credentials | Credentials available in build | | Plugin installation | Check installed plugins | Required plugins active | | Security | Access without login | Redirect to login page |
Troubleshooting Scenarios
| Issue | Diagnosis | Resolution | |-------|-----------|------------| | Jenkins won't start | Java not installed or wrong version | Install correct Java version | | Agent not connected | Network or secret key issue | Verify network; regenerate secret | | Pipeline fails | Permission or plugin missing | Check permissions; install plugins | | Build timeout | Long-running step | Increase timeout or optimize step | | Credential not found | Wrong credential ID | Verify credential ID in Jenkins | | Plugin installation fails | Network or compatibility issue | Check network; verify plugin version |
Best Practices
- **Use Jenkins LTS:** Stable version with longer support
- **Backup Jenkins home:** Regularly back up `/var/jenkins_home`
- **Use pipeline as code:** Store Jenkinsfile in version control
- **Limit controller builds:** Run builds on agents, not controller
- **Clean workspaces:** Use `cleanWs()` to free disk space
- **Rotate build history:** Use build discarder to keep recent builds
- **Secure credentials:** Never hardcode secrets in Jenkinsfile
- **Monitor Jenkins:** Set up monitoring for Jenkins itself
Security Considerations
- **Enable CSRF protection:** Prevent cross-site request forgery
- **Use HTTPS:** Encrypt Jenkins web traffic
- **Restrict script approval:** Carefully review Groovy script approvals
- **Limit admin access:** Few users should have Jenkins admin
- **Audit plugin sources:** Install plugins from official update center
- **Rotate API tokens:** Periodically regenerate API tokens
- **Backup secrets:** Securely back up credential store
Conclusion
Jenkins remains one of the most popular CI/CD platforms due to its flexibility, extensive plugin ecosystem, and pipeline-as-code capabilities. By following this tutorial, you can set up a production-ready Jenkins environment with secure pipelines, distributed agents, and proper credential management. Regular maintenance and security reviews ensure your Jenkins instance remains reliable and secure.
References
- Jenkins Documentation: https://www.jenkins.io/doc/
- Jenkins Pipeline: https://www.jenkins.io/doc/book/pipeline/
- Blue Ocean: https://www.jenkins.io/doc/book/blue-ocean/
- Jenkins Kubernetes Plugin: https://plugins.jenkins.io/kubernetes/
- Jenkins Security: https://www.jenkins.io/doc/book/system-administration/security/