Cheat Sheet #day35 - Jenkins

Cheat Sheet #day35 - Jenkins

Jenkins Cheatsheet

Installation and Setup

  • Download Jenkins

  • 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

  • Unlock Jenkins

    • Follow on-screen instructions to unlock using the initial admin password located at /var/lib/jenkins/secrets/initialAdminPassword.

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]
    

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.
  • 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/.
  • 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.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!