DEV Community

Esther Nnolum
Esther Nnolum

Posted on

Trigger Jenkins builds with Github Webhook Using Smee Client

In line to automating a complete CICD pipeline where Jenkins pipelines are triggered by GitHub web-hook based on specific events; It has been argued that neither polling for updates nor exposing the Jenkins server is desirable in any environment. Both of these issues can be resolved by using the SMEE client, having things not addressable on the web, or locked down in some default way and not burn through API quotas in the polling method.

Smee is an open source project offered by GitHub that receives web-hook payloads (from Github) and sends them to listening clients(Jenkins).

Image description

USAGE
You’ll need a method for making localhost accessible to the internet if your application wants to respond to web-hooks. http://Smee.io is a small service that transmits web-hook payloads to your locally running application through the usage of Server-Sent Events.

SETUP
Step 1: Go to https://smee.io, you may create a new channel and acquire a special URL (which you should copy for later use) to send payloads to. The public website http://smee.io and the smee-client are the two parts of Smee that make it function. Through Server-Transmitted Events, a connection type that enables messages to be sent from a source to any clients listening, they communicate with one another.

Step 2: install the smee client where you have the Jenkins server running using the command:

npm install --global smee-client
Enter fullscreen mode Exit fullscreen mode

This will enable the smee client to accept and deliver webhooks.
Step3: Start the smee client now, then direct it to the Jenkins server. I’m currently running it on port 8080. (change both the port and the smee URL as needed)

smee --url https://smee.io/GSm1B40******SjYS --path /github-webhook/ --port 8080
Enter fullscreen mode Exit fullscreen mode

This says to connect to the smee service, and web-hooks should be forwarded to /github-webhook/ (the trailing slash is crucial; don’t forget it). You will notice that it is connected and forwarding web-hooks as soon as this is started. You can continue to receive web-hooks as long as you leave this command running.

You can also setup the service as a systemd service, by adding the smee.service file to /etc/systemd/system directory

[Unit]
Description=smee.io webhook delivery from GitHub
After=network.target
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=1
User=jenkins
ExecStart=smee -u https://smee.io/z*****BvuEt --path /generic-webhook-trigger/invoke --port 8080
[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

If you’re using github-webhook-trigger in your Jenkins then the ExecStart command should be;

ExecStart=smee -u https://smee.io/z*****BvuEt --path /generic-webhook-trigger/invoke --port 8080
Enter fullscreen mode Exit fullscreen mode

Step 4: Configure a pipeline that makes use of github from Jenkins. Make sure to check the ‘GitHub hook trigger for GITScm polling’ or the ‘Generic Webhook Trigger’ depending on your previous choice.

Image description
Next, specify a repository. This will prepare the server to accept webhooks from GitHub. (It’s also OK if you already have a pipeline configured that uses GitHub as the SCM source.)

Step 5: The last step is to instruct GitHub to notify Smee of webhook events for that repository.
Go to the GitHub repository’s settings tab, select “webhooks,” and then “add webhook.”

Image description
Next, configure the webhook: It should resemble the following:

  • Enter the “smee” URL that you copied in the previous step.
  • Choose application/json as the content type
  • Choose what events you want to trigger the web-hook
  • Press Add Web-hook (or update)

The links from jenkins and github will help to further understand how it works.

Top comments (0)