Hexo webhook 自动部署

Hexo webhook 自动部署

Neurocoda

Hexo 是什么?

Hexo 是一个快速、简洁且高效的博客框架。 Hexo 使用 Markdown(或其他标记语言)解析文章,在几秒内,即可利用靓丽的主题生成静态网页。

本文对应的环境:

  • 一台公网服务器
  • 有 Git 平台托管 hexo 生成的站点(Github 或自建 Git 仓库)
  • 本地能运行 Hexo

若你没有公网服务器,那么可以忽略本文 服务器配置 部分,转而采用类似 Github page 等其他平台的服务。但是 本地配置 部分是相同。

本文采取的建站方案:

  • 本地编写文章,编写好后通过 hexo 构建站点,并将站点通过 git push 到 github 仓库。当 push 结束后, github 通过 webhook 向我的公网服务器发送提示,我的公网服务器收到提示后自动 clone 站点到 nginx 反向代理的站点目录。

    公网服务器端通过 neurocoda/gitrigger:latest 容器自动化完成接收 webhook 提示, git clone 工作。

Git 平台配置

创建一个代码仓库。

本地配置

安装 Nodejs

各种系统的安装方式详见:https://hexo.io/zh-cn/docs/

我本地是 Ubuntu:
根据: https://github.com/nodesource/distributions

1
2
3
curl -fsSL https://deb.nodesource.com/setup_23.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh
sudo apt-get install -y nodejs

验证安装:

1
node -v

安装 Hexo-cli

1
npm install -g hexo-cli

建立 Hexo 仓库

1
2
3
hexo init <folder>  
cd <folder>
npm install

配置 Git

创建 ssh key:

1
ssh-keygen

获取公钥:

1
cat /root/.ssh/id_ed25519.pub

将获取到的公钥添加到你选择的 git 平台

Github: https://github.com/settings/keys

Hexo 添加 Git

在 Hexo 根目录下:

安装插件:

1
npm install hexo-deployer-git --save
1
vim _config.yml

在底部找到,并添加仓库信息:

1
2
3
4
5
6
# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
type: git
repo: git@github.com:neurocoda/Hexo.git
branch: main

服务器配置

防火墙配置

开放防火墙:

1
2
3
ufw allow 22
ufw allow 80
ufw allow 443

启用并查看防火墙状态:

1
2
ufw enable
ufw status

安装 Nginx, Docker

更新源:

1
2
apt update
apt upgrade -y

安装 docker-compose, nginx, python3-certbot-nginx

1
apt install docker-compose nginx certbot python3-certbot-nginx

确定 nginx 已经正确运行了:

1
systemctl status nginx

配置 SSH 密钥

如果你的仓库是私有仓库,那么需要配置 ssh 密钥:

创建 ssh key:

1
ssh-keygen

获取公钥:

1
cat /root/.ssh/id_ed25519.pub

将获取到的公钥添加到你选择的 git 平台

Github: https://github.com/settings/keys

配置 Webhook

在仓库配置 webhook:

服务启动

编写 docker-compose.yml:

1
vim docker-compose.yml

填入以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
version: '3.8'

services:
hexo:
image: neurocoda/gitrigger:latest
container_name: hexo
restart: unless-stopped
ports:
- "127.0.0.1:7890:5000"
environment:
REPO_URL: "git@github.com:neurocoda/Hexo.git"
volumes:
- /root/.ssh/id_ed25519:/root/.ssh/id_rsa:ro
- /var/www/sites:/clone_data

docker-compose.yml 所在目录执行 up 指令:

1
docker-compose up -d

查看容器状态:

1
docker ps -a

状态为 Up 意为容器正确运行。(这里我还有其他服务,所以看起来可能不一样)

查看容器日志:

1
docker logs hexo

配置反向代理

编辑 nginx.conf:

1
vim /etc/nginx/nginx.conf

添加反向代理:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
worker_connections 768;
# multi_accept on;
}

http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# My Server
##

server {
server_name neurocoda.com;

root /var/www/sites/Hexo;
index index.html index.htm;

location / {
try_files $uri $uri/ =404;
}

location ~* \.(?:css|js|jpg|jpeg|png|gif|ico|svg|webp)$ {
try_files $uri =404;
expires 30d;
add_header Cache-Control "public";
}

error_page 404 /404.html;
location = /404.html {
root /var/www/sites/hexo;
}
}

server {
listen 80;
server_name webhook.neurocoda.com;

location / {
proxy_pass http://127.0.0.1:7890;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##
gzip on;

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

# mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
# }

修改好配置之后检查语法是否错误:

1
nginx -t

重载 nginx 配置:

1
systemctl reload nginx

确定已正确配置 DNS 记录:

配置 SSL

配置 SSL:

1
certbot --nginx

按照要求填入信息

检查 certbot 修改后的 nginx.conf 语法是否正确:

1
nginx -t

重载 nginx 配置

1
systemctl reload nginx

同步测试

在你的本地的 Hexo 的目录:

1
hexo clean & hexo g & hexo d

等待执行完成后访问你的服务器,不出意外的话,网页已经展示出来了。

常见问题

权限问题

1
2
chmod 755 /var/www/sites/Hexo
chown -R www-data:www-data /var/www/sites/Hexo
  • Title: Hexo webhook 自动部署
  • Author: Neurocoda
  • Created at : 2025-02-13 17:01:33
  • Updated at : 2025-02-13 17:01:33
  • Link: https://neurocoda.com/p/2cc1110b.html
  • License: This work is licensed under CC BY-ND 4.0.
Comments