前言

Action自动化部署原本是给Hugo准备的,一直没成功,直到我搭好了Hexo自动化,再用Hexo的步骤来搭建Hugo然后成功了… …

该教程仅对clone主题有效,npm下载主题无效

该教程与 Hexo -Action自动化部署(无指令) 大同小异 ,说白了还是看 自动化的源码

原教程源自 wolanx 大佬


教程

准备工作

  1. 确保在配置过程中能访问Github ,无法访问看下面教程

  2. 下载 Github Desktop ,并 登录 好

  3. 备份好本地源码! 备份好本地源码! 备份好本地源码!

  4. 在博客 根目录 创建文件夹 命名 .github ,在 .github 文件夹下再 创建 一个文件夹 命名 workflows

    1
    根目录/.github/workflows

自动化部署

备份好本地源码!!!

  1. 把 博客仓库 clone 到 本地 ,记录好 本地路径

  2. 找到 Current branch ,输入myblog ,点击 New branch ,再点击 Create branch

  3. 等待创建分支,点击 Publish branch 推送到远端 PS: 仓库就会生成一个名为myblog的新分支,新分支会复制旧分支的内容

  4. 回到 Github Desktop , 查看 Current branch 是否为 myblog , 否 则切换到 myblog

  5. 在准备工作 4 中 workflows 文件夹下 新建文件命名 pipeline.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
    42
    43
    44
    45
    name: Action

    on:
    push:
    tags:
    - '*'
    branches:
    - myblog

    env:
    REGISTRY: ghcr.io
    IMAGE_NAME: ${{ github.repository }}

    jobs:
    build:
    runs-on: ubuntu-latest
    # permissions:
    # contents: read
    # packages: write
    concurrency:
    group: ${{ github.workflow }}-${{ github.ref }}

    steps:
    - name: checkout
    uses: actions/checkout@v2
    with:
    submodules: true # Fetch Hugo themes (true OR recursive)
    fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod

    - name: setup hugo
    uses: peaceiris/actions-hugo@v2
    with:
    hugo-version: '0.92.0'
    extended: true

    - name: build
    run: hugo --minify

    - name: deploy
    uses: peaceiris/actions-gh-pages@v3
    if: ${{ github.ref == 'refs/heads/myblog' }}
    with:
    github_token: ${{ secrets.GITHUB_TOKEN }}
    publish_dir: ./public
    cname: 域名 # 重点 !!!
  6. 把克隆源码删除 ,再把本地源码复制进入 注意:把 根目录下 public 文件夹里的 .git 删掉,一定要删掉!不然无法推远端

  7. 回到 Github Desktop , Summary 随便填 , 点击Commit to myblog

  8. 回到 Github仓库 , 等待 Actions 部署 ,即可


常见问题

  1. 错误代码 Process completed with exit code 128.
    解决方法: 仓库 => Settings => Actions => General => Workflow permissions 选择第一个 , 点击 Save ,重新部署即可
  2. 错误代码 Process completed with exit code 2. / 1.
    解决方法: 检查代码是否存在格式问题(一般都是格式问题)

补充

Hugo自动化部署后会在仓库生成一个新的分支gh-pages,切记不要删除 。

如果你是托管到Vercel,记得在Vercel更改推送分支为gh-pages (项目 => Settings => Git => Production Branch) 。