Skip to main content

Using Github's Webhook to Automate Blog Deployment

· 2 min read
MuelNova
Pwner who wants to write codes.

I suddenly got tired of having to log in via SSH and then run git pull && npm run build to deploy my blog every time, so I thought of using a webhook.

However, I still have to fix the package-lock.json conflicts myself. I'll think of a way to deal with this later (just ignore it!).

Webhook Configuration

About webhooks - GitHub Docs

Just read this and you're done. Add a new webhook at REPO->Settings->Webhooks->Add webhook.

image-20230511190219973

image-20230511190315524

For the URL, just enter your public IP address. I chose JSON for the content type.

Note the Secret. This is the key used to generate the HMAC, so try to make it as random as possible. The backend will need it later.

After you succeed, it will send a ping, so you can hold off on completing the addition for now.

Backend

The backend logic is simple: listen on the URL above, verify the POST request, and then run the commands.

I'm using Go's Gin, so it's just a matter of adding it.

You can see the code here: MuelNova/go-github-build-hook.

The only thing worth noting is that the build takes a long time, so I just throw it into a goroutine to run on its own, and the backend returns 200 immediately.

When I was testing it at first, it kept timing out, and I thought there was some problem.

Yes, this post is pure filler, just because I wanted to test its effect (lol).

Ideas

Since the information provided by GitHub is quite comprehensive, I'll probably make it a bit more customizable later. Or maybe add a push notification to a QQ bot to let me know when the build is complete.

info

This Content is generated by LLM and might be wrong / incomplete, refer to Chinese version if you find something wrong.

Loading Comments...