Netlify Lambda Function that listens to GitHub's push Webhook, verifying which sites/folders include file changes, and triggering Netlify's build hook only for those specific sites.
This is useful if you have a mono-repository with multiple sites, and would like to avoid unecessary builds and deploys being triggered for sites without changes.
To run the lambda function locally, here’s what you’ll need:
git clone https://github.com/ramigs/netlify-lambda-github-webhook-router
cd netlify-lambda-github-webhook-routernpm installCreate and edit a file .env to specify the sites - and respective Netlify endpoints- included in the mono-repository.
Each site is describe by two env variables:
- SITE_1_FOLDER, the relative repository path of site
1 - SITE_1_NETLIFY_ENDPOINT, the URL of Netlify's build hook for site
1
Check .env_sample to see an example.
npm run lambda-buildnpm run lambda-serveFunction will be listening at http://localhost:9000/webhookParser
You can send a POST request to http://localhost:9000/webhookParser , following the same
request format of GitHub's Webhook.
If you send the following HTTP request, and you have 11-Custom-Video-Player site/folder added to your env variables, as well as its respective Netlify endpoint, the build and deploy will be triggered.
{
"head_commit": {
"added": ["11-Custom-Video-Player/f1.txt", "f2.txt"],
"removed": ["f3.txt", "f4.txt"],
"modified": ["f5.txt", "f6.txt"]
}
}Create a new site on Netlify, and connect your function's repo to it.
Through Netlify's UI, create your environment variables in your function's site, following the same format of .env.
PS: If you make changes to variables, a new build and deploy is required to force variables to be reloaded.
Configure GitHub to send the push Webhook to the deployed function's Netlify endpoint.
Request disabling of automatic deploys for all your sites in the mono-repository, while still maintaining the repo linked to Netlify, as mentioned here.
This project is open source and available under the MIT License.