How to Build a Simple CI Pipeline in Jenkins: A Beginner’s Guide

H A R S H H A A - Oct 2 - - Dev Community

Introduction

In today's fast-paced software development world, Continuous Integration (CI) has become the backbone of efficient code deployment. It allows development teams to integrate their work frequently, with automated builds and tests running to catch issues early. Jenkins is one of the most popular tools for automating CI pipelines, offering flexibility, ease of use, and integration with almost any tool a DevOps engineer might need.

This guide is designed for beginners who want to build a simple CI pipeline in Jenkins, but it will also provide deeper insights to help DevOps engineers understand the full potential of Jenkins in automating workflows.


Table of Contents

  1. What is Continuous Integration (CI)?
  2. Why Jenkins?
  3. Jenkins Installation and Setup
  4. Creating Your First Jenkins Job
  5. Configuring Source Code Management (SCM) with GitHub
  6. Adding Build Triggers
  7. Defining Build Steps (Compiling and Testing)
  8. Running and Monitoring the CI Pipeline
  9. Setting Up Automated Reports and Notifications
  10. Conclusion

1. What is Continuous Integration (CI)?

Continuous Integration (CI) is a software development practice where team members frequently merge their code changes into a shared repository, often several times a day. Each integration is automatically verified by creating a build and running automated tests against the merged code, ensuring bugs and issues are detected early.

Why is CI important?

  • Early Detection of Bugs: By continuously integrating, bugs are caught early and are easier to fix.
  • Reduced Merge Conflicts: Integrating frequently helps minimize conflicts between different branches of development.
  • Increased Team Collaboration: CI encourages collaboration between development, QA, and operations, fostering a DevOps culture.

Back to top


2. Why Jenkins?

Jenkins is an open-source automation server that supports building, testing, and deploying code. It plays a pivotal role in CI pipelines for several reasons:

  • Extensibility: Jenkins supports over 1,500 plugins, allowing integration with almost every tool.
  • Easy Setup: With a straightforward web interface, Jenkins makes it easy for even a beginner to get started.
  • Automation Flexibility: Jenkins can automate anything from simple code builds to complex multi-step workflows.

Back to top


3. Jenkins Installation and Setup

Before building a pipeline, you need to install and configure Jenkins. Here’s a step-by-step guide for setting it up.

Step 1: Installing Jenkins

For simplicity, we'll focus on an Ubuntu-based system.

  1. Install Java: Jenkins requires Java to run. Install it with:
   sudo apt update
   sudo apt install openjdk-11-jdk
Enter fullscreen mode Exit fullscreen mode
  1. Add Jenkins Repository: Import the GPG key and add the Jenkins repository.
   curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee \
   /usr/share/keyrings/jenkins-keyring.asc > /dev/null
   sudo sh -c 'echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
   https://pkg.jenkins.io/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
Enter fullscreen mode Exit fullscreen mode
  1. Install Jenkins:
   sudo apt update
   sudo apt install jenkins
Enter fullscreen mode Exit fullscreen mode
  1. Start Jenkins:
   sudo systemctl start jenkins
   sudo systemctl enable jenkins
Enter fullscreen mode Exit fullscreen mode

Step 2: Access Jenkins Web Interface

  • Open your browser and go to http://<your-server-ip>:8080.
  • During the initial setup, Jenkins will ask for an administrator password found in the /var/lib/jenkins/secrets/initialAdminPassword file. Copy and paste it to log in.
  • Complete the setup by installing the recommended plugins and setting up an admin user.

Back to top


4. Creating Your First Jenkins Job

Once Jenkins is installed, it’s time to create your first job (or project) that will form the foundation of your CI pipeline.

Step 1: Create a New Job

  1. Go to the Jenkins dashboard and click on "New Item."
  2. Enter a name for the job, choose Freestyle project, and click OK.

Step 2: Configure the Job

  • In the job configuration screen, you’ll see multiple sections like General, Source Code Management, Build Triggers, and Build Steps. We'll walk through configuring each one.

Back to top


5. Configuring Source Code Management (SCM) with GitHub

A crucial part of any CI pipeline is linking it to a source code repository where Jenkins will pull the code to build. For this example, we’ll use GitHub.

Step 1: Add GitHub Repository

  1. In the Source Code Management section, select Git.
  2. Enter the URL of your GitHub repository:
   https://github.com/<your-username>/<your-repo>.git
Enter fullscreen mode Exit fullscreen mode
  1. If your repository is private, you’ll need to add Git credentials. You can do this by selecting Add under Credentials, then providing your GitHub username and access token.

Back to top


6. Adding Build Triggers

Build triggers are conditions that determine when Jenkins should execute the job. For CI pipelines, a common practice is to trigger a build whenever there is a new code commit.

Step 1: Triggering a Build with Every Commit

  1. In the Build Triggers section, check GitHub hook trigger for GITScm polling. This means Jenkins will trigger a build every time there’s a new push to the GitHub repository.

  2. Set up a GitHub Webhook:

    • Go to your repository on GitHub, then navigate to Settings > Webhooks > Add webhook.
    • Paste the following URL as the payload URL:
     http://<your-jenkins-server>/github-webhook/
    
  • Select application/json as the content type and click Add webhook.

Back to top


7. Defining Build Steps (Compiling and Testing)

Now that Jenkins knows when to pull the code, you need to define what happens during the build process. For simplicity, let’s say we’re working on a Java Maven project, and we want Jenkins to compile the code and run tests.

Step 1: Add Build Steps

  1. In the Build section, click Add build step and select Invoke top-level Maven targets.
  2. In the Goals field, type:
   clean install
Enter fullscreen mode Exit fullscreen mode

This will clean the previous build files and compile your project from scratch.

Step 2: Add Test Step

If you want Jenkins to run tests after the build, ensure that your pom.xml file has test phases, and Maven will automatically execute them.

Back to top


8. Running and Monitoring the CI Pipeline

Now that the job is configured, let’s run it manually for the first time and monitor its status.

Step 1: Manually Trigger the Job

  1. From the Jenkins dashboard, click on your job name.
  2. On the left-hand side, click Build Now to trigger a manual build.
  3. Jenkins will pull the code, build it, and run the tests (if configured).

Step 2: Monitor Build Logs

  1. Once the build is running, click on the Build #1 on the left-hand side.
  2. Select Console Output to see the build logs in real-time.

Back to top


9. Setting Up Automated Reports and Notifications

To complete the CI pipeline, it’s essential to set up reports and notifications so that the team is aware of build failures or successes.

Step 1: Install Email Extension Plugin

  1. Go to Manage Jenkins > Manage Plugins.
  2. Search for the Email Extension Plugin and install it.

Step 2: Configure Email Notifications

  1. In your job configuration, scroll down to Post-build Actions.
  2. Select Editable Email Notification and configure it to send an email when the build fails or succeeds.
  3. You can also add recipient email addresses to notify team members.

Back to top


10. Conclusion

Building a CI pipeline in Jenkins is an essential skill for any DevOps engineer. By following this guide, you’ve learned how to install Jenkins, set up a simple pipeline, and trigger builds based on GitHub commits. As you become more comfortable with Jenkins, you can expand this pipeline to include more stages, like deployment, code quality analysis, and security checks.

By automating builds and tests, you’re not only improving your workflow but also embracing the culture of DevOps, which emphasizes collaboration, automation, and continuous feedback.

Back to top


👤 Author

banner

Join Our Telegram Community || Follow me on GitHub for more DevOps content!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player