目前我的博客是部署在 GitHub Pages 上,源码是一个 repo,渲染出来的静态页面是一个 repo。更新的时候是把后者作为前者的 submodule。 感觉这种方式有些落后了,简直和某司内部的平台有的一比。因此尝试用 GitHub Actions 来自动化这个部署流程。
构建 GitHub Pages#
其实类似 GitLab CI。最开始我以为需要自己配置服务器,结果发现并不需要,直接用公用的就可以。 详细内容可以阅读 GitHub Actions。
遇到的主要问题是,在一个 repo 的 pipeline 里推送到另外一个 repo 时提示权限错误。
解决的办法是配置 ssh key。
假设源码的 repo 称为 A,渲染得到的静态页面的 repo 称为 B,那么先生成一对 ssh-key。
然后在 A 里,settings -> secrets 添加一个 secret,名称为 ACTIONS_DEPLOY_KEY,内容为 private key 的内容。
然后在 B 里,settings -> deploy keys 添加一个 key,名称无所谓,内容为 public key 的内容。
这样每次 push 到源码 repo A,就会自动触发 GitHub Actions,将静态页面推送到 repo B,然后 repo B 自动触发 GitHub Pages 机制。
附一个 GitHub Actions 的配置文件
详细代码
1# This is a basic workflow to help you get started with Actions
2
3name: CI
4
5# Controls when the action will run.
6on:
7 # Triggers the workflow on push or pull request events but only for the master branch
8 push:
9 branches: [ master ]
10 pull_request:
11 branches: [ master ]
12
13 # Allows you to run this workflow manually from the Actions tab
14 workflow_dispatch:
15
16# A workflow run is made up of one or more jobs that can run sequentially or in parallel
17jobs:
18 # This workflow contains a single job called "build"
19 build:
20 # The type of runner that the job will run on
21 runs-on: ubuntu-18.04
22
23 # Steps represent a sequence of tasks that will be executed as part of the job
24 steps:
25 # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
26 - uses: actions/checkout@v1
27 with:
28 submodules: true
29
30 - name: Setup Hugo
31 uses: peaceiris/actions-hugo@v2
32 with:
33 hugo-version: '0.62.2'
34
35
36 # Runs a set of commands using the runners shell
37 - name: Run a multi-line script
38 run: |
39 git config --global user.email "hust.111qqz@gmail.com"
40 git config --global user.name "111qqz"
41 hugo --minify
42 - name: Deploy
43 uses: peaceiris/actions-gh-pages@v3
44 with:
45 deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} # 这里的 ACTIONS_DEPLOY_KEY 则是上面设置 Private Key的变量名
46 external_repository: 111qqz/111qqz.github.io # Pages 远程仓库
47 publish_dir: public
48 keep_files: false # remove existing files
49 publish_branch: master # deploying branch
50 commit_message: ${{ github.event.head_commit.message }}使用 Algolia 构建搜索索引#
其实用 Algolia 作为静态博客搜索工具的方案我之前就知道,但是这个工具依赖于 npm,而我除了刚毕业写了一阵 React 之外,不怎么接触 Node.js。 再加上我对环境有洁癖,不想每换一个环境就先装个 nodejs,因此 Algolia 生成的索引一直都停留在好久之前。
不过现在有了 GitHub Actions,很容易想到把推送 Algolia 数据的过程放到 GitHub 上来做。
加上这部分之后,配置文件是这个样子了:详细代码
1
2# This is a basic workflow to help you get started with Actions
3
4name: CI
5
6# Controls when the action will run.
7on:
8 # Triggers the workflow on push or pull request events but only for the master branch
9 push:
10 branches: [ master ]
11 pull_request:
12 branches: [ master ]
13
14 # Allows you to run this workflow manually from the Actions tab
15 workflow_dispatch:
16
17# A workflow run is made up of one or more jobs that can run sequentially or in parallel
18jobs:
19 # This workflow contains a single job called "build"
20 build:
21 # The type of runner that the job will run on
22 runs-on: ubuntu-18.04
23
24 # Steps represent a sequence of tasks that will be executed as part of the job
25 steps:
26 # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
27 - uses: actions/checkout@v1
28 with:
29 submodules: true
30
31
32 - name: setup npm env
33 uses: actions/setup-node@v2
34 with:
35 node-version: '12'
36 - run: npm install atomic-algolia --save
37
38 - name: Setup Hugo
39 uses: peaceiris/actions-hugo@v2
40 with:
41 hugo-version: '0.62.2'
42
43
44 # Runs a set of commands using the runners shell
45 - name: Run a multi-line script
46 run: |
47 git config --global user.email "hust.111qqz@gmail.com"
48 git config --global user.name "111qqz"
49 hugo --minify
50
51 - name: upload algolia data
52 uses: actions/setup-node@v2
53 with:
54 node-version: '12'
55 - run: npm run algolia
56
57 - name: Deploy
58 uses: peaceiris/actions-gh-pages@v3
59 with:
60 deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} # 这里的 ACTIONS_DEPLOY_KEY 则是上面设置 Private Key的变量名
61 external_repository: 111qqz/111qqz.github.io # Pages 远程仓库
62 publish_dir: public
63 keep_files: false # remove existing files
64 publish_branch: master # deploying branch
65 commit_message: ${{ github.event.head_commit.message }}