使用 GitHub Action 自动构建 Hexo 博客

Step 1

把 Hexo 代码上传到 GitHub 仓库

Step 2

.github/workflows 中创建一个 build.yml 文件

Step 3

将以下代码复制到刚刚创建的文件中

# https://xyz8848.com/archives/11
name: Build Hexo

on:
  push:
    branches:
      - main # Hexo 所在分支
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@master

      - name: Setup Node.js
        uses: actions/setup-node@master
        with:
          node-version: "20"

      - name: Cache NPM Dependencies
        uses: actions/cache@v4
        with:
          path: node_modules
          key: ${{ runner.OS }}-npm-cache
          restore-keys: |
            ${{ runner.OS }}-npm-cache

      - name: Install Dependencies
        run: npm install

      - name: Build Hexo
        run: npm run build

      - name: Upload Pages
        uses: crazy-max/ghaction-github-pages@v2
        with:
          # 上传的分支
          target_branch: gh-pages
          # Hexo 的输出目录
          build_dir: public
          # 自定义域名(CNAME)(可选)
          fqdn:
          # true = 创建增量提交;false = 执行强制推送
          keep_history: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Step 4

更改 build.yml 中的:

  • Hexo 所在分支
作者:xyz8848
本文链接:https://xyz8848.com/archives/11
转载请注明出处
上一篇
下一篇