使用github actions来部署 github pages

Posted by 111qqz on Saturday, January 23, 2021

TOC

目前我的博客是部署在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的配置文件

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on:  ubuntu-18.04

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v1
        with:
          submodules:  true
      
      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: '0.62.2'


      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          git config --global user.email "hust.111qqz@gmail.com"
          git config --global user.name "111qqz"
          hugo --minify
      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} # 这里的 ACTIONS_DEPLOY_KEY 则是上面设置 Private Key的变量名
          external_repository: 111qqz/111qqz.github.io # Pages 远程仓库 
          publish_dir: public
          keep_files: false # remove existing files
          publish_branch: master  # deploying branch
          commit_message: ${{ github.event.head_commit.message }}

使用algolia构建搜索索引

其实使用algolia来作为静态博客的搜索工具的方案我是之前就知道的 但是这个工具依赖于npm..而我除了刚毕业写了一阵react之外,是不怎么接触nodejs的。。 再加上我对环境有洁癖。。不想每换一个环境就先装个nodejs。。。 因此algolia生成的索引一直都停留在好久之前。

不过现在有了github actions,很容易想到把推送algolia数据的过程放到github上来做

加上这部分之后,配置文件是这个样子了:


# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on:  ubuntu-18.04

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v1
        with:
          submodules:  true
      
     
      - name: setup npm env
        uses: actions/setup-node@v2
        with:
            node-version: '12'
      - run: npm install atomic-algolia --save
      
      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: '0.62.2'


      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          git config --global user.email "hust.111qqz@gmail.com"
          git config --global user.name "111qqz"
          hugo --minify
          
      - name: upload algolia data
        uses: actions/setup-node@v2
        with:
            node-version: '12'
      - run: npm run algolia
     
      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} # 这里的 ACTIONS_DEPLOY_KEY 则是上面设置 Private Key的变量名
          external_repository: 111qqz/111qqz.github.io # Pages 远程仓库 
          publish_dir: public
          keep_files: false # remove existing files
          publish_branch: master  # deploying branch
          commit_message: ${{ github.event.head_commit.message }}

参考资料