Configuring Jenkins for CI/CD

Tutorial 3 of 5

1. Introduction

In this tutorial, our main goal is to configure Jenkins for Continuous Integration/Continuous Delivery (CI/CD). Jenkins is an open-source automation server that allows you to automate different stages of your delivery pipeline. You will learn how to set up Jenkins, create a simple pipeline, and integrate it with a version control system.

You will learn how to:
- Install Jenkins
- Configure Jenkins
- Create a Jenkins pipeline
- Integrate Jenkins with GitHub

Prerequisites:
- Basic knowledge of version control systems, preferably Git
- Java installed on your system
- A GitHub account

2. Step-by-Step Guide

2.1 Installing Jenkins

First, you need to install Jenkins. Here are the steps to install Jenkins on Ubuntu:

  1. Update your system with sudo apt update
  2. Install Java Development Kit with sudo apt install openjdk-11-jdk
  3. Add the Jenkins Debian repository with wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
  4. Append the Debian package repository address to the server's sources.list: echo deb http://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list
  5. Update the apt package list and install Jenkins sudo apt update && sudo apt install jenkins

After installation, Jenkins service will start automatically. You can check its status using systemctl status jenkins

2.2 Configuring Jenkins

Open your web browser and navigate to http://localhost:8080. You'll see a Unlock Jenkins page. To find the password, use the command sudo cat /var/lib/jenkins/secrets/initialAdminPassword. Copy the password, paste it into the form, and click Continue.

Next, you'll be prompted to customize Jenkins. For this tutorial, select Install suggested plugins. Jenkins will then install the suggested plugins.

2.3 Creating a Jenkins Pipeline

Once Jenkins is configured, you need to create a new job. On the Jenkins dashboard, click on New Item. In the next screen, enter a name for the job, select Pipeline, and click OK.

In the pipeline configuration page, scroll down to the Pipeline section. Here, you can define your pipeline script. For simplicity, we'll create a pipeline that prints a simple message.

pipeline {
    agent any

    stages {
        stage('Stage 1') {
            steps {
                echo 'Hello, Jenkins!'
            }
        }
    }
}

Click Save to finish creating the pipeline.

2.4 Integrating Jenkins with GitHub

To integrate Jenkins with GitHub, you need to install the GitHub plugin. Go to Manage Jenkins > Manage Plugins > Available, then find and select GitHub plugin. Click on Install without restart.

After installing the plugin, go to your job configuration page and in the Pipeline section, select Pipeline script from SCM. Then, select Git and enter your repository URL.

3. Code Examples

Here's an example of a Jenkins pipeline that clones a Git repository and builds a Java project with Maven. The example assumes you have a Maven project in your repository.

pipeline {
    agent any

    stages {
        stage('Clone repository') {
            steps {
                git 'https://github.com/your-repository.git'
            }
        }

        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }
    }
}

4. Summary

In this tutorial, you've learned how to install and configure Jenkins, create a simple pipeline, and integrate Jenkins with GitHub. To expand your knowledge, you can explore how to use Jenkins with other version control systems, and how to configure complex pipelines.

5. Practice Exercises

Exercise 1: Install Jenkins on your local machine and create a simple pipeline that prints a message.

Exercise 2: Create a Jenkins pipeline that clones a Git repository and lists the files in the repository.

Exercise 3: Create a Jenkins pipeline that clones a Git repository, builds a Java project with Maven, and archives the build results.

Remember, practice is the key to mastering any skill. Keep experimenting with different pipeline configurations and Jenkins features.