Learn Jenkins! Complete Jenkins Course - Zero to Hero

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, an open-source automation server widely used for building, testing, and deploying software. Whether you're a beginner or looking to enhance your skills, this guide will walk you through the setup of Jenkins, including creating CI/CD pipelines, managing Jenkins infrastructure, and utilizing both Freestyle jobs and Groovy pipelines.

Step 1: Understanding Jenkins

  • Jenkins is an automation server that supports continuous integration and continuous deployment (CI/CD).
  • It allows developers to automate parts of the software development processes, making it essential for DevOps practices.
  • Familiarize yourself with key concepts:
    • Jenkins Master Server: The main server that manages the Jenkins environment.
    • Jenkins Agents: Machines that run the jobs assigned by the master server, which can be either permanent or cloud-based.

Step 2: Setting Up Jenkins Using Docker

  1. Install Docker on your machine. Follow the official Docker installation guide for your operating system.
  2. Pull the Jenkins Docker image by running:
    docker pull jenkins/jenkins:lts
    
  3. Run Jenkins in Docker with the following command:
    docker run -d -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts
    
  4. Access the Jenkins web interface by navigating to http://localhost:8080 in your web browser.

Step 3: Exploring the Jenkins Web GUI

  • After accessing Jenkins, follow the on-screen instructions to unlock Jenkins using the initial admin password found in the Docker logs.
  • Set up your Jenkins environment by installing suggested plugins and creating your first admin user.

Step 4: Creating a Simple Freestyle Job

  1. Click on "New Item" from the Jenkins dashboard.
  2. Enter a name for your job and select "Freestyle project."
  3. Configure the job by:
    • Specifying the source code repository (e.g., Git).
    • Defining build triggers (e.g., polling the SCM).
    • Adding build steps (e.g., executing a shell command).
  4. Save and run the job to see it in action.

Step 5: Running Python Scripts with Jenkins

  • Add a build step to your Freestyle job:
    • Select "Execute Shell" and enter your Python script command:
      python your_script.py
      
  • Configure the workspace to ensure that your script runs correctly.

Step 6: Setting Up Docker Cloud Agents

  1. Configure Docker as a cloud agent in Jenkins:
    • Go to "Manage Jenkins" > "Manage Nodes and Clouds" > "Configure Clouds."
    • Add a new Docker cloud and specify the Docker host URI.
  2. Define Docker agent templates, including settings for images and labels.

Step 7: Automating Builds on Commit

  • Set up your Freestyle job to trigger builds automatically on commits:
    • Navigate to the job configuration and check "Poll SCM."
    • Set a schedule (e.g., H/5 * * * * for every 5 minutes).

Step 8: Setting Up Declarative Pipelines Using Groovy

  1. Create a new pipeline job by selecting "Pipeline" when creating a new item.
  2. In the Pipeline section, use the following example of a declarative pipeline:
    pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    echo 'Building...'
                }
            }
            stage('Test') {
                steps {
                    echo 'Testing...'
                }
            }
            stage('Deploy') {
                steps {
                    echo 'Deploying...'
                }
            }
        }
    }
    
  3. Save and run your pipeline to observe the stages in action.

Step 9: Using a Jenkinsfile for Pipelines

  • Create a Jenkinsfile in your repository to define the pipeline as code.
  • Ensure that the pipeline is triggered using the Jenkinsfile located in your source control.

Step 10: Exploring Jenkins Blue Ocean

  • Install the Blue Ocean plugin from "Manage Jenkins" > "Manage Plugins."
  • Access Blue Ocean from the Jenkins dashboard for a modern UI to visualize and manage your CI/CD pipelines.

Conclusion

You have now learned the essentials of setting up and using Jenkins for CI/CD. From creating Freestyle jobs to working with Groovy pipelines and Blue Ocean, you can effectively automate your software development processes. Next steps include further exploring advanced Jenkins features, integrating with other tools, and participating in the community for continuous learning.