Back
Featured image of post Building Blog With GitHub Actions

Building Blog With GitHub Actions

Reference

Prerequisite

建立好本地仓库和GitHub的连接:Connect Git to GitHub

Steps

Github Actions是GitHub提供的,使用脚本的大致作用如下:

The GitHub Action is going to grab the content from the XXX_source repository, build it using Hugo and push the output into the XXX repository.

创建一个新的SSH密钥对:

1ssh-keygen -t rsa -b 4096 -C "yourmail@mail.com" -f ~/.ssh/gh-pages -N ""

给绑定GitHub Pages的公开仓库添加公钥:

给源码仓库添加私钥:

注意:此 private key 的名称之后要填入 Action 脚本的特定位置。

给源码仓库添加脚本

在源码仓库中找到Actions栏,新建workflow:

可以直接找到workflow的模板:

可以在右侧查看actions市场和GitHub Actions的使用文档:

修改脚本内容

一些专用术语:

  • workflow (工作流程):持续集成一次运行的过程,就是一个 workflow。
  • job (任务):一个 workflow 由一个或多个 jobs 构成,含义是一次持续集成的运行,可以完成多个任务。
  • step(步骤):每个 job 由多个 step 构成,一步步完成。
  • action (动作):每个 step 可以依次执行一个或多个命令(action)。

脚本内容修改如下:

 1name: Deploy Hugo Site to Github Pages on Main Branch
 2
 3# 触发条件是git push
 4on:
 5  push:
 6    branches:
 7      - main
 8      - test
 9
10jobs:
11  build-deploy:
12    runs-on: ubuntu-latest
13    steps:
14      - uses: actions/checkout@v1  # v2 does not have submodules option now
15       # with:
16       #   submodules: true
17
18      - name: Setup Hugo
19        uses: peaceiris/actions-hugo@v2
20        with:
21          # The Hugo version to download (if necessary) and use. Example: 0.58.2
22          hugo-version: '0.108.0'  # default is 'latest', better use your local development version
23          extended: true
24
25      - name: Build
26        run: hugo --minify
27
28      - name: Deploy
29        uses: peaceiris/actions-gh-pages@v3
30        with:
31          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} # 这里的 ACTIONS_DEPLOY_KEY 则是上面设置 Private Key的变量名
32          external_repository: Morimit/Morimit.github.io # Pages绑定的远程仓库 
33          publish_dir: "./public" 
34          keep_files: false # remove existing files
35          publish_branch: main  # deploying branch
36          commit_message: ${{ github.event.head_commit.message }}
  • 注意:publish_branch要和该仓库绑定GitHub Pages的发布branch相一致
  • 上面的步骤完成后,每次从本地git push源文件到GitHub都会触发GitHub Actions脚本把./public文件夹更新到GitHub pages。

搭建完成后的workflow

workflow