Using GitHub Webhooks to Purge Cache
Let’s say you’re working on a web project and are making commits through GitHub that will update its look or functionality. If you’re using a CDN such as KeyCDN, then you’ll need to either purge the assets you’ve updated or purge the entire Zone so that all visitors will receive the latest asset changes. This can be a slightly cumbersome process if you’re required to log in to your KeyCDN dashboard every time you need to purge an asset URL or Zone.
However, with GitHub Webhooks, you can instantly purge an entire Zone upon committing a change. This makes the purging process much more streamlined. What’s more, this process is easy to setup and in this guide, we’ll cover how to do just that.
Purging KeyCDN Zones with GitHub Webhooks
Properly configuring a GitHub webhook is a simple task. Follow the steps below to implement this in your own workflow.
- Go to your repository’s Settings page, then click on Webhooks
Select “Add Webhook” and configure the Payload URL to resemble the following (remember to add your KeyCDN API key and the Zone ID that you want to purge):
https://<API key>@api.keycdn.com/zones/purge/<zone id>.json
application/jsonas the Content Type
After committing a change to your repo, check that the Webhook ran successfully by navigating to the Webhook you just created and scrolling to the bottom of the page to “Recent Deliveries”.
Expand one of the recent deliveries and check the Response to ensure that the KeyCDN purge completed successfully.
Once your webhook is implemented, you’ll be able to automatically purge a particular Zone whenever you commit a change to your GitHub repository. If ever you need to change the Zone being purged, simply edit the Webhook to include the new Zone ID or remove the Webhook if it is no longer needed. For more information about purging options with KeyCDN, check out our complete purge CDN cache article.