logo
eng-flag

Jenkins Cheatsheet

Table of Contents

  1. Jenkins Installation
  2. Basic Jenkins Configuration
  3. Jenkins Jobs
  4. Jenkins Pipelines
  5. Jenkins Plugins
  6. Jenkins Security
  7. Jenkins Distributed Builds
  8. Jenkins API
  9. Jenkins with Docker
  10. Jenkins with Kubernetes
  11. Jenkins Backup and Restore
  12. Jenkins Monitoring
  13. Jenkins Best Practices
  14. 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

Configure Jenkins through web interface

  1. Install suggested plugins
  2. Create first admin user
  3. Configure instance URL

Jenkins Jobs

Create a new job

  1. Click "New Item" on Jenkins dashboard
  2. Enter a name for the job
  3. Select job type (e.g., Freestyle project, Pipeline)
  4. Configure job settings
  5. Click "Save"

Configure source code management

  1. In job configuration, go to "Source Code Management" section
  2. Select your SCM (e.g., Git, Subversion)
  3. Enter repository URL and credentials

Configure build triggers

  1. In job configuration, go to "Build Triggers" section
  2. Select trigger type (e.g., SCM polling, webhook)

Configure build steps

  1. In job configuration, go to "Build" section
  2. Add build steps (e.g., Execute shell, Invoke Gradle script)

Configure post-build actions

  1. In job configuration, go to "Post-build Actions" section
  2. 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

  1. Click "New Item" on Jenkins dashboard
  2. Enter a name for the job
  3. Select "Pipeline" as job type
  4. In "Pipeline" section, select "Pipeline script from SCM"
  5. Configure SCM settings and Jenkinsfile path

Run a Pipeline job

  1. Go to the Pipeline job page
  2. Click "Build Now"

Jenkins Plugins

Install a plugin

  1. Go to "Manage Jenkins" > "Manage Plugins"
  2. Go to "Available" tab
  3. Search for the plugin
  4. Select the plugin and click "Install without restart"

Update plugins

  1. Go to "Manage Jenkins" > "Manage Plugins"
  2. Go to "Updates" tab
  3. Select plugins to update
  4. Click "Download now and install after restart"

Uninstall a plugin

  1. Go to "Manage Jenkins" > "Manage Plugins"
  2. Go to "Installed" tab
  3. Select the plugin to uninstall
  4. Click "Uninstall"

Jenkins Security

Configure global security

  1. Go to "Manage Jenkins" > "Configure Global Security"
  2. Enable security
  3. Choose security realm (e.g., Jenkins' own user database)
  4. Choose authorization strategy (e.g., Matrix-based security)

Create a new user

  1. Go to "Manage Jenkins" > "Manage Users"
  2. Click "Create User"
  3. Fill in user details and click "Create User"

Configure project-based security

  1. In job configuration, go to "General" section
  2. Enable "Enable project-based security"
  3. Configure permissions for users/groups

Jenkins Distributed Builds

Add a new node

  1. Go to "Manage Jenkins" > "Manage Nodes and Clouds"
  2. Click "New Node"
  3. Enter node name and select type (e.g., Permanent Agent)
  4. Configure node settings (e.g., # of executors, remote root directory)

Configure node launch method

  1. In node configuration, go to "Launch method" section
  2. Select launch method (e.g., Launch agent via SSH)
  3. Configure launch method settings

Connect a node

  1. Go to node page
  2. 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

Get build information

curl -s http://localhost:8080/job/JOB_NAME/BUILDNUMBER/api/json

Jenkins with Docker

Install Docker plugin

  1. Go to "Manage Jenkins" > "Manage Plugins"
  2. Install "Docker" and "Docker Pipeline" plugins

Configure Docker agent

  1. 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

  1. Go to "Manage Jenkins" > "Manage Plugins"
  2. Install "Kubernetes" plugin

Configure Kubernetes cloud

  1. Go to "Manage Jenkins" > "Manage Nodes and Clouds"
  2. Click "Configure Clouds"
  3. Add a new Kubernetes cloud
  4. 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

  1. Install "Prometheus" plugin
  2. Configure Prometheus in Jenkins
  3. Set up Prometheus server to scrape Jenkins metrics
  4. Create Grafana dashboard for Jenkins metrics

Use Jenkins Metrics plugin

  1. Install "Metrics" plugin
  2. Access metrics at http://localhost:8080/metrics/

Jenkins Best Practices

  1. Use Jenkinsfile for pipeline definition
  2. Store Jenkinsfile in source control
  3. Use multibranch pipelines for branch-based workflows
  4. Implement proper error handling and notifications
  5. Use shared libraries for common functionality
  6. Implement proper security measures
  7. Regularly update Jenkins and plugins
  8. Use agents for distributed builds
  9. Implement proper backup and disaster recovery plans
  10. 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

  1. Stop Jenkins
  2. Delete $JENKINS_HOME/.cache directory
  3. Start Jenkins

Diagnose plugin issues

  1. Go to "Manage Jenkins" > "Manage Plugins"
  2. Click on "Advanced" tab
  3. Click "Check Now" to update plugin information

Increase heap size

Edit /etc/default/jenkins and modify JAVA_ARGS:

JAVA_ARGS="-Xmx4g"

2024 © All rights reserved - buraxta.com