Automatic System Switchover

Automatically deploy a Hugo blog to GitHub Pages and Gitee using GitHub Actions

Background introduction

While updating my blog yesterday, I noticed that the Travis service was unavailable. Checking the Travis website, I saw the progress was stuck on pulling source code. This reminded me of GitHub’s Actions service.

Given it will be quite busy and requires an application to use, now that it’s officially launched, and with a free weekend, why not try out a new toy?

You can find official information on the website. I won’t be reposting it further. If you’ve used Kubernetes before, you’ll notice that the action YAML configuration is quite similar.

For beginner tutorials or introductory materials in Chinese, search for 阮一峰的博客. There are two articles: the first introduces basic grammar, and the second presents a practical case study.

Main body

Required knowledge points

  • github secrets
  • Action grammar

The core job is completed using existing components, pushed to the domestic Gitee with commands. The command portion is rather crude, utilizing force pushes and inheriting logic from Travis.

name: github pages and gitee pages

on:
  push:
    branches:
      - hugo

jobs:
  deploy:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: 'latest'
          extended: true

      - name: Build Github and Gitee ## 单个step只能写一个run命令
        run: hugo -b "https://www.xiangtianlong.com/" -d "github_public" && hugo -b "https://www.xiangtianlong.com/" -d "gitee_public" && ls

      - name: Deploy Github
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.BLOG_TOKEN }}
          publish_dir: ./github_public
          publish_branch: master
          cname: xiangtianlong.com

      - name: Deploy Gitee
        run: cd ./gitee_public && git init && git config user.name "TianlongXiang" && git config user.email "tianlongxiang51@gmail.com" && git add . && git commit -m "Update TianlongXiang's Blog" && git push --force "https://xiangtianlong:${{ secrets.GITEE_PASSWORD }}@gitee.com/xiangtianlong/xiangtianlong.git" master:master   

Afterword

Based on the actions provided by the official market, there are currently many supported features. After building a Docker image, you no longer need to rely on services from Docker Hub.

Reviewing Hugo’s issues, I found that for automatically deploying Git Pages using GitHub Actions, the final published webpage needs to be on the master branch. If deployed to another branch, GitHub will indicate a syntax issue with the deployment page in the settings.

The issue is that Hugo’s source files were located in the master branch, causing GitHub to treat them as Jelly blog code and fail checks

The solution is simple: Hugo source files are placed on another branch, and static files are published on the master branch

A financial IT programmer's tinkering and daily life musings
Built with Hugo
Theme Stack designed by Jimmy