Skip to main content

跨repo部署博客到Github Pages

· 2 min read

时隔多年,我又开始搭建博客了,这次使用的是 Facebook 开源的Docusaurus

这次我将博客的源码和用于部署的产物分别放到两个仓库里,源码设置为私人仓库,然后使用 Github Action 来自动部署,但因为两份代码分属两个仓库,就用到了远程部署。

当时的 Docusaurus 中文文档还没有完全翻译完,这一块的 文档正好没有翻译,虽然我大致看明白了文档的意思,但因为一直没搞明白 ssh key,结果折腾了一下午。

后来我才恍然大悟,意识到 private key 和 public key 是不同的,我们用ssh-keygen -t ed25519 -C "your_email@example.com"会生成一对私钥和公开钥,原来那个以.pub结尾的就是 public key。

详细的文档可以看Docusaurus 官网文档,这里我将列出我的配置方案:

  1. 新建一个 ssh key
  2. 打开博客源码在 github 上的地址,找到Settings-Secrets-Actions,点击"New repository secret",添加刚刚创建的 private key,名称设置为GH_PAGES_DEPLOY
  3. 打开部署产物所在的仓库——yourname/yourname.github.io,找到Settings-Deploy keys,点击"Add deploy key",添加刚刚生成的 public key(扩展名为.pub
  4. 博客源码仓库根目录下创建.github/workflows/deploy.yml文件,使用以下配置
Github Action
.github/workflows/deploy.yml
name: Deploy to GitHub Pages

on:
pull_request:
branches: [master]
push:
branches: [master]

jobs:
test-deploy:
if: github.event_name != 'push'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 16.x
cache: yarn
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Test build website
run: yarn build
deploy:
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 16.x
cache: yarn
- uses: webfactory/ssh-agent@v0.5.0
with:
ssh-private-key: ${{ secrets.GH_PAGES_DEPLOY }}
- name: Deploy to GitHub Pages
env:
USE_SSH: true
GIT_USER: jhxxs
CURRENT_BRANCH: master
run: |
git config --global user.email "actions@github.com"
git config --global user.name "gh-actions"
yarn install --frozen-lockfile
yarn deploy