Jenkins Cheatsheet
Table of Contents
Jenkins Installation
Basic Jenkins Configuration
Jenkins Jobs
Jenkins Pipelines
Jenkins Plugins
Jenkins Security
Jenkins Distributed Builds
Jenkins API
Jenkins with Docker
Jenkins with Kubernetes
Jenkins Backup and Restore
Jenkins Monitoring
Jenkins Best Practices
Troubleshooting Jenkins
Jenkins Installation
Install Jenkins on Ubuntu
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins
Install Jenkins on CentOS
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
sudo yum install jenkins
Start Jenkins service
sudo systemctl start jenkins
Basic Jenkins Configuration
Access Jenkins web interface
Open a web browser and navigate to http://localhost:8080
Get initial admin password
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Install suggested plugins
Create first admin user
Configure instance URL
Jenkins Jobs
Create a new job
Click "New Item" on Jenkins dashboard
Enter a name for the job
Select job type (e.g., Freestyle project, Pipeline)
Configure job settings
Click "Save"
In job configuration, go to "Source Code Management" section
Select your SCM (e.g., Git, Subversion)
Enter repository URL and credentials
In job configuration, go to "Build Triggers" section
Select trigger type (e.g., SCM polling, webhook)
In job configuration, go to "Build" section
Add build steps (e.g., Execute shell, Invoke Gradle script)
Configure post-build actions
In job configuration, go to "Post-build Actions" section
Add actions (e.g., Publish JUnit test results, Archive artifacts)
Jenkins Pipelines
Create a Jenkinsfile
pipeline {
agent any
stages {
stage ( 'Build' ) {
steps {
sh 'echo "Building the application"'
}
}
stage ( 'Test' ) {
steps {
sh 'echo "Running tests"'
}
}
stage ( 'Deploy' ) {
steps {
sh 'echo "Deploying the application"'
}
}
}
}
Create a Pipeline job
Click "New Item" on Jenkins dashboard
Enter a name for the job
Select "Pipeline" as job type
In "Pipeline" section, select "Pipeline script from SCM"
Configure SCM settings and Jenkinsfile path
Run a Pipeline job
Go to the Pipeline job page
Click "Build Now"
Jenkins Plugins
Install a plugin
Go to "Manage Jenkins" > "Manage Plugins"
Go to "Available" tab
Search for the plugin
Select the plugin and click "Install without restart"
Update plugins
Go to "Manage Jenkins" > "Manage Plugins"
Go to "Updates" tab
Select plugins to update
Click "Download now and install after restart"
Uninstall a plugin
Go to "Manage Jenkins" > "Manage Plugins"
Go to "Installed" tab
Select the plugin to uninstall
Click "Uninstall"
Jenkins Security
Go to "Manage Jenkins" > "Configure Global Security"
Enable security
Choose security realm (e.g., Jenkins' own user database)
Choose authorization strategy (e.g., Matrix-based security)
Create a new user
Go to "Manage Jenkins" > "Manage Users"
Click "Create User"
Fill in user details and click "Create User"
In job configuration, go to "General" section
Enable "Enable project-based security"
Configure permissions for users/groups
Jenkins Distributed Builds
Add a new node
Go to "Manage Jenkins" > "Manage Nodes and Clouds"
Click "New Node"
Enter node name and select type (e.g., Permanent Agent)
Configure node settings (e.g., # of executors, remote root directory)
In node configuration, go to "Launch method" section
Select launch method (e.g., Launch agent via SSH)
Configure launch method settings
Connect a node
Go to node page
Follow instructions to connect the node
Jenkins API
Get Jenkins version
curl -s http://localhost:8080/api/json?tree= version
List all jobs
curl -s http://localhost:8080/api/json?tree= jobs[ name,url]
Trigger a build
curl -X POST http://localhost:8080/job/JOB_NAME/build --user USER :API_TOKEN
curl -s http://localhost:8080/job/JOB_NAME/BUILDNUMBER/api/json
Jenkins with Docker
Install Docker plugin
Go to "Manage Jenkins" > "Manage Plugins"
Install "Docker" and "Docker Pipeline" plugins
In job configuration, use Docker agent in Jenkinsfile:
pipeline {
agent {
docker {
image 'maven:3.8.1-jdk-8'
}
}
stages {
stage ( 'Build' ) {
steps {
sh 'mvn clean package'
}
}
}
}
Build Docker image in Jenkins
pipeline {
agent any
stages {
stage ( 'Build Docker Image' ) {
steps {
script {
docker. build ( "my-image: ${ env. BUILD_ID } " )
}
}
}
}
}
Jenkins with Kubernetes
Install Kubernetes plugin
Go to "Manage Jenkins" > "Manage Plugins"
Install "Kubernetes" plugin
Go to "Manage Jenkins" > "Manage Nodes and Clouds"
Click "Configure Clouds"
Add a new Kubernetes cloud
Configure Kubernetes settings (e.g., Kubernetes URL, credentials)
Use Kubernetes agent in Pipeline
pipeline {
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: maven
image: maven:3.8.1-jdk-8
command:
- cat
tty: true
'''
}
}
stages {
stage ( 'Build' ) {
steps {
container ( 'maven' ) {
sh 'mvn clean package'
}
}
}
}
}
Jenkins Backup and Restore
Backup Jenkins configuration
JENKINS_HOME = /var/lib/jenkins
tar czf jenkins_backup.tar.gz $JENKINS_HOME
Restore Jenkins configuration
JENKINS_HOME = /var/lib/jenkins
sudo service jenkins stop
sudo rm -rf $JENKINS_HOME /*
sudo tar xzf jenkins_backup.tar.gz -C /
sudo chown -R jenkins:jenkins $JENKINS_HOME
sudo service jenkins start
Jenkins Monitoring
Monitor Jenkins with Prometheus and Grafana
Install "Prometheus" plugin
Configure Prometheus in Jenkins
Set up Prometheus server to scrape Jenkins metrics
Create Grafana dashboard for Jenkins metrics
Use Jenkins Metrics plugin
Install "Metrics" plugin
Access metrics at http://localhost:8080/metrics/
Jenkins Best Practices
Use Jenkinsfile for pipeline definition
Store Jenkinsfile in source control
Use multibranch pipelines for branch-based workflows
Implement proper error handling and notifications
Use shared libraries for common functionality
Implement proper security measures
Regularly update Jenkins and plugins
Use agents for distributed builds
Implement proper backup and disaster recovery plans
Use parameterized builds for flexibility
Troubleshooting Jenkins
Check Jenkins logs
tail -f /var/log/jenkins/jenkins.log
Restart Jenkins
sudo systemctl restart jenkins
Clear Jenkins cache
Stop Jenkins
Delete $JENKINS_HOME/.cache
directory
Start Jenkins
Diagnose plugin issues
Go to "Manage Jenkins" > "Manage Plugins"
Click on "Advanced" tab
Click "Check Now" to update plugin information
Increase heap size
Edit /etc/default/jenkins
and modify JAVA_ARGS:
JAVA_ARGS="-Xmx4g"