每次更新文章都要生成一次就太麻烦啦!
于是利用 GitHub 的 Webhooks 做一个自动化部署
大概说一下搭建的经过~
Runo+ Blog
GitHub Webhooks
安装以下几个包
- express 服务端
- node-cmd 用于执行命令
- crypto 校验 sha1(可选)
- dotenv 加载.env(可选)
1
| npm install express node-cmd crypto dotenv --save
|
在 hexo 项目下新建一个 webhooks.js
hmmm 萌新代码写的不好 见谅
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| const express = require('express'); const app = express(); const nodeCmd = require('node-cmd'); const crypto = require('crypto'); require('dotenv').config();
app.use(express.json());
app.post('/apis/update', (req, res) => { const sign = req.headers['x-hub-signature']; const sha1 = 'sha1=' + crypto .createHmac('sha1', process.env.GITHUB_WEBHOOKS_SECRET) .update(JSON.stringify(req.body)) .digest() .toString('hex'); if (!sign || sign !== sha1) return res.send('secret error'); nodeCmd.get('git pull', (err, data) => { if (!err) { console.log(data); nodeCmd.get('npx hexo generate', (err, data) => { if (err) console.error(err); else console.log(data); }); } else console.error(err); }); res.send('success'); });
app.listen(process.env.GITHUB_WEBHOOKS_PORT, () => console.log('listening ' + process.env.GITHUB_WEBHOOKS_PORT) );
|
进行服务器的部署
将代码提交后, 在服务器 git clone
执行 npm run build
会在 public 文件夹生成静态网页, 再使用 nginx 等web服务器, 把域名指向 public 文件夹
使用 pm2 start
启动 webhooks.js, 服务端就算是部署好啦!
为仓库添加 Webhook
打开 GitHub 仓库
依次点击 Setting → Webhooks → Add webhook
设置 Webhook
Payload URL: 需要响应的地址 (例如 https://runo.plus/xxx)
Content type: 请求的内容类型 (选择 application/json)
Secret: 秘钥 (需要与服务器上的一样! GITHUB_WEBHOOKS_SECRET, 如果不需要校验则省略)
选择 Just the push event. (仓库接到推送后触发)
勾选 Active
点击 Add webhook 完成添加
提交一次仓库试试效果owo~