Configuring Jenkins with Github

Introduction

In our last story, we have installed Jenkins on ubuntu. Now we will create our first job on Jenkins which will pull the latest code from codebase and then execute some command on it. But for that we need a codebase and for that we are going to use Github on which we can put our source code. So, lets start hacking!

Prerequisites

  • You must have a Github account and a repository on which we can test Jenkins.
  • You must have a configured Jenkins Server. If you do not have, go and first set-up Jenkins Server.
⚠️ If you don’t have a Github account yet. Click here

1. Connecting your github repository with jenkins

First, we have to setup out Github repository so that whenever we make a commit in any branch of this repository, a web hook will be triggered and tell our Jenkins server that its time to start a build.

  • Open you Github repository and click on Settings Tab.
  • Then click on Integrations & Services and click Add Service. Here start typing jenkins and install Jenkins plugin for Github.
  • Then you have to enter a Jenkins hook url. Every time when code will be pushed into your repository or any commit will made, Github will tell Jenkins (which will be watching on this URL) that it has to trigger a job corresponding to this hook. The URL will be something like this :

http://<ip_or_domain_name>:<port>/github-webhook/

  • Specify your IP or Domain Name of your Jenkins Server and click Add Service. This service is now active on this repository.
To test that this Service is working or not. Open this service and click on Test Service. If all things are fine, it will show you a success message.

2. Creating first job on Jenkins

  • From the Jenkins interface home, select New Item and enter a name and select Freestyle Project.

After this click OK and you will be redirected to configuring section.

  • Now this page is where you specify the job configuration. As you’ll quickly observe, there are a number of settings available when you create a new project. Generally, one of the more important controls is to connect to a source repo.
  • Under Source Code Management Section, click git and add your credentials by click Add.
There are many ways to provide credentials. Click to see all of them.
  • Under Build Trigger Section, make sure to check Github hook trigger for GITScm polling. It means this job will be execute every time a commit is made in your github repository.
  • On this configuration page you also have the option to add build steps to perform extra actions like running scripts.

This will provide you with a text box in which you can add whatever commands you need. Use this to run various tasks like server maintenance, version control, reading system settings, etc.

We’ll use this section to run a script. Again, for demonstration purposes, we’ll keep it extremely simple.

If you want, you can add subsequent build steps as well. Keep in mind that if any segment or individual script fails, the entire build will fail.

You can also select post-build actions to run, such as emailing the results to yourself.

Save the project, and you’ll be taken to its project overview page. Here you can see information about the project, including its built history, though there won’t be any of that at the moment since this is a brand-new project.

3. Testing what we have done yet

Click Build Now on the left-hand side to start the build. You will momentarily see the build history change to indicate it is working. When done, the status icon will change again to show you the results in a concise form.

To see more information, click on that build in the build history area, whereupon you’ll be taken to a page with an overview of the build information:

The Console Output link on this page is especially useful for examining the results of the job in detail — it provides information about the actions taken during the build and displays all the console output. Especially after a failed build, this can be a useful place to look.

If you go back to Jenkins home, you’ll see an overview of all projects and their information, including status (in this case there’s only the one):

Status is indicated two ways, by a weather icon (on the home page dashboard, seen above) and by a colored ball (on the individual project page, seen below). The weather icon is particularly helpful as it shows you a record of multiple builds in one image.

In the image above, you see clouds, indicating that some recent builds succeeded and some failed. If all of them had succeeded, you’d see an image of a sun. If all builds had recently failed, there would be a poor weather icon.

These statuses have corresponding tooltips with explanations on hover and, coupled with the other information in the chart, cover most of what you need in an overview.

5. Conclusion

In this tutorial we learned how to link our Github codebase with our new Jenkins server and how to trigger build on Jenkins whenever there will be a commit in our codebase. Next week we will learn how to deploy a NodeJS application on server using dokku.

Happy Coding!!


If you like this article, please ❤/👏 or share it! For more like this, follow me on Medium or Twitter. To ask a Question visit this link. More about me on my website.

Subscribe to our newsletter service if you want to stay update with trending articles, repositories, modules and many more things.

Like what you read? Give Gaurav Mehla a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.