gh_pages 使用/部署指南

一个 github 提供的 pages 托管服务,方便展示静态内容。

01 添加 .github ci 文件

创建文件如下

# 跳到项目目录,创建目录
mkdir .github/workflows/
# 创建文件
touch .github/workflows/deploy.yml

02 示例配置

一个目前在运行的示例配置

# AI: https://chat.qwen.ai/c/25e9d10b-e4dc-4cc8-be73-50fbf7ae862b

name: Deploy Dumi Site to GitHub Pages

on:
  push:
    branches:
      - main  # 根据你的主分支名调整,如 main / master
  workflow_dispatch:  # 允许手动触发

permissions:
  contents: write  # 必须授权写权限,否则 GITHUB_TOKEN 无法推送

jobs:
  deploy:
    runs-on: ubuntu-latest
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22'  # dumi 推荐 Node >=18

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 9

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - name: Setup pnpm cache
        uses: actions/cache@v4
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Build Dumi site
        run: pnpm build

      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./dist  # dumi 默认输出目录

03 关于 GITHUB_TOKEN

关于这个变量 GITHUB_TOKEN 实际上是无法设置到 github actions 的变量里的

而且,这个不用设置, github 执行 actions 的时候,会自动设置这个

04 github 的 gh_pages 要打开

配置如下图

dumi docs github pages