Jenkins - pranavkumarpk01/MD-DevOps GitHub Wiki

πŸš€ What is Jenkins?

Jenkins is an open-source automation server written in Java. It is used to automate parts of software development, including building, testing, and deploying code.


πŸ”§ Jenkins Installation

On Ubuntu/Debian:

sudo apt update
sudo apt install openjdk-11-jdk -y
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 update
sudo apt install jenkins -y
sudo systemctl start jenkins
sudo systemctl enable jenkins

πŸ‘₯ Jenkins: Creating Users, Jobs, and Master-Slave Configuration


βœ… Creating Users in Jenkins

Jenkins allows multiple users to access and manage jobs securely.

Steps:

  1. Go to Manage Jenkins > Manage Users.
  2. Click on Create User.
  3. Fill in the following details:
    • Username
    • Password
    • Full name
    • Email
  4. Click Create User.

πŸ” You must have Matrix-based security or Role-based authorization enabled to manage users.


βš™οΈ Creating Jobs in Jenkins

Jenkins Jobs are tasks like building a project, running tests, deploying apps, etc.

Steps to Create a Job:

  1. From Jenkins Dashboard, click New Item.
  2. Enter a job name.
  3. Choose a type:
    • Freestyle Project (GUI-based)
    • Pipeline (Scripted)
    • Multi-configuration Project (for matrix builds)
  4. Click OK.
  5. Configure the job:
    • Source Code Management: Git, SVN, etc.
    • Build Triggers: SCM polling, schedule, webhook
    • Build Steps: Shell commands, Maven, Gradle, etc.
    • Post-build Actions: Email notifications, archiving, etc.
  6. Click Save.

πŸ“Œ Once saved, you can click Build Now to run the job.


πŸ”— Configuring Jenkins Master-Slave Architecture

Jenkins supports a distributed build architecture with Master and Slave (Agent) nodes.

πŸ’‘ Why Use Master-Slave?

  • Distribute builds across multiple machines.
  • Reduce load on master.
  • Run jobs in parallel.
  • Run jobs in specific environments (labels/tags).

🧱 Master:

  • Central Jenkins server.
  • Manages UI, job scheduling, result tracking.
  • Can also execute jobs.

βš™οΈ Slave (Agent):

  • Remote machine connected to Master.
  • Executes jobs as per Master's instruction.

πŸ”§ Steps to Configure Master-Slave:

  1. Go to Manage Jenkins > Manage Nodes and Clouds.
  2. Click on New Node.
  3. Give a name, select Permanent Agent, and click OK.
  4. Fill in:
    • Remote root directory (e.g., /home/jenkins)
    • Labels (e.g., linux, build, test)
    • Launch method:
      • SSH
      • Java Web Start (deprecated)
      • Custom launcher
  5. Save the node.
  6. Jenkins will attempt to connect to the slave machine and establish communication.

πŸ›  Requirements on Slave:

  • Java should be installed.
  • Jenkins master must be able to SSH into slave.

πŸ” Verify:

  • Node status will show as Connected and Idle if configured correctly.
  • Use labels to bind jobs to specific slaves.

⚠️ Ensure port 50000 (JNLP) or SSH port is open between master and slave for communication.


πŸš€ Jenkins Pipelines, Jenkinsfiles, and Git Integration


πŸ›  Creating Pipelines in Jenkins

Jenkins Pipeline is a suite of plugins that supports implementing continuous delivery pipelines as code.

πŸ”Ή Types of Pipelines:

  • Declarative: Simpler, structured syntax
  • Scripted: More flexible, Groovy-based

βœ… Steps to Create a Pipeline:

  1. From Jenkins Dashboard β†’ Click New Item.
  2. Enter a name and select Pipeline.
  3. Click OK.
  4. In the configuration:
    • Add description (optional).
    • Under Pipeline, choose:
      • Pipeline script (inline)
      • Or Pipeline script from SCM (Git repo).
  5. Write or paste your pipeline code (Jenkinsfile).
  6. Save and click Build Now to execute.

🧾 Example Declarative Pipeline:

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests...'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying the app...'
            }
        }
    }
}

πŸ”— Git Integration with Jenkins

Integrating Git with Jenkins enables version-controlled CI/CD pipelines and seamless automation with your source code.


πŸ“¦ Step-by-Step Git Integration

1️⃣ Install Git Plugin in Jenkins

The Git Plugin is required to enable Git SCM features in Jenkins.

πŸ”§ How to Install:

  • Navigate to: Manage Jenkins β†’ Manage Plugins
  • Go to the Available tab
  • Search for: Git plugin
  • Click Install without restart

πŸ”„ You can also check the Installed tab to verify if Git plugin is already installed.


2️⃣ Configure Global Git Settings

Jenkins needs to know the location of the Git binary on the system.

  • Go to: Manage Jenkins β†’ Global Tool Configuration
  • Under Git, click Add Git
  • Name it (e.g., Default Git)
  • Let Jenkins auto-detect the path OR manually set the Git executable path (like /usr/bin/git)

3️⃣ Create Jenkins Job with Git Integration

For Freestyle Projects:

  • Go to New Item β†’ Enter job name β†’ Select Freestyle Project
  • Under Source Code Management, choose Git
  • Enter:
    • Repository URL (e.g., https://github.com/user/repo.git)
    • Branch to build (default is */main or */master)
    • Credentials (if it’s a private repo)

For Pipeline Jobs:

Use Git inside the Jenkinsfile like this:

pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        git branch: 'main', url: 'https://github.com/user/repo.git'
      }
    }
  }
}

πŸ” Managing Git Credentials in Jenkins


πŸ“₯ Where to Add Credentials

  • Navigate to: Manage Jenkins β†’ Credentials
  • Choose the appropriate domain (e.g., Global)
  • Click Add Credentials

🧾 Types of Credentials

Type Description
Username/Password For HTTPS Git URLs requiring a username and password
SSH Username with Private Key For Git over SSH (e.g., GitHub using SSH key)
Secret Text For personal access tokens (PAT)
Certificate For advanced use-cases involving cert-based auth

🎯 Use Secret Text if you're integrating GitHub using a Personal Access Token (PAT) for enhanced security.


πŸ›  Using Credentials in Jenkins Job

  • When configuring Git in Freestyle or Pipeline job, select the appropriate credentialsId.
  • Example in Jenkinsfile:
git url: 'https://github.com/user/repo.git', credentialsId: 'github-token-id'