跳至主内容

利用 Github Action 自动更新 submodule

喵小六

每次本博客的上游主题更新的时候,都要手动更新一下 submodule,都 2021 年了,我选择自动化。

{{< toc >}}

一、设置 token

1.1 生成一个新的 token

在配置 Github Action 之前,我们需要新建一个供脚本使用的 token。

比如 Auto Update Submodule of blog.hexsix.me

把 repo 全部勾上,我们需要 repo 的读写权限。

记住 token,马上就要用了

PS. 本节内容翻译自 https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token

1.2 在项目内配置 token 变量

PS. 本节内容翻译自 https://docs.github.com/en/actions/security-guides/encrypted-secrets

二、配置 Github Action

  1. 在 Actions 页面新建 workflow。
  2. 找到 Skip this and set up a workflow yourself
name: 'Submodules Sync'

on:
  schedule:
  - cron: "0 2 * * *"
  # Allows you to run this workflow manually from the Actions tab or through HTTP API
  workflow_dispatch:

jobs:
  sync:
    name: 'Submodules Sync'
    runs-on: ubuntu-latest

    # Use the Bash shell regardless whether the GitHub Actions runner is ubuntu-latest, macos-latest, or windows-latest
    defaults:
      run:
        shell: bash

    steps:
    # Checkout the repository to the GitHub Actions runner
    - name: Checkout
      uses: actions/checkout@v2
      with:
        token: ${{ secrets.CI_TOKEN }}
        submodules: true

    # Update references
    - name: Git Sumbodule Update
      run: |
        git pull --recurse-submodules
        git submodule update --remote --recursive

    - name: Commit update
      run: |
        git config --global user.name 'Git bot'
        git config --global user.email '[email protected]'
        git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
        git commit -am "Auto updated submodule references" && git push || echo "No changes to commit"

其中

完成创建后立即运行一次,看看有没有问题吧。

PS. 本节内容翻译自 https://stackoverflow.com/questions/64407333/using-github-actions-to-automatically-update-the-repos-submodules

如果你的 submodule 是自己的,链接里还展示了一种 submodule 里创建 Actions 主动推送形式更新 submodule 的方法,本文就不展开了。

编辑此文章
上一篇
除了死,生活与故事没有终点。
下一篇
腾讯云轻量服务器做BT下载并自动上传到阿里云盘