Hexo配置git远程部署

本文最后更新于 2024年6月24日 晚上

服务端配置

1. 创建一个用户,用于使用ssh访问git服务
1
sudo adduser git
2.添加公钥访问

将本地ssh使用的公钥追加到用户授权的公钥列表

1
echo "PUBLIC KEY" >> /home/git/.ssh/authorized_keys 
3.禁用新建用户的shell权限

出于安全考虑,我们需要禁用新建的git 用户的shell 登录权限。可以编辑 /etc/passwd 来实现,在 /etc/passwd 中找到类似下面的一行:

1
git:x:1001:1001:,,,:/home/git:/bin/bash

将其改为:

1
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
4.服务器端创建仓库并配置hooks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Ⅰ.创建裸仓库
git init --bare hexo.git
# Ⅱ.改变hexo.git目录的拥有者为新建的git用户
sudo chown -R git:git hexo.git
# Ⅲ.进入hooks文件夹
cd /path/to/hexo.git/hooks
# Ⅳ.新建post-receive文件,并添加下列内容
vim post-receive

/* post-receive文件内容 */
#!/bin/sh
# 将裸仓库内容恢复到work-tree所指定的目录
git --work-tree=/path/to/html/directory --git-dir=/path/to/blog.git checkout -f

# Ⅴ.赋予post-receive可执行权限
chmod +x post-receive
5.修改html目录权限
1
chmod -R go+w /path/to/html/directory
6.禁用密码登陆

修改配置文件/etc/ssh/sshd_config,需要注意,如果主账户没有配置ssh密钥,可能导致主账户无法登陆。

1
2
3
4
5
6
7
8
/* /etc/ssh/sshd_config */
# 启用密码验证
#PasswordAuthentication yes
# 禁用密码验证
PasswordAuthentication no

# 重启ssh服务使配置生效
sudo systemctl restart ssh.service

本地配置

修改 hexo 目录下的 _config.yml 文件,找到deploy部分,修改为:

1
2
3
4
deploy:
type: git
repo: git@SERVER_IP_ADDRESS:/path/to/hexo.git
branch: master

需要将SERVER_IP_ADDRESS替换为服务器的IP地址或者域名,同时将/path/to/hexo.git替换为服务器上新建的仓库路径。

一键部署

在完成上述操作之后,部署博客内容将变得非常简单。

1
2
3
4
# 更新public目录
hexo g
# 部署
hexo d

Hexo配置git远程部署
https://www.happyallday.cn/Hexo配置git远程部署/
作者
DevoutPrayer
发布于
2024年6月18日
更新于
2024年6月24日
许可协议