.. back to home

使用 Github Actions 自动化发布博客


介绍背景:

博客网站myblog仓库底下有两个子库,一个子库是用来存储markdown文章的blogs仓库,一个是用来存储public内容的github pageslivebug.github.io仓库

当博客网站样式更新提交,或者文章提交,触发myblog仓库的 actions ,执行完之后发布到pages仓库

开发myblog仓库的提交 actions

 1# Sample workflow for building and deploying a Hugo site to GitHub Pages
 2name: Deploy Hugo site to Pages
 3
 4on:
 5  # Runs on pushes targeting the default branch
 6  push:
 7    branches: ["master"]
 8
 9  # Allows you to run this workflow manually from the Actions tab
10  workflow_dispatch:
11
12# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
13permissions:
14  contents: read
15  pages: write
16  id-token: write
17
18# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
19# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
20concurrency:
21  group: "pages"
22  cancel-in-progress: false
23
24# Default to bash
25defaults:
26  run:
27    shell: bash
28
29jobs:
30  # Build job
31  public:
32    runs-on: ubuntu-latest
33    env:
34      HUGO_VERSION: 0.114.0
35    steps:
36      - name: Setup Hugo # 初始化 护工环境
37        uses: peaceiris/actions-hugo@v2
38        with:
39          hugo-version: "latest" 
40      - name: Checkout # 检出资源
41        uses: actions/checkout@v3
42        with:
43          submodules: recursive # 子模块,当前是 blogs 仓库
44      - name: Build with Hugo   # 编译生成
45        env:
46          # For maximum backward compatibility with Hugo modules
47          HUGO_ENVIRONMENT: production
48          HUGO_ENV: production
49        run: |
50          hugo            
51      - name: Deploy  # 发布
52        uses: peaceiris/actions-gh-pages@v3
53        with:
54          personal_token: ${{ secrets.PERSONAL_TOKEN }} # 另外还支持 deploy_token 和 github_token
55          external_repository: livebug/livebug.github.io # 修改为你的 GitHub Pages 仓库
56          publish_dir: ./public
57          publish_branch: master

子库提交,父库刷新触发action,并重新部署

  1. 基础道理,提交到blogs中,触发仓库中的action,重新提交父库

  2. 提交父库时,会触发父库的action,自动重新部署

 1name: Send submodule updates to parent repo
 2
 3on:
 4  push:
 5    branches: 
 6      - master
 7
 8jobs:
 9  update:
10    runs-on: ubuntu-latest
11
12    steps:
13      - uses: actions/checkout@v2  # 检出父库
14        with: 
15          repository: livebug/myblog
16          token: ${{ secrets.PERSONAL_TOKEN }}
17          submodules: true
18
19      - name: Pull & update submodules recursively # 更新子库
20        run: |
21          git submodule update --init --recursive
22          git submodule update --recursive --remote          
23
24      - name: Commit      # 父库提交
25        run: |
26          git config user.email "actions@github.com"
27          git config user.name "GitHub Actions - update submodules"
28          git add --all
29          git commit -m "Update submodules" || echo "No changes to commit"
30          git push          

action 认证失败

需要重新刷新一下密钥,因为生成密钥时选择了期限,重新在仓库变量那重新输入刷新一下