目的: 每次在fridayhub/blog仓库修改的博客源文件,push到github以后,自动编译构建,自动推送到[博客仓库,最终使得博客得到更新.

github 自动构建流程:

workflows

具体步骤:

前提: 源码仓库 fridayhub/blog 博客仓库 fridayhub/fridayhub.github.io

1. 生成ssh key

为两个仓库绑定ssh key,当在源码仓库提交代码,github actions编译生成静态的网站文件,push到博客仓库 gen-ssh-key

注意生成路径,不要将默认的秘钥覆盖了

2. 添加秘钥到仓库setting中

pulic key添加到博客仓库 set-pub-key

private key添加到源码仓库setting中 set-private-key 注意,private key的name ACTIONS_DEPLOY_KEY,将用于后续的自动化构建脚本中。

3.增加自动化脚本:

在源码仓库的目录中新增:.github/workflows/main.yml

 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
name: Deploy Hugo Site to Github Pages on Master Branch
         
on:         
  push:  
    branches:
      - master
         
jobs:       
  build-deploy:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v1  # v2 does not have submodules option now
       # with:
       #   submodules: true
         
      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: '0.73.0'
          # extended: true
         
      - name: Build
        run: hugo --minify && cp -r static public/     #需要拷贝静态文件                                                                                                                                                        
         
      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} # 这里的 ACTIONS_DEPLOY_KEY 则是上面设置 Private Key的变量名
          external_repository: fridayhub/fridayhub.github.io # push到哪个仓库 
          publish_dir: ./public # push的目录
          keep_files: false # remove existing files
          publish_branch: master  # deploying branch
          commit_message: ${{ github.event.head_commit.message }}

构建过程可以在actions中查看: actions