Hexo使用Github Action自动部署

Hexo使用Github Action自动部署

Hexo 配置

yaml
# _config.yml 中的 deploy 配置,指定静态站要推送到哪里
deploy:
  type: git
  repo: "git@github.com:simonkimi/simonkimi.github.io.git"   # Pages 仓库的 SSH 地址
  branch: master   # 推送到的分支,GitHub Pages 默认从 master/main 发布

需要在 Page 的仓库中配置 Deploy Key,用于 Github Action 自动部署。

secrets.DEPLOY_KEY 为私钥。

Github Action 配置

yaml
# 工作流名称,在 Actions 页面中显示
name: 自动部署文章

# 触发条件:何时运行此工作流
on:
  push:
    branches:
      - pub   # 仅当推送到 pub 分支时自动触发
  workflow_dispatch:   # 允许在 GitHub 网页上手动点击运行

jobs:
  deploy:
    runs-on: ubuntu-latest   # 使用 GitHub 提供的 Ubuntu 虚拟机
    steps:
      # 拉取当前仓库代码(含子模块和 LFS 大文件)
      - name: Checkout
        uses: actions/checkout@v6
        with:
          submodules: "recursive"
          lfs: true

      # 安装 pnpm 包管理器
      - name: Setup pnpm
        uses: pnpm/action-setup@v4.2.0

      # 安装 Node.js,并启用 pnpm 缓存以加速后续构建
      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: 'pnpm'

      # 安装项目依赖(--frozen-lockfile 保证与 lock 文件一致,避免版本漂移)
      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      # 配置 Git 用户信息,部署时提交到 Pages 仓库会用到
      - name: Configure Git
        run: |
          git config --global user.email "git@z31.mozmail.ink"
          git config --global user.name "simonkimi"

      # 加载 DEPLOY_KEY 私钥到 ssh-agent,用于推送到 Pages 仓库
      - name: Setup SSH for deploy
        uses: webfactory/ssh-agent@v0.5.3
        with:
          ssh-private-key: ${{ secrets.DEPLOY_KEY }}

      # 执行 Hexo 的 deploy 命令,生成静态站并推送到配置的 Git 仓库
      - name: Deploy
        run: pnpm run deploy


Hexo使用Github Action自动部署
https://simonkimi.githubio.io/2022/01/30/Hexo使用Github-Action自动部署/
作者
simonkimi
发布于
2022年1月30日
许可协议