Automatic blog updates with Git Webhooks
I was executing cURL requests to download release data from Github, manually saving images to the project, and committing the result to the repository. This manual labor quickly became annoying, especially because it takes a while before changes go to production.
So I asked AI to create a script that does the work for me, that can run in NodeJS. It took a few tries before it understood the problem with private repositories: you have to download the full json+html version (Accept: 'application/vnd.github.full+json') to get the proper image links.
I made it so the script runs at startup. But I don't want to restart the frontend every time I change a release.
Add Webhook
In Github, you can define webhooks for a variety of things. I created one for releases, and pointed it at https://photopaste.com/webhooks/github-release. This means Github sends a POST request to the specified URL whenever something changes in releases.
In server.ts, I made an endpoint with the same path:
//server.ts
app.use('/webhooks', webhooksRouter);
//webhooks.ts
const webhooksRouter = Router();
webhooksRouter.post('/github-release', async (req, res) => {
try {
const stats = await fetchReleases();
res.status(200).send(`Releases downloaded: ${stats.releases.length}, images downloaded: ${stats.filesSaved.length}`);
} catch (err) {
console.error(err);
res.status(500).send('Failed to update github-releases.json + media');
}
return res;
});
This is a simplified version. The real version also checks headers for security so only Github can trigger the webhook.
Now whenever I change something in my Github releases, it automatically updates the blog.
Read more articles
- Moving towards Cloud storage
- Add event and user statistics
- Refactor Infrastructure
- Add QR code to mobile
- Video support and Scrolling Menu
- Automatic blog updates with Git Webhooks
- Add sliding sidebar menu to mobile home page
- I've been doing WebFlux wrong
- Implement Blog
- Migrating event urls
- Interesting bug
- Dynamic social media badges
- Improving Games and Teams
- Blue Green deployments
- Why I left the big cloud
- Moving from AWS Amplify to Google Firebase
- First version