前情提要

由于在 Obsidian 中,虽然我有不被 git 追踪的文件夹(私有文件夹),但是这也限制了我多设备同步 的需求(因为 Windows 上的 iCloud 十分难用)

因此,我们期望工作流程如下:

  1. 在私有仓库中更新博客
  2. 在公开仓库中拉取博客内容并自动构建

本质上,我们将公开仓库作为了一个博客生成器,专门用来触发 CI

GitHub 与本地设置

GitHub

首先,需要申请一个 个人 Token,名字自由命名,例如 BLOGS,但是记得复制这个 Token,例如 <TOKEN>

然后,进入到博客的仓库,例如 <your-name>.github.io,在这个仓库的 Setting -> Secrets and variables -> Actions 中,添加一个 secret,把先前的 <TOKEN> 粘贴进去,命名为 BLOGS

本地设置

本地的设置十分简单,我们在当前博客目录下,使用以下命令:

git submodule add <your-private-repo> content/

Tip

如果之前这个目录已经是一个 git 仓库,可以考虑把 .git 删了,重新建立一个仓库,然后 git push -f 上传

之后,我们在 <your-private-repo> 这个仓库中进行博客内容的创作与同步

Important

注意设置 Quartz 中发布文章的配置,防止把原来不想发布的也发布了,可以考虑 publish 而不是 draft

接着,我们需要更改我们的 CI 文件,例如 .github/workflows/deploy.yml

name: Deploy Quartz site to GitHub Pages
 
on:
  push:
    branches:
      - v4
 
permissions:
  contents: read
  pages: write
  id-token: write
 
concurrency:
  group: "pages"
  cancel-in-progress: false
 
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0 # Fetch all history for git info
          token: ${{ secrets.BLOGS }}
          submodules: recursive
	  # .....

最后,我们只需要推送原来博客的本地仓库到 GitHub 即可触发这个 CI,自动去博客内容的仓库拉取,然后部署更新

工具

为了方便,也可以使用下面这个脚本,然后通过 cron 来设置定时任务

Tip

注意修改路径

#!/usr/bin/env bash
source /etc/profile
 
# Push this repo to github
cd /path/to/blog/content/folder
git add .
git commit -am "add: new blogs(weekly auto push)"
git pull
git push
 
# Push website repo and build quartz
 
cd /path/to/my/blog/website/folder
git pull --recurse-submodules
git submodule update --remote
git add .
git commit -am "sync with blog content"
git push

然后,我们通过 cron 命令:

crontab -e

来书写一个 bash 命令,定时执行上面的脚本即可,可以参考 crontab 时间设置 来看看自己的语法有没有出错