Skip to content
Inspire - Capo Productions

使用 GitHub Action、GitHub Pages 部署静态文件

打算使用 VuePress 搭建个人博客,想要每当提交代码到 GitHub 仓库时,通过 GitHub Action 工作流,自动更新 GitHub Pages 为最新内容。工作流配置如下:

.github/workflows/deploy-docs.yml

yml
name: Deploy docs

on:
  # 每当 push 到远程 master 分支时触发 Github Action
  push:
    branches:
      - master

permissions:
  contents: write

jobs:
  deploy-docs:
    # 暂时禁用该 job
    if: ${{ false }}
    # 使用环境
    runs-on: ubuntu-latest

    steps:
      # 将代码拉取到虚拟机
      - uses: actions/checkout@v3
        with:
          # “最近更新时间” 等 git 日志相关信息,需要拉取全部提交记录
          fetch-depth: 0

      
      # 安装 pnpm
      - name: Setup pnpm
        uses: pnpm/action-setup@v2
        with:
          version: 8
          # 使用 pnpm install 安装依赖
          run_install: true

      # 安装 Node.js
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 18
          # 用于指定用于缓存的包管理器,应该先安装包管理器
          cache: pnpm

      # 运行构建脚本
      - name: Build VuePress site
        run: pnpm build

      # 部署到 GitHub Pages
      - name: Deploy to GitHub Pages
        uses: crazy-max/ghaction-github-pages@v2
        with:
          # 部署到 gh-pages 分支
          target_branch: gh-pages
          # 部署目录为 VuePress 的默认输出目录
          build_dir: src/.vuepress/dist
        env:
          # @see https://docs.github.com/cn/actions/reference/authentication-in-a-workflow#about-the-github_token-secret
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

除了上面的 crazy-max/ghaction-github-pagespeaceiris/actions-gh-pages 也可以实现将代码部署到GitHub Pages。

当执行上述工作流报如下错误时:

/usr/bin/git push origin gh-pages
remote: Write access to repository not granted.
fatal: unable to access 'https://github.com/username/repository.git/': The requested URL returned error: 403
Error: Action failed with "The process '/usr/bin/git' failed with exit code 128"

有两种方式解决。

方式一:

修改工作流配置文件,向 workflow 或 job 的 permissions.contents 添加写权限:

yml
permissions:
  contents: write

方式二:

配置 GITHUB_TOKEN,使其具有读写权限:

项目 Settings -> Actions -> General -> Workflow permissions -> 选择 Read and write permissions

Preview

上面的工作流配置使用了方式一。

布局切换

调整 VitePress 的布局样式,以适配不同的阅读习惯和屏幕环境。

全部展开
使侧边栏和内容区域占据整个屏幕的全部宽度。
全部展开,但侧边栏宽度可调
侧边栏宽度可调,但内容区域宽度不变,调整后的侧边栏将可以占据整个屏幕的最大宽度。
全部展开,且侧边栏和内容区域宽度均可调
侧边栏宽度可调,但内容区域宽度不变,调整后的侧边栏将可以占据整个屏幕的最大宽度。
原始宽度
原始的 VitePress 默认布局宽度

页面最大宽度

调整 VitePress 布局中页面的宽度,以适配不同的阅读习惯和屏幕环境。

调整页面最大宽度
一个可调整的滑块,用于选择和自定义页面最大宽度。

内容最大宽度

调整 VitePress 布局中内容区域的宽度,以适配不同的阅读习惯和屏幕环境。

调整内容最大宽度
一个可调整的滑块,用于选择和自定义内容最大宽度。

聚光灯

支持在正文中高亮当前鼠标悬停的行和元素,以优化阅读和专注困难的用户的阅读体验。

ON开启
开启聚光灯。
OFF关闭
关闭聚光灯。