防止合并推送到主分支时触发递归工作流程

huangapple go评论51阅读模式
英文:

Prevent Recursive Workflow Triggering on Merge Push to main Branch

问题

我想知道如何防止GitHub Actions工作流递归触发自身。

我有一个简单的工作流,它拉取存储库并提交更改到main,然后推送到originmain

由于工作流在推送到main时触发,这导致工作流递归触发。 文档 指出,如果将GITHUB_TOKEN设置为${{ secrets.GITHUB_TOKEN }},则不应发生递归调用。

我做错了吗?

我的工作流:

name: Test preventing workflow recursion

on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  run:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3.5.2
        with:
          token: ${{ secrets.xxx }}            

      - name: Make changes and commit 
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          echo a >> test.txt
          git config user.name 'bot'
          git config user.email '...'
          git add .
          git commit -m "Update explorationsContent.md" --no-verify
          git push          

当我运行这个工作流时,我会递归触发更多的工作流运行。

英文:

I want to know how to prevent a GitHub Actions workflow from recursively triggering itself.

I have a simple workflow which pulls in the repository and commit a change to main, then push to origin to main.

Since the workflow has a trigger on push to main, this causes the workflow to be triggered recursively. The [documentation](https://docs.github.com/en/actions/using-workflows/triggering-a-workflow#triggering-a-workflow-from-a-workflow "Triggering a workflow from a workflow") indicates that if the GITHUB_TOKEN is set to ${{ secrets.GITHUB_TOKEN }}, then no recursive calling should occur.

Am I doing this incorrectly?

My workflow:

name: Test preventing workflow recursion

on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  run:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3.5.2
        with:
          token: ${{ secrets.xxx }}            

      - name: Make changes and commit 
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          echo a >> test.txt
          git config user.name 'bot'
          git config user.email '...'
          git add .
          git commit -m "Update explorationsContent.md" --no-verify
          git push

When I run this workflow, I recursively trigger more workflow runs.

答案1

得分: 1

初始问题是,在“结帐”步骤中我指定了一个令牌,这个令牌覆盖了“GITHUB_TOKEN”,因此防止递归工作流程的行为未激活。

但不幸的是,由于我的存储库上有分支保护规则,我必须使用一个具有绕过分支保护权限的 GitHub 用户的令牌,因此我不能使用“GITHUB_TOKEN”。

解决方案是在工作流中包含某种“基本情况”,它将阻止递归触发的第二个工作流程执行。

英文:

The initial problem is that I was specifying a token in the Checkout step which was overwriting the GITHUB_TOKEN so the behavior to prevent recursive workflows was not active.

But, unfortunately, since I have branch protection rules on my repo, I have to use a token for a github user with permissions to bypass branch protections, so I cannot use the GITHUB_TOKEN.

The solution is to include some type of "base case" in the workflow which will stop the second workflow that is kicked off recursively from executing.

答案2

得分: 0

你可以通过使用个人访问令牌(PAT,Personal Access Token)来避免出现“递归触发更多工作流运行”错误。

在你的 GitHub 帐户设置中创建你的 PAT。PAT 必须授予仓库权限以访问 'Contents' 和 'Workflows',然后一切应该正常工作。在这里,我已经重新编写了代码以提供一些帮助:

name: 防止工作流递归测试

on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  run:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          token: ${{ secrets.MY_PAT_WITH_REPO_CONTENTS_AND_WORKFLOWS_PERMISSIONS }}

      - name: 进行更改并提交
        run: |
          echo a >> test.txt
          git config user.name 'bot'
          git config user.email '...'
          git add .
          git commit -m "更新 explorationsContent.md" --no-verify
          git push          
英文:

You can avoid getting the "recursively trigger more workflow runs" error by using a PAT (Personal Access Token).

Create your PAT in your GitHub account settings. The PAT must grant repository permissions for 'Contents' and 'Workflows,' and then everything should work well. Here, I have rewritten the code to provide some help:

name: Test preventing workflow recursion

on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  run:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          token: ${{ secrets.MY_PAT_WITH_REPO_CONTENTS_AND_WORKFLOWS_PERMISSIONS }}

      - name: Make changes and commit
        run: |
          echo a >> test.txt
          git config user.name 'bot'
          git config user.email '...'
          git add .
          git commit -m "Update explorationsContent.md" --no-verify
          git push          

huangapple
  • 本文由 发表于 2023年5月25日 02:28:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定