Jenkins Tutorial For Beginners | Jenkins Full Course | Jenkins Tutorial

4 min read 3 hours ago
Published on Oct 11, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial is designed for beginners who want to get started with Jenkins, a popular automation server in the DevOps ecosystem. It covers the fundamentals of Jenkins, including installation, job creation, integration with GitHub, and advanced features like the CI/CD pipeline and distributed builds. By the end of this guide, you will have a solid understanding of Jenkins and how to utilize it for continuous integration and delivery.

Step 1: Understand DevOps and CI/CD

  • Familiarize yourself with key concepts:
    • DevOps: A set of practices that combines software development (Dev) and IT operations (Ops) to shorten the systems development life cycle.
    • Agile: An iterative approach to software development that promotes flexibility and customer satisfaction.
    • CI/CD: Continuous Integration and Continuous Delivery/Deployment practices that automate the testing and deployment of applications.

Step 2: Install Jenkins on Ubuntu

  • There are multiple ways to install Jenkins. Choose one of the following methods:
    1. Using the package manager:
      • Update your package index:
        sudo apt update
        
      • Install Java (Jenkins requires Java):
        sudo apt install openjdk-11-jdk
        
      • Add the Jenkins repository:
        wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
        sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
        
      • Install Jenkins:
        sudo apt update
        sudo apt install jenkins
        
      • Start Jenkins:
        sudo systemctl start jenkins
        
    2. Using Docker:
      • Pull the Jenkins image:
        docker pull jenkins/jenkins
        
      • Run Jenkins:
        docker run -d -p 8080:8080 jenkins/jenkins
        

Step 3: Create Your First Jenkins CI/CD Job

  • Set up a basic CI/CD job using Maven and GitHub:
    1. Access Jenkins by opening http://localhost:8080 in your web browser.
    2. Unlock Jenkins using the initial admin password found in /var/lib/jenkins/secrets/initialAdminPassword.
    3. Install suggested plugins.
    4. Create a new item:
      • Select "Freestyle project."
      • Name your project.
    5. Configure Source Code Management:
      • Choose Git and enter your GitHub repository URL.
    6. Set up Build Triggers:
      • Check "GitHub hook trigger for GITScm polling."
    7. Add a Build step:
      • Select "Invoke top-level Maven targets" and specify the goals (e.g., clean install).
    8. Save and build the job.

Step 4: Integrate Jenkins with GitHub

  • Set up a webhook in GitHub:
    1. Go to your repository on GitHub.
    2. Navigate to "Settings" > "Webhooks."
    3. Click "Add webhook."
    4. Enter the payload URL as http://<your-jenkins-url>/github-webhook/.
    5. Set content type to application/json.
    6. Select "Just the push event" and click "Add webhook."

Step 5: Explore Jenkins Master-Slave Architecture

  • Understand the architecture:
    • Master: Manages the build process and schedules jobs.
    • Slave: Executes the jobs assigned by the master.
  • To set up a slave:
    1. Install Java on the slave machine.
    2. From the master, go to "Manage Jenkins" > "Manage Nodes and Clouds."
    3. Create a new node and configure it.

Step 6: Create a Jenkins CI/CD Pipeline

  • Create a pipeline job:
    1. Select "New Item" and choose "Pipeline."
    2. Define the pipeline script:
      • Use a Jenkinsfile stored in your GitHub repo, or write the script directly.
    3. Example Jenkinsfile:
      pipeline {
          agent any
          stages {
              stage('Build') {
                  steps {
                      sh 'mvn clean install'
                  }
              }
              stage('Test') {
                  steps {
                      sh 'mvn test'
                  }
              }
              stage('Deploy') {
                  steps {
                      sh 'deploy.sh'
                  }
              }
          }
      }
      

Step 7: Implement Multi-branch Pipeline

  • Use multi-branch pipelines for better management of branches:
    1. Create a new item and select "Multibranch Pipeline."
    2. Add a branch source (e.g., Git).
    3. Configure it to scan the repository for branches and pull request builds.

Conclusion

By following this tutorial, you have learned the basics of Jenkins, from installation to creating CI/CD jobs and integrating with GitHub. You now have a foundation to explore more advanced features such as Jenkins pipelines and distributed builds. Next, consider experimenting with different plugins and configurations to optimize your CI/CD processes further.