How to connect Jenkins and GitHub with webhooks

Lukasz Skowroński - Senior Solutions Architect

31 Jan 2024

Share on social media

In the previous blog post we shared with you the process that helps with making your Sitecore’s CI/CD pipeline more bulletproof. In this blog post we will focus on the configuration that has to be applied to send webhook data from GitHub to Jenkins. 

Webhook Configuration

To configure webhooks you have to be an administrator and have access to the settings of repository and hooks section.  

In the hooks section you have to press “Add webhook” button 

A screenshot of the Webhook settings.

It will open a form where you need to specify: 

    • Payload URL (url where payload is being sent)  
    • Content Type (application/x-www-form-urlencoded) 
    • Type of the events (pull requests) 
A screenshot of the Manage Webhook settings.
A screenshot of the pull requests command and what it does.

Url that has to be specified consists of: 

    • url of your Jenkins instance 
    • endpoint 
    • token 

and can look like this: http://jenkins.host/generic-webhook-trigger/invoke?token=xyz

Jenkins Configuration 

The URL that we used in the previous step will be partially generated by the Jenkins plugin “Generic Webhook Trigger Plugin”.  Thanks to this plugin in the build configuration you will see a new configuration option in the “Build Triggers” section where you will find information about the mentioned URL.  

A screenshot of the generic webhook trigger command.

At the end of the URL configured on the GitHub side we had to add also token that is part of configuration of this plugin.  

A screenshot of the build triggers.

The most important task of this plugin is conversion of the received  via webhook data into the object that you can use in your scripts. We must pay a lot attention to the notation at this point. 

When GitHub sends data to Jenkins, it sends JSON object. When Jenkins receives that JSON object, it converts it to array of objects what is not well documented and may take some of your time to realize that. Based on the data being sent from GitHub you would expect expression like “$.payload” – but that would not work.  

In the “Post content parameters” you have to configure payload variable as follows: 

A screenshot of the expression field being filled out within the Build Triggers.

PowerShell Scripts 

The next step that would make sense is to use payload variable in your scripts. Start with adding build step called “PowerShell” and then inside of it you can access variable by: $ENV:payload 

Because we want to use with the Objects we have to convert Json to Object understandable by PowerShell scripts and we can make it by running a command: $payload = ConvertFrom-Json $ENV:payload

Starting from now you can use all of the properties that this object has like: 

    • name of the action: $payload.action 
    • pull request number: $payload.number 
    • pull request url: $payload.pull_request.url 
    • and many more 
A screenshot of the PowerShell command.

Summary 

With having all that data available in your PowerShell scripts you can build many very useful processes.  

Sign up to our newsletter

Share on social media

Lukasz Skowroński

Lukasz Skowroński

I have been awarded with the Sitecore MVP award seven times (the first time in 2017) for my continued support of the Sitecore Community. Besides blogging, as a Sitecore Community member, I organize all of the Sitecore User Group meetups in Poland. Since 2021 I have helped to organize the Sitecore User Group Conference (SUGCON) as one of the co-organizers.


Subscribe to newsletter