前言
每年都要折腾一下自己的博客,今年准备把所有的博客再次迁移到静态博客 Hexo 中,配合 Github Action 实现,只需要一个浏览器就可以自动部署博客。
思路
整体思路很简单,在你的 github-pages 的项目建立两个 分支 master 和 gh-pages,在 gh-pages 分支上完成 hexo 的构建,然后将构建后的静态网页推送到 master 分支上

其中在 github actions 涉及的主要 action 为 github-pages-deploy-action,参考说明文档,需要使用到 token 或者 ssh-key,这里我使用到是前者,获取 token 的方法可以参考 encrypted-secrets,然后在你的 github-pages 仓库 secrets 设置一下 token,命名为 ACCESS_TOKEN
脚本
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 46 47 48 49 50 51 52
   | name: Blog CI/CD
 
  on:   push:     branches:        - gh-pages
  env:   TZ: Asia/Shanghai
  jobs:   blog-cicd:     name: Hexo blog build & deploy     runs-on: macos-latest 
      steps:     - name: Checkout codes       uses: actions/checkout@v2
      - name: Setup node              uses: actions/setup-node@v1       with:         node-version: '12.x'
      - name: Cache node modules              uses: actions/cache@v1       with:         path: ~/.npm         key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
      - name: Install hexo dependencies              run: |         npm install -g hexo-cli         npm install
      - name: Generate files              run: |         hexo clean         hexo generate
      - name: Deploy hexo blog              uses: JamesIves/github-pages-deploy-action@releases/v3       with:         ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}         BRANCH: master         FOLDER: public
   | 
 
这里略过了关于 hexo 的配置,如果对 hexo 的配置不太了解可以参考 hexo官网,到此,只要每次直接更新 gh-pages 就可以触发自动构建流程了。