#
Integrate Jenkins with GitHub (using Webhooks)
This tutorial explains to you how to integrate Jenkins with GitHub using a webhook.
There are 2 ways we can integrate Jenkins with GitHub:
- using a
Poll SCM strategy
: when Jenkins on a regular basis check for updates in GitHub - using a
webhook strategy
: when GitHub trigger the Jenkins job after every push in the repo
In this tutorial I prent the Poll SCM strategy method.
Prerequisites:
- we have a GitHub repository named "GitRepo1"
- we have created the application from the tutorial named Compile & Run a Java Application in Jenkins
- We have Jenkins already installed and the Jenkins console is accessible at 192.168.72.132:8080
- "HelloWorld.java" class is in the root directory of the "GitRepo1"
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- ngrok is configured in order to forward an Internet request to 192.168.72.132:8080 as in the following image:
In this case, https://eafd-95-76-244-219.eu.ngrok.io
request goes to my local computer which forward
the request to http://192.168.72.132:8080
.
The steps for integrating Jenkins and GitHub using a webhook strategy
are:
In GitHub
Go to the repository which keeps your code and go to "Settings" and click on "webhooks"
Click on "Add webhook" and enter the webhook information:
The "Payload URL" is the one from ngrok + "github-webhook/". Don't forget the last "/" !!! The "Content type" must be "application/json". There is no "Secret" defined in Jenkins for this. Click on "Add webhook" button.
Each time a hook is triggered we can see it in "Recent Deliveries" tab:
At this point, GitHub notifies Jenkins when an event occurs in GitHub repository ("GitRepo1" repo in my case). Now we need to configure Jenkins to receive notifications from GitHub.
In Jenkins
- Go to the job configuration and configure:
Project URL
Repository URL:
The branch used by the job
define the build triggers
Here we need to check "GitHub hook trigger for GITScm polling".
define what the job must do
click on "Save" button.
At this point, Jenkins is integrated with GitHub.
We can verify if the integration is working by committing something in GitHub.
In Console Output of the job you can see something like this:
Started by GitHub push by ptomoiu
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/Compile_Run_Java_Application
The recommended git tool is: NONE
No credentials specified
> git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/Compile_Run_Java_Application/.git # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/ptomoiu/GitRepo1.git # timeout=10
Fetching upstream changes from https://github.com/ptomoiu/GitRepo1.git
> git --version # timeout=10
> git --version # 'git version 2.39.0'
> git fetch --tags --force --progress -- https://github.com/ptomoiu/GitRepo1.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse origin/main^{commit} # timeout=10
Checking out Revision 7922ce2fd4639fd449268c2aa4c1ed67f9de18a2 (origin/main)
> git config core.sparsecheckout # timeout=10
> git checkout -f 7922ce2fd4639fd449268c2aa4c1ed67f9de18a2 # timeout=10
Commit message: "Update HelloWorld.java"
> git rev-list --no-walk 7922ce2fd4639fd449268c2aa4c1ed67f9de18a2 # timeout=10
[Compile_Run_Java_Application] $ /bin/sh -xe /tmp/jenkins16697023168051956715.sh
+ javac HelloWorld.java
+ java HelloWorld
Hello, World A !
Finished: SUCCESS