使用github actions来部署 github pages
目前我的博客是部署在github pages上,源码是一个repo,渲染出来的静态页面是一个repo. 更新的时候是把后者作为前者的submodule. 感觉这种方式有些落后了,简直和某司内部的平台有的一比。因此尝试采用了下github actions,来自动化这个部署的流程。
build github pages
其实类似gitlab ci. 最开始我以为需要自己配置服务器,结果发现并不需要,直接用公用的就可以。 详细内容可以阅读github actions
遇到的主要问题其实是,在一个repo的github actions的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 }}
51
使用algolia构建搜索索引
其实使用algolia来作为静态博客的搜索工具的方案我是之前就知道的 但是这个工具依赖于npm..而我除了刚毕业写了一阵react之外,是不怎么接触nodejs的。。 再加上我对环境有洁癖。。不想每换一个环境就先装个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 }}
66