Triggering a Jenkins build from a push to Github

In this article we will look at setting up the basis for Continuous Integration using Jenkins for orchestration and Github for source control. We will be configuring a Jenkins build to be initiated on a push to a repository.

1. Install Github Integration Plugin

First we need to install the GitHub Integration Plugin, this will give us the ability to configure Jenkins to use our Github repository.

Installing Github integration plugin

2. Prepare Github repository

We need to add a service to call the Jenkins Github webhook on a push, to do this go to settings -> integrations & Services and add a new service. The Jenkins Github plugin service should be in the list of available services.

Enter the URL of your Jenkins instance followed by /github-webhook/

3. Giving the Jenkins user access to the Github repository

We need to give the Jenkins user access to our repository by adding a deploy key in the Github settings.

The first step is generating SSH keys for the Jenkins user if they do not already exist.

jenkins@ip:/home/ubuntu$ ssh-keygen

Depending on where the key was created, you need to copy the public key so that it can be added to Github

jenkins@ip:/home/ubuntu$ cat /var/lib/jenkins/.ssh/

Add the key copied in the previous step to Github. To do this head to the repository settings -> Deploy keys

The last step is to check that everything is working as expected, as the Jenkins user in your console enter the below to check the connection to Github.

jenkins@ip:~/.ssh$ ssh

If successful you should see the following message

Warning: Permanently added the RSA host key for IP address '{YOUR_SERVER_IP}' to the list of known hosts.
PTY allocation request failed on channel 0
Hi marcbest/medium-jenkins-git-tut! You've successfully authenticated, but GitHub does not provide shell access.
Connection to closed.

4. Update Jenkins job with Github configuration

In the ‘General’ section of the job configuration check the Github project tick box and enter the URL to the repository that you configured in step 2.

Next update the Source Code Management section, first set the repository URL (note the format{YOUR_REPO}. You can also specify the branch you would like to use.

The last step is to tell Jenkins to build when the Github hook is called, select the highlighted option below in the Build Triggers section.

That’s it! Your Jenkins build should now be triggered whenever a push is made to your repository.