Create your first CI/CD pipeline with Jenkins and GitHub

A guide to create and deploy a freestyle Jenkins project

Pratik Choudhari
Towards Data Science

--

Photo by Samuel Sianipar on Unsplash

So, quickly imagine this, your team is working very hard every day and trying to roll out new features as soon as possible but to take this project with updated code into testing and production phases we have to update the servers and make sure nothing goes wrong in this process. The obvious, but taxing, solution here is to manually pull code from version control into the servers, run the tests and give it a green flag, all good? Sadly, No! As the frequency of commits increase a developer has to do this whole server update process again and again.

Here’s a manual way to update servers:

  1. SSH into the server.
  2. Pull from VCS.
  3. Check configs before starting the app.
  4. Start app in containers.
  5. Check logs and make sure app is running.
  6. End SSH session.

So, how to automate this process? Enter the paradigm of Dev Ops, Dev Ops focuses on making the cycles of development and operations compatible with each other, it tries to bind them both together. Jenkins is a CI/CD Dev Ops tool built in Java.

As mentioned in the title, we are going to see how Jenkins can help us eliminate this manual process and automate the entire flow.

Prerequisites

Before getting started with our recipe let’s get our ingredients straight.

This tutorial is going to use Jenkins installed on local machine, in case you are doing it on a remote computer replace localhost by your computer IP

Firstly, Jenkins needs to be installed on the computer you are trying to deploy on, installation instructions here: https://www.jenkins.io/doc/book/installing/. Secondly, this demo is going to use docker so if you want to follow along install docker on your system, guide: https://docs.docker.com/engine/install/.

In case, you want to learn docker here’s a GitHub repository I have created to explain the basics and intermediate concepts of docker. The explanation is in simple English organized in markdown files with a few resources mentioned after every lesson.

Lastly, we need a project repository set up on a VCS like GitHub.

Post Installation Steps

Once Jenkins installation is completed, go to http://localhost:8080 and a screen will appear asking you to unlock Jenkins. To set up your user, which is a mandatory process, follow this guide: https://www.jenkins.io/doc/book/installing/linux/#setup-wizard

Creating Jenkins Project

Jenkins has 2 types of projects, Freestyle and Pipeline. The major difference in both is that pipeline projects follow a particular flow and can have multiple agents and stages to build, test and deploy the project. Freestyle project is easy to set up and get started with. Now let’s start with our project configuration.

After you are logged in, you will see a screen like this. This is the Jenkins home page or dashboard, here a user can see all the pipelines created and a summary of the pipeline.

Jenkins Dashboard

Here, we have to click on New Item at the top left, following this, enter a name for your project, click on Freestyle project and hit OK

The next step is to configure our project with VCS information, I am going to use GitHub as a VCS. Tick GitHub project and and enter the repository URL.

Scroll down a little and we can see a Source Code Management form asking for the credentials to log in to VCS and the branch to use. In case of a fresh installation, credentials need to be added to Jenkins, to do this click on the Add button, enter your username and password click on Add.

Now we enter in the section of Build Triggers, here we set triggers which will tell Jenkins when to build a project. The most common and efficient way to do this is by using web-hooks.

Web hooks allow interaction between web-based applications through the use of custom callbacks. The use of web-hooks allows web applications to automatically communicate with other web-apps. A web-hook is set to trigger when a certain event occurs.

Source: GeeksForGeeks

In our case, the event which will trigger the webhook is going to be a push action to the remote repository. We are not going to use webhooks in this tutorial instead we will be using the Poll SCM option, what this option does is that it continuously queries the VCS, based on a predefined schedule, for new changes. If new changes are encountered then Jenkins will start the project build steps.

The predefined schedule used here is * * * * * which is a time schedule for every minute, Jenkins uses a syntax similar to Crontab, explore the format on crontab.guru.

The only step remaining is to write the build commands to do so head over to the Build section and write your docker build and run commands in detached mode.

We are done setting up our project, the last step is to click Save at the bottom of screen and start the first build by clicking on the Build Now button.

On a successful build, the build number will show a green tick icon and your app will be published on specified port of defined network, in our case, the app will go live on localhost:3000. You can check the console output for the build process by clicking on the drop down beside the build number.

With the help of this console output, a user can debug the build process.

Conclusion

Hope you were able to follow along and successfully complete your first build. I will be more than happy to answer your doubts in the comments. Thanks for reading! Stay in touch for more such tutorials on automation and python projects.

--

--