之前出过一期使用 Travis CI自动化部署hugo静态博客 ,好久没有更新博客了,今天发现 travis-ci 没有自动触发,查询之后发现 tranvis-ci 已经改变了收费规则 🙈 之前还能奏效的配置也都失效了,非付费用户需要寻找替代品了

前段时间也用过GitHub推出的Actions服务,引用其他开源的项目编译过路由器固件,感觉很方便,这次也研究下,用Actions来自动生成hugo博客静态网站

其实GitHub Actions里已经集成了一些常用的自动化配置,可以在仓库->Actions搜索框中搜索hugo

github_actions

点击后会引导用户在仓库目录下创建一个文件 opso-code.github.io/.github/workflows/hugo.yml,麻烦的是我之前markdown文件是放在source中,master只放生成的静态文件,Github Actions 只读取解析默认分支下 .github/workflows/*.yml(非默认分支下提交在Actions里没有解析),所以还是需要重命名,删掉 master,重命名 source -> master

$ git branch -m source master
$ git fetch origin
$ git branch -u origin/master master
$ git remote set-head origin -a

由于我的主题依赖的版本比较旧,需要指定hugo版本为0.74.3版本,其他都是默认

# Sample workflow for building and deploying a Hugo site to GitHub Pages
name: Deploy Hugo site to Pages

on:
  # Runs on pushes targeting the default branch
  push:
    branches: ["master"]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow one concurrent deployment
concurrency:
  group: "pages"
  cancel-in-progress: true

# Default to bash
defaults:
  run:
    shell: bash

jobs:
  # Build job
  build:
    runs-on: ubuntu-latest
    env:
      HUGO_VERSION: 0.74.3
    steps:
      - name: Install Hugo CLI
        run: |
          wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_Linux-64bit.deb \
          && sudo dpkg -i ${{ runner.temp }}/hugo.deb
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Setup Pages
        id: pages
        uses: actions/configure-pages@v1
      - name: Build with Hugo
        env:
          # For maximum backward compatibility with Hugo modules
          HUGO_ENVIRONMENT: production
          HUGO_ENV: production
        run: |
          hugo \
            --minify \
            --baseURL ${{ steps.pages.outputs.base_url }}
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v1
        with:
          path: ./public

  # Deployment job
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v1

提交后查看状态,静态文件会自动部署到 Pages服务中,而不是像以前,单独部署到gh-pages 等分支中

github_actions_pages

🚀 Nice GitHub !