CI/CD Pipelines with Jenkins and Docker: A Beginner’s Project Journey

@Harsh
6 min readFeb 21, 2024

--

INTRODUCTION

In today’s software development landscape, speed, reliability, and efficiency are paramount. Continuous Integration and Continuous Deployment (CI/CD) pipelines, powered by automation tools like Jenkins, have emerged as indispensable assets for modern development teams. In this comprehensive guide, we’ll embark on a journey to unravel the intricacies of CI/CD pipelines using Jenkins, catering specifically to beginners.

Overview of the Project

Our project is designed to provide a hands-on demonstration of a basic CI/CD pipeline setup using Jenkins, with Docker as an essential component. We’ll walk through each step of the process, from cloning a GitHub repository to deploying a simple web application. Leveraging Docker containers, we’ll ensure consistency and portability across different environments, facilitating seamless deployment.

Understanding the Use Case

Imagine you’re part of a software development team tasked with building and deploying web applications. As deadlines loom and user demands escalate, manual deployment processes become increasingly cumbersome and error-prone. By implementing a CI/CD pipeline, you can automate repetitive tasks, accelerate the release cycle, and ensure consistent and reliable deployments, ultimately enhancing productivity and reducing time-to-market.

Practical Steps

1. Agent Configuration

The first step in our journey is configuring Jenkins agents to handle different stages of the CI/CD pipeline. We’ll set up three agents: one for building, one for testing, and one for production. We also set the labels for each agent that will help jenkins to restrict the job in a particular agent.

For launching the agent, refer this article and launch three agents for this project with name “Building”, “Testing” and “Project”.

Simplifying Jenkins Master-Slave Architecture👨‍🍳: A Comprehensive Guide

Additionally, we’ll configure the master node to handle email notifications, keeping stakeholders informed throughout the process.

We will go at the master node server side, and create the file with name “email_code.py” in python :

Let’s create the job for email notification with name “false_feedback” because it will run in master node only.

Here we put that only if the upstream job that is testing_stage is fail means unstable then run this job.

Now we will copy the command that we want to run in this job:

2. Building Stage (Job)

With our agents in place, we’ll dive into the building stage of the pipeline. Using a Jenkins job configured to monitor changes in our GitHub repository, we’ll trigger the building stage whenever new code is pushed. This job will execute on the building agent, where it will clone the repository, build the Docker image, and push it to the Docker registry, laying the foundation for subsequent stages.

We want that this job will run only on building agent that we created in above step, therefore we will make use of labels,

Add the github link that you want to monitor from this job,

After this we will set the time that after how many minutes, it has to lookout to github for changes.

This means it will keep on monitor github every minute and as soon as you make any changes there , it will trigger the job.

Now, after triggering the job it will extract the files and Dockerfile from the github to the agent and now we want to build the docker image and push it to the docker hub. For this we will give some commands in “Execute Shell”

3. Testing Stage

Upon successful completion of the building stage, Jenkins will automatically trigger the testing stage job. Running on the testing agent, this job will spin up a container with the built image and execute automated tests, ensuring the integrity and functionality of the application. If the tests pass, the job will proceed to the next stage; otherwise, it will notify the development team of the failure, enabling prompt resolution of issues.

Create the testing stage, and then configure it, we will perform some similar steps here ;

Now we want that this job will run automatically after successfully completing the building_stage.

Now we will run the container with the help of below command, and test it if this will launch successfully, it will go ahead and trigger production job.

Otherwise if it fails it will trigger the email job that we configure above, where it will notify developer.

4. Production Stage

Assuming the tests pass, Jenkins will seamlessly transition to the production stage, deploying the application in a production environment. This final job will run on the production agent, where it will pull the Docker image from the registry and launch the container, making the website live for users to access and interact with.

Creating the production_stage job and configure it for this job and to run automatically after testing job is successful.

That’s all, your project is ready. Now all you have to do is clone the github repository in your base OS and create the index.html and Dockerfile for creating docker image.

5. Cloning Github repo

Go to your Github repo and copy the address,

Clone it to your local PC,

Now go to the folder and create your required files;

The file are available on the github : https://github.com/harsh2478/Jenkins_training_2024.git

Then commit and push the files :

As soon as you push, it will uploaded to the github and trigger the “building_stage” job.

You will the scenario like this :

Pre-requisite

  1. Install git on your base OS.
  2. Install git on the target agent, here “building node” because from this agent we will monitor github.
  3. Install docker on all the three agents that is “building”, “testing”, and “production”.

Conclusion

By following along with our project, you’ve gained invaluable insights into the world of CI/CD pipelines and Jenkins automation. Armed with this knowledge, you’re well-equipped to embark on your journey towards mastering CI/CD practices and revolutionizing your software delivery process. As you continue to explore and experiment with Jenkins, remember that the possibilities are endless, and automation is the key to unlocking new realms of efficiency and innovation.

Stay curious, stay ambitious, and keep pushing the boundaries of what’s possible with Jenkins CI/CD pipelines. The journey may be challenging at times, but the rewards are well worth the effort. Here’s to your continued success in the world of software development!

Happy automating!

--

--

@Harsh
@Harsh

Written by @Harsh

A devOps engineer from India

No responses yet