前言
我使用hugo搭建博客,记录一些技术、经验、踩坑,已有567天,博客部署在vercel,使用的是官方的cname:cname.vercel-dns.com,访问速度不佳,
鉴于手头上小鸡多,何不把博客同步到vps,这样当本地写完文章,推送到Github的时候,同时也同步到了小鸡上。
教程
原理:使用 rsync,配置 Actions 脚本同步
1.安装 rsync
登录远程服务器,键入以下命令来完成 rsync 的安装:
sudo apt install rsync
2.新建 rsync 用户
sudo adduser rsyncer
创建用户的途中会要求设置用户密码,其它配置使用默认值直接回车就行。
然后会在/home目录生成一个rsyncer
文件夹,记住这个文件夹。
3.本地生成 SSH key
然后在你的电脑上生成一个 Key 对,其中一个扩展名是.pub,是公钥。假设分别命名为:id_rsa
和 id_rsa.pub
ssh-keygen -q -t rsa -b 4096 -C "阿弥托福" -N "" -f ~/.ssh/id_rsa
打开 id_rsa.pub, 复制里面的内容,稍后需要用到,
4.将公钥添加到vps 登录你的服务器,逐个输入以下代码
su rsyncer # 要输入密码
cd ~
mkdir -p www .ssh
chmod 700 .ssh
touch .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
vim .ssh/authorized_keys # 用文本编辑器打开 `id_rsa.pub`,复制所有内容到这个文件里
上面一连串代码的目的是把公钥内容复制到authorized_keys文件里,
或者你可以手动复制公钥到/home/rsyncer/.ssh/authorized_keys,前提是新建authorized_keys文件
cd /home/rsyncer
mkdir -p www .ssh
chmod 700 .ssh
touch .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
5.设置 Github仓库(重点)
打开仓库设置,按下图把相关设置加入 secrets,
地址:https://github.com/ 用户名 / 仓库名 /settings/secrets/actions
解读:
HONGKONG_PRIVATE_KEY :你电脑上的id_rsa私钥内容
HONGKONG_SERVER_IP : 你的服务器IP地址
HONGKONG_USERNAME :rsyncer #用户名就是前面新建的rsyncer用户
HONGKONG_WWW_PATH :www #就是服务器同步的目录为www,保持不变
6.修改自动化 Actions 脚本
在仓库 .github\workflows\main.yml 添加以下内容到末尾:
- name: webfactory/ssh-agent
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.HONGKONG_PRIVATE_KEY }}
- name: Publish blog to remote vps
run: |
rsync -av -e "ssh -o StrictHostKeyChecking=no -p 5522" --delete --exclude={'.git/','.github/','.user.ini'} public/ ${{ secrets.HONGKONG_USERNAME }}@${{ secrets.HONGKONG_SERVER_IP }}:${{ secrets.HONGKONG_WWW_PATH }}
- 注意:
-p 5522
是服务器的 ssh 端口,记得修改成你自己的。其他保持不变
最后就是推送测试了,博客静态文件会同步到/home/rsyncer/www
文件夹下,按照这套流程下来,应该没有问题了。然后新建站点,目录指向/home/rsyncer/www即可。
我自己的博客main.yml:
name: Auto Deploy hugo
on:
push:
branches:
- main # 更新触发的分支
jobs:
build-deploy:
runs-on: ubuntu-22.04
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE20: true # ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true
steps:
- name: Check out repository code
uses: actions/checkout@v4
with:
submodules: recursive # Fetch Hugo themes (true OR recursive)
fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod
- name: Setup hugo
uses: peaceiris/actions-hugo@v3
with:
hugo-version: "0.127.0"
extended: true # 设置是否需要 extended 版本
- name: Cache resources # 缓存 resource 文件加快生成速度
uses: actions/cache@v4
with:
path: resources
key: ${{ runner.os }}-hugocache-${{ hashFiles('content/**/*') }}
restore-keys: ${{ runner.os }}-hugocache-
- name: Build Hugo static files # 部署静态资源
run: hugo --gc --minify
- name: Deploy to Github Pages # 部署到Github Pages页面
uses: peaceiris/actions-gh-pages@v4
with:
personal_token: ${{ secrets.PERSONAL_TOKEN }} # 两个仓库请使用 deploy_key
external_repository: woniu336/woniu336.github.io # 如果在同一个仓库请注释
publish_dir: ./public # hugo 生成到 public 作为跟目录
publish_branch: main # Github Pages 所在分支
commit_message: ${{ github.event.head_commit.message }}
- name: webfactory/ssh-agent
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.HONGKONG_PRIVATE_KEY }}
- name: Publish blog to remote vps
run: |
rsync -av -e "ssh -o StrictHostKeyChecking=no -p 5522" --delete --exclude={'.git/','.github/','.user.ini'} public/ ${{ secrets.HONGKONG_USERNAME }}@${{ secrets.HONGKONG_SERVER_IP }}:${{ secrets.HONGKONG_WWW_PATH }}