Jenkins Full Course 2023 | Jenkins Tutorial For Beginners

4 min read 2 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 provides a comprehensive guide to Jenkins, a key tool in the DevOps pipeline, tailored for both beginners and experienced users. It will walk you through setting up Jenkins, managing jobs, and integrating various tools and processes to automate your software development lifecycle.

Step 1: Understanding Jenkins and Its Importance

  • What is Jenkins?
    • An open-source automation server used to automate parts of software development related to building, testing, and deploying.
  • Why use Jenkins?
    • Continuous integration and continuous delivery (CI/CD) capabilities.
    • Large plugin ecosystem for integration with various tools.
    • Supports multiple languages and technologies.

Step 2: Setting Up Jenkins on Ubuntu

  • Prerequisites:
    • A running Ubuntu machine.
    • Access to terminal with sudo privileges.
  • Installation Steps:
    1. Update your package index:
      sudo apt update
      
    2. Install Java Development Kit (JDK), required for Jenkins:
      sudo apt install openjdk-11-jdk
      
    3. Add the Jenkins repository and key:
      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'
      
    4. Install Jenkins:
      sudo apt update
      sudo apt install jenkins
      
    5. Start Jenkins service:
      sudo systemctl start jenkins
      
    6. Enable Jenkins to start at boot:
      sudo systemctl enable jenkins
      

Step 3: Configuring Jenkins

  • Accessing Jenkins:
    • Open your web browser and navigate to http://localhost:8080.
  • Unlock Jenkins:
    • Retrieve the initial admin password:
      sudo cat /var/lib/jenkins/secrets/initialAdminPassword
      
    • Enter the password in the setup wizard.
  • Install Suggested Plugins:
    • Follow the prompts to install the recommended plugins.

Step 4: Managing Jenkins Settings

  • Access Manage Jenkins:
    • From the dashboard, click on "Manage Jenkins".
  • Configure Global Tools:
    • Set up JDK, Git, Maven, etc., under "Global Tool Configuration".
  • Plugin Management:
    • Install additional plugins via "Manage Plugins".

Step 5: Setting Up Credentials

  • Open Credentials Manager:
    • In Manage Jenkins, click on "Manage Credentials".
  • Add New Credential:
    • Choose the appropriate domain, click "Add Credentials", and fill out the necessary fields (username, password, etc.).

Step 6: Configuring Jenkins Agents

  • Set Up a Virtual Machine:
    • Ensure the VM can communicate with Jenkins master.
  • Connect Agent to Jenkins:
    • On Jenkins, navigate to "Manage Jenkins" > "Manage Nodes and Clouds".
    • Create a new node and follow the instructions to connect your VM.

Step 7: Creating a Basic Freestyle Job

  • Create a New Job:
    • From the dashboard, select "New Item" and choose "Freestyle project".
  • Configure Source Code Management:
    • Link your Git repository under "Source Code Management".
  • Build Triggers:
    • Set triggers like "Poll SCM" to automate builds.

Step 8: Building with Maven

  • Add Build Step:
    • In the Freestyle project configuration, add a build step:
      • Select "Invoke top-level Maven targets".
    • Specify goals (e.g., clean install).

Step 9: Creating a Declarative Pipeline

  • Pipeline Job Creation:
    • Select "New Item" and choose "Pipeline".
  • Script Configuration:
    • In the pipeline script section, define your stages:
      pipeline {
          agent any
          stages {
              stage('Checkout') {
                  steps {
                      git 'https://your-repo-url.git'
                  }
              }
              stage('Build') {
                  steps {
                      sh 'mvn clean install'
                  }
              }
              // Additional stages like OWASP Check, Sonarqube Analysis, Docker Build & Push can be added here
          }
      }
      

Step 10: Deploying Applications

  • Docker Integration:
    • Ensure Docker is installed on the agent.
    • Add stages in your pipeline for building and pushing Docker images.
  • Access Deployed Application:
    • After deployment, access your application through the specified URL.

Conclusion

You have now set up Jenkins, configured it for job management, and learned how to create both Freestyle jobs and Declarative Pipelines. The next steps could involve exploring more advanced Jenkins features, integrating additional tools like Sonarqube for static code analysis, or setting up a multi-branch pipeline for managing multiple branches in a repository. Continue to explore the vast capabilities of Jenkins to enhance your DevOps practices.