如何避免机器人触发 GHA 中的工作流程

问题描述 投票:0回答:1

有一个具有子模块的存储库,在 PR 触发的工作流中,它提交并将更改推送到子模块,之后,工作流有一个步骤来更新调用者存储库以获取子模块中的最新更改。

这是代码:

 - name: Checkout submodule repo
    uses: actions/checkout@v4
    with:
      repository: submodule_repository
      path: submodule
      token: ${{ env.GITHUB_TOKEN }}
      fetch-depth: 0
      persist-credentials: true

  - name: Writes to submodule
    run: |
      python ./main.py

  - name: Push changes to submodule
    working-directory: submodule
    run: |
      git config user.name "name"
      git config user.email "email"
      git add .
      git commit -m "Created or excluded DAG"
      git push

  - name: Checkout caller repo
    uses: actions/checkout@v4
    with:
      ref:  ${{ github.event_name == 'pull_request' && github.head_ref || github.ref }}
      path: caller
      token: ${{ env.GITHUB_TOKEN }}
      submodules: true

  - name: Update caller repo to latest submodule
    working-directory: caller
    run: |
      git config user.name "name"
      git config user.email "email"
      git submodule update --recursive --remote
      git add .
      git commit -m "Udpated submodule"
      git push

${{ env.GITHUB_TOKEN }}
是一个 Github 应用程序。

问题是,在步骤

Update caller repo to latest submodule
中,当它推送到调用者存储库分支时,它会再次触发工作流程,创建一个循环,根据此doc,这是不应该发生的。

您可以看到下面的运行,其中运行号

151
是由我向 main 创建 PR 触发的,而
152
是由运行号
Update caller repo to latest submodule
的步骤
151
中的推送触发的:

enter image description here

git github github-actions
1个回答
0
投票

@GuiFalourd 的主意好极了。

解决方案是在提交消息中添加

[skip actions]

  - name: Update caller repo to latest submodule
    working-directory: caller
    run: |
      git config user.name "name"
      git config user.email "email"
      git submodule update --recursive --remote
      git add .
      git commit -m "[skip actions] Udpated submodule"
      git push

顺便说一句,发生这种情况是因为

${{ env.GITHUB_TOKEN }}
是 Github 应用程序令牌,因此根据 doc 触发另一次运行是正确的。

© www.soinside.com 2019 - 2024. All rights reserved.