Automatic Blog Deployment Using Github Webhook
Feeling tired of typing ssh
, git pull
, and npm run build
every time to deploy your blog? Well, I thought about using a webhook.
But still need to manually resolve the conflicts in package-lock.json
, wondering if there's a way to ignore it (just ignore this thing!)
Webhook Configuration
Just take a look at this, go to REPO->Settings->Webhooks->Add webhook to add a new webhook.
Simply fill in the URL with the public IP, I chose json as content type.
Remember the Secret, this is used to generate HMAC key, try to make it random, the backend will need it later.
After successful setup, it will send a ping, so you can leave it unfinished for now.
Backend
The backend logic is simple, listen to the URL above, verify when receiving a POST request, and then run the command.
Using go's gin, just add it and you're done.
You can find the code at MuelNova/go-github-build-hook here.
The only thing worth noting is that because the build time is too long, I let it run in a goroutine, and the backend directly returns 200.
At the beginning, when I finished writing the test, it kept timing out, I thought there was a problem.
Yes, this article is just for fun because I want to test its effect (laugh)
Ideas
Because GitHub provides comprehensive messages, I might make it more customizable in the future? Or maybe add a notification to a QQ bot to let me know when the compilation is done.
This Content is generated by ChatGPT and might be wrong / incomplete, refer to Chinese version if you find something wrong.