Github action 部署博客(vitepress)

双仓库方案:

source 仓库: 存放源码和.md 文件(随着该仓库的更新,会自动触发 action 构建到.io 仓库)

username.github.io 仓库: 存放构建后的静态文件

GitHub Action 配置

  1. 在 source 仓库创建 workflow 文件:
  2. 设置部署密钥:
    • 生成 SSH 密钥对
    • 私钥添加到 source 仓库的 Secrets
    • 公钥添加到部署仓库的 Deploy Keys

详细指南

  1. 在 source 仓库
1
ssh-keygen -t rsa -b 4096 -C "your-email@example.com" -f deploy_key
  1. 配置仓库密钥:

在 Source 仓库:

去 Settings -> Secrets and variables -> Actions -> New repository secret
添加名为 DEPLOY_KEY 的 secret,内容是 deploy_key(私钥里面完整内容)

在 .io 仓库:

去 Settings -> Deploy keys -> Add deploy key
添加名为 DEPLOY_KEY 的 secret,内容是 deploy_key.pub(公钥里面完整内容)
勾选 “Allow write access”

  1. 在 Source 仓库创建 deploy.yml 文件

示例:

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
name: Deploy VitePress site to Pages

on:
push:
branches: [main]
workflow_dispatch:

jobs:
build-and-deploy:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: pnpm/action-setup@v3
with:
version: 9

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm

- name: Install dependencies
run: |
pnpm add -D vitepress
pnpm install

- name: Build with VitePress
run: pnpm docs:build

- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
deploy_key: ${{ secrets.DEPLOY_KEY }}
external_repository: ZCST-OpenCS/ZCST-OpenCS.github.io
publish_branch: main
publish_dir: docs/.vitepress/dist

GitHub Actions 流程

  1. 拉取代码
  2. 安装依赖
  3. 构建站点
  4. 自动部署到 github.io 仓库