Jenkins Cheat Sheet

Here’s a cheat sheet for working with Jenkins, a popular open-source automation server:

Jenkins Basics

Access Jenkins Web Interface:

http://localhost:8080

Install Jenkins Plugins:

  • Navigate to “Manage Jenkins” > “Manage Plugins” > “Available” tab.

Create a New Job (Freestyle Project):

  • Click on “New Item” > Enter a name > Choose “Freestyle project.”

Freestyle Project Configuration

Source Code Management:

  • Configure your version control system (Git, SVN, etc.).

Build:

  • Define build steps (execute shell commands, invoke Ant/Maven, etc.).

Post-Build Actions:

  • Archive artifacts, trigger other projects, etc.

Pipeline Job

Create a Pipeline Job:

  • Click on “New Item” > Enter a name > Choose “Pipeline.”

Write a Simple Pipeline Script:

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                // Build steps
            }
        }

        stage('Test') {
            steps {
                // Test steps
            }
        }

        stage('Deploy') {
            steps {
                // Deployment steps
            }
        }
    }
}

Jenkinsfile

Create a Jenkinsfile:

  • Store pipeline script in a file named Jenkinsfile in your project.

Multibranch Pipeline:

  • Create a Multibranch Pipeline project for automatic branch detection.

Jenkins CLI

Install Jenkins CLI:

  • Download the CLI JAR file from Jenkins and run commands.

Example CLI Command:

java -jar jenkins-cli.jar -s http://localhost:8080/ -auth username:password command

Job DSL (Job Definitions as Code)

Install Job DSL Plugin:

  • Manage Jenkins > Manage Plugins > Available > Job DSL

Create a DSL Script:

  • Create a new job with a DSL script defining job configuration.

Jenkins Security

Manage Users:

  • Manage Jenkins > Manage Users > Create/Delete Users

Global Security Configuration:

  • Manage Jenkins > Configure Global Security

Jenkins Environment Variables

Build Environment Variables:

  • Access build-related information using environment variables (e.g., $BUILD_NUMBER, $WORKSPACE).

Jenkins Notifications

Configure Email Notifications:

  • Manage Jenkins > Configure System > E-mail Notification

Slack Integration:

  • Use the Slack plugin for notifications.

Jenkins Tips and Tricks

Console Output Filtering:

  • Use Log Parser Plugin for filtering console output.

Jenkins Job DSL Scripts:

  • Share and version control job configurations using Job DSL scripts.

Jenkins Groovy Scripts

Script Console:

  • Manage Jenkins > Script Console for running Groovy scripts.

Jenkins Script Approval:

  • Approve script signatures in Manage Jenkins > In-process Script Approval.

This cheat sheet covers essential Jenkins operations and features. For detailed information and advanced use cases, refer to the Jenkins documentation.