全自动部署 Hexo 到 github

有一天把 hexo 配置好了,但是每次更新后还有手动部署太麻烦。正好有个 pm2 的工具,完全可以代替我们做这些事。

安装 pm2

1
npm install -g pm2

在博客 source 目录新建 start.json

1
2
3
4
5
6
var process = require('child_process');
process.exec(' hexo g -d', function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}
});

新建 watch.json

1
2
3
4
5
6
7
8
9
{
"apps" : [{
"name" : "blog",
"script" : "./start.js",
"exec_interpreter": "node",
"exec_mode" : "fork_mode",
"watch" : "_posts"
}]
}

使用 pm2 命令实现监控文件变动自动提交

1
pm2 start watch.json

pm2 常用命令

1
2
3
4
pm2 list
pm2 logs
pm2 start watch.json
pm2 show <id|name>

这样就会发现 Hexo 已经被自动部署到 github 啦,而且时刻保持最新版