首先安装git拉取我们的代码

1
yum update -y && yum install -y git

然后安装nginx

安装Nginx

安装nodeJS

下载安装包

1
wget https://nodejs.org/dist/v12.16.0/node-v12.16.0-linux-x64.tar.xz

解压

1
tar -xvf node-v12.16.0-linux-x64.tar.xz

修改配置文件etc/profile PATH=/opt/nodejs/bin:$PATH

1
2
3
4
5
6
7
8
9
10
**#你的node下载解压到路径 比如我刚刚解压到了 /opt/node/node-v12.16.0-linux-x64 那路径就是 PATH=/opt/node/node-v12.16.0-linux-x64/bin:$PATH**

echo "**PATH=/opt/node/node-v12.16.0-linux-x64/bin:$PATH**" >>/etc/profile

#刷新环境变量
source /etc/profile

#成功打印
[root@VM-4-3-centos bin]# node -v
v12.16.0

安装pm2

1
2
3
4
5
npm install pm2 -g

# 查看PM2版本
[root@VM-4-3-centos node-v12.16.0-linux-x64]# pm2 -v
5.1.2

安装完成之后准备三个文件

webhook.js 监听webhook 请求,这个请求是由GitHub配置webhook 请求我们的nginx,并且由nginx转发给我们

查看webhook.js

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
40
41
var http = require('http')
var createHandler = require('github-webhook-handler')
// path 是由nginx反向代理指定给我们的 nginx配置如下
// 监听webhook请求
// location /onpush {
// proxy_pass http://127.0.0.1:15535/onpush;
// }
// secret 是github上设置的 加密字符
var handler = createHandler({ path: '/onpush', secret: 'XmkzRV8gHgzv33-' })

function run_cmd(cmd, args, callback) {
var spawn = require('child_process').spawn;
var child = spawn(cmd, args);
var resp = "";

child.stdout.on('data', function (buffer) { resp += buffer.toString(); });
child.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
child.stdout.on('end', function () { callback(resp) });
}

http.createServer(function (req, res) {

handler(req, res, function (err) {
res.statusCode = 404
res.end('no such location')
})
}).listen(15535, () => { // 监听的端口,对应我们的nginx反向代理的端口
console.log('WebHooks Listern at 15535');
})
handler.on('push', function (event) {
console.log('Received a push event for %s to %s',
event.payload.repository.name,
event.payload.ref);
// 判断是否为 main分支,也可以自行修改
if (event.payload.ref === 'refs/heads/master') {
console.log('deploy master..')
run_cmd('sh', ['./deploy.sh'], function (text) { console.log(text) });
}
})

package.json webhook的依赖

查看package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"name": "webhook",
"version": "1.0.0",
"main": "webhooks.js",
"scripts": {
"start": "node webhooks.js"
},
"license": "ISC",
"dependencies": {
"child-process": "^1.0.2",
"github-webhook-handler": "^1.0.0",
"http": "^0.0.1-security"
}
}

执行拉去代码的shell脚本

查看deploy.sh

1
2
3
4
5
6
# 进入部署上线项目的文件夹
cd /www/ryder.pw/fuguiliu.github.io;
# 进行拉取操作
git pull --rebase origin master;
# 如必要还需让项目重启
# 我是采用pm2 restart some_id的方式

再package.json 路径下初始化环境

1
npm i

启动守护进程监听webhook请求

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
# 启动脚本
pm2 start wobhook.js

[root@beautiful-bytes-2 NodeWebhooks]# pm2 start webhook.js
[PM2] Applying action restartProcessId on app [webhook](ids: 0)
[PM2] [webhook](0) ✓
[PM2] Process successfully started
┌─────┬────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ webhook │ default │ 1.0.0 │ fork │ 16548 │ 0s │ 3 │ online │ 0% │ 8.3mb │ root │ disabled │
└─────┴────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

#查看启动日志
pm2 logs

0|webhook |
0|webhook | Received a push event for fuguiliu.github.io to refs/heads/master
0|webhook | deploy master..
0|webhook | stderr: Cannot pull with rebase: Your index contains uncommitted changes.
0|webhook | Please commit or stash them.
0|webhook |
0|webhook |
0|webhook | Received a push event for fuguiliu.github.io to refs/heads/master
0|webhook | deploy master..
0|webhook |
0|webhook | stderr: shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
0|webhook | sh: ./deploy.sh: No such file or directory
0|webhook |
0|webhook | WebHooks Listern at 15535
0|webhook | WebHooks Listern at 15535

最后再本地push一下你的代码

看一下PM2日志