Jenkins Cheatsheet
Installation and Setup
Download Jenkins
- Download Jenkins from jenkins.io.
Install Jenkins on Ubuntu
wget -q -O - https://pkg.jenkins.io/debian/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
Start Jenkins Service
sudo systemctl start jenkins
Access Jenkins
- Open a web browser and go to
http://localhost:8080
.
- Open a web browser and go to
Unlock Jenkins
- Follow on-screen instructions to unlock using the initial admin password located at
/var/lib/jenkins/secrets/initialAdminPassword
.
- Follow on-screen instructions to unlock using the initial admin password located at
Basic Commands
Install Jenkins Plugin
- Navigate to Manage Jenkins -> Manage Plugins -> Available, search for plugins and install.
Update Jenkins Plugins
- Navigate to Manage Jenkins -> Manage Plugins -> Installed, select plugins to update.
Restart Jenkins
- Navigate to Manage Jenkins -> Reload Configuration from Disk.
Managing Jobs
Create a New Job
Freestyle Project:
- Navigate to New Item -> Enter name -> Choose Freestyle project -> Configure.
Pipeline Project:
- Use Jenkinsfile with declarative or scripted pipeline.
Build Job
- Click Build Now in job's dashboard.
Configure Job
- Navigate to job -> Configure -> Update settings -> Save.
Delete Job
- Navigate to job -> Delete -> Confirm.
Copy Job
- Navigate to job -> Copy Job -> Provide new name -> OK.
Jenkins Pipeline
Pipeline Syntax
Declarative Pipeline:
pipeline { agent any stages { stage('Build') { steps { // Build steps } } stage('Test') { steps { // Test steps } } stage('Deploy') { steps { // Deployment steps } } } }
Scripted Pipeline:
node { stage('Build') { // Build steps } stage('Test') { // Test steps } stage('Deploy') { // Deployment steps } }
Pipeline Libraries
- Configure global libraries under Manage Jenkins -> Configure System -> Global Pipeline Libraries.
Parallel Execution
pipeline { agent any stages { stage('Parallel') { parallel { stage('Branch A') { steps { // Steps for branch A } } stage('Branch B') { steps { // Steps for branch B } } } } } }
Authentication and Authorization
Configure Security
- Navigate to Manage Jenkins -> Configure Global Security -> Configure security settings.
Create Users
- Navigate to Manage Jenkins -> Manage Users -> Create User.
Manage Roles
- Install and configure plugins like Role-based Authorization Strategy to manage roles and permissions.
Plugins
Install Plugins
- Navigate to Manage Jenkins -> Manage Plugins -> Available -> Search and install plugins.
Useful Plugins
Git Plugin: Integrate Git with Jenkins jobs.
Pipeline Plugin: Enable Jenkinsfile-based pipelines.
Credentials Plugin: Manage credentials securely.
Blue Ocean Plugin: Provides a modern UI for pipelines.
Jenkins CLI
Install Jenkins CLI
- Download CLI jar from Jenkins web interface or use package manager.
Basic CLI Commands
java -jar jenkins-cli.jar -s http://[jenkins-url] [command]
Example:
java -jar jenkins-cli.jar -s
http://localhost:8080
help
java -jar jenkins-cli.jar -s
http://localhost:8080
build [job-name]
Script Console
Access Script Console
- Navigate to Manage Jenkins -> Script Console.
Run Groovy Scripts
- Execute Groovy scripts for Jenkins administration and automation tasks.
Backup and Restore
Backup Jenkins
- Archive Jenkins home directory (
/var/lib/jenkins
) regularly.
- Archive Jenkins home directory (
Restore Jenkins
- Replace Jenkins home directory with backup or use Jenkins CLI to restore jobs and configurations.
Monitoring and Troubleshooting
Monitoring
- Use built-in monitoring tools or plugins like Jenkins Monitoring Plugin for performance insights.
Logs
- Jenkins logs are typically found in
/var/log/jenkins/
.
- Jenkins logs are typically found in
Troubleshooting
- Check Jenkins UI for notifications and logs for errors.
This cheatsheet covers essential commands and configurations for Jenkins, helping with job management, pipelines, security settings, plugins, and more. Adjust commands and paths based on your Jenkins installation and configuration.