使用 GitHub Actions 自动更新 repo 的子模块

ma1*_*234 8 git github

标题说明了一切。我尝试使用一堆不同的 git 命令,例如git submodule update --remote --mergegit submodule foreach git pull origin master,它们在我的计算机上运行良好,但在 GitHub 操作上运行时却没有。我尝试添加git status到工作流程中,状态仅显示“与原点/主控保持同步,无需提交”或类似内容。

Akn*_*sis 20

如今更好的选择是利用 Dependabot 自动为子模块创建 PR。

https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#package-ecosystem

每天检查子模块是否已更新的示例:在.github/dependabot.yml

version: 2

updates:
  - package-ecosystem: gitsubmodule
    schedule:
        interval: "daily"
    directory: /
Run Code Online (Sandbox Code Playgroud)

  • 这确实是更好的选择,但它有一个缺点:它不在周六和周日运行(请参阅https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options -for-the-dependabot.yml-file#scheduleinterval)。 (3认同)

Dro*_*let 17

您可以通过子模块存储库中的单个操作来实现此目的:

name: Send submodule updates to parent repo

on:
  push:
    branches: 
      - main

jobs:
  update:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        with: 
          repository: org/parent_repository
          token: ${{ secrets.PRIVATE_TOKEN_GITHUB }}
          submodules: true

      - name: Pull & update submodules recursively
        run: |
          git submodule update --init --recursive
          git submodule update --recursive --remote

      - name: Commit
        run: |
          git config user.email "actions@github.com"
          git config user.name "GitHub Actions - update submodules"
          git add --all
          git commit -m "Update submodules" || echo "No changes to commit"
          git push
Run Code Online (Sandbox Code Playgroud)

你需要:

通过此操作,main子模块存储库中分支的每次推送都将导致在父存储库中拉取更新的提交。

  • 请记住在repository:下面添加submodules: 'true': (2认同)

Ner*_*min 8

简单的解决方案

您递归地拉取和更新子模块,然后提交到存储库。

name: Update submodules

# Controls when the action will run.
on:
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  # This workflow contains a single job called "update"
  update:
    runs-on: ubuntu-latest

    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - name: Pull & update submodules recursively
        run: |
          git submodule update --init --recursive
          git submodule update --recursive --remote

      - name: Commit & push changes
        run: |
          git config --global user.name ${{ secrets.USER_NAME }}
          git config --global user.email ${{ secrets.USER_EMAIL }}
          git commit -am "Update submodules"
          git push
Run Code Online (Sandbox Code Playgroud)

因为我很懒,所以我没有像我那样使用 Github 秘密${{ secrets.USER_NAME }},只是硬编码我的 git 凭据,但我认为你可能关心安全性。


Art*_*tru 6

如果您需要通过 GitHub 操作自动更新对子模块的引用:

在您的父存储库中创建一个将同步引用的新工作流:

name: 'Submodules Sync'

on:
  # 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 'bot@noreply.github.com'
        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"
Run Code Online (Sandbox Code Playgroud)

在哪里

  • CI_TOKEN 是 GitHub 中的安全令牌变量,对父存储库具有“读写”访问权限,对子模块存储库具有“读取”访问权限。

在子(子模块)GitHub 操作中,将更改通知父级。

name: 'Submodule Notify Parent'

on:
  push:
    branches:
      - main    

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

jobs:
  notify:
    name: 'Submodule Notify Parent'
    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:
    - name: Github REST API Call
      env:
        CI_TOKEN: ${{ secrets.CI_TOKEN }}
        PARENT_REPO: <my_organization/my-app>
        PARENT_BRANCH: develop
        WORKFLOW_ID: <9999999>
      run: |
        curl -fL --retry 3 -X POST -H "Accept: application/vnd.github.v3+json" -H "Authorization: token ${{ env.CI_TOKEN }}" https://api.github.com/repos/${{ env.PARENT_REPO }}/actions/workflows/${{ env.WORKFLOW_ID }}/dispatches -d '{"ref":"${{ env.PARENT_BRANCH }}"}'
Run Code Online (Sandbox Code Playgroud)

在哪里

  • PARENT_REPO - 在 Github 中的父存储库的名称(my_org/my_app,如果没有组织,则只是 my_app)。
  • WORKFLOW_ID - 通过 rest API 调用的父 Wofklow ID。要找到它,请使用以下命令运行 curl CI_TOKEN
    curl -X GET -H "Authorization: token $CI_TOKEN" https://api.github.com/repos/$PARENT_REPO/actions/workflows
    
    Run Code Online (Sandbox Code Playgroud)

为每个子模块重复工作流创建。


PS 对我来说它的工作非常稳定。如果有的话,请使用上述逻辑添加有关现有 github 操作的信息。

  • 您好@ma1234,是的,可以设置工作流程运行的时间表,请参阅https://docs.github.com/en/actions/reference/events-that-trigger-workflows#scheduled-events在这种情况下,父分支与子模块引用不同步时会有时间间隔。 (2认同)

Von*_*onC 0

您可以将您的 GitHub Action 源代码与类似的源代码进行比较submodule-branch-check,后者确实至少生成了一个git submodule update.

检查是否update --remote足以从其自己的远程源拉取。