You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何终止GitHub Actions工作流中的无限循环?附YML配置

终止GitHub Actions工作流无限循环的方案

当前配置存在的问题

  1. 你加的Stop Loop步骤用exit 0只是让该步骤成功结束,不会终止整个工作流,后续步骤依然会执行,完全起不到终止循环的作用。
  2. 无限循环的核心原因是build-and-release任务里的自动发布操作(marvinpinto/action-automatic-releases)会创建或更新标签,触发on: push/on: tags事件,导致工作流反复触发。

一、修复手动终止功能

直接在test任务的级别添加判断,只要是手动触发且勾选了stop,就跳过整个任务,连带后续依赖的build-and-release也不会执行:

jobs:
  test:
    runs-on: macos-latest
    # 新增判断:如果是手动触发且stop为true,就不执行这个任务
    if: github.actor != 'my_bot_account' && !(github.event_name == 'workflow_dispatch' && github.event.inputs.stop == 'true')
    steps:
      # 原有的步骤保留...

如果需要强制终止正在运行的工作流,也可以用GitHub API直接取消:

- name: Stop Loop
  if: github.event.inputs.stop == 'true'
  uses: actions/github-script@v6
  with:
    script: |
      await github.rest.actions.cancelWorkflowRun({
        owner: context.repo.owner,
        repo: context.repo.repo,
        run_id: context.runId
      });

二、从根源阻止循环触发

在工作流触发条件或任务执行条件里,排除自动发布bot的操作,避免它触发新的工作流:

1. 修改触发条件,排除bot推送

on:
  push:
    branches:
      - main
    tags:
      - '*'
    # 排除github官方bot的推送操作
    if: github.actor != 'github-actions[bot]'
  pull_request:
    branches:
      - main   
  workflow_dispatch:
    inputs:
      stop:
        description: 'Stop the loop'    

2. 在发布任务里添加bot排除条件

build-and-release:
  needs: test
  # 只在非bot触发的push或release事件下执行
  if: (github.event_name == 'release' || github.event_name == 'push') && github.actor != 'github-actions[bot]'
  runs-on: macos-latest
  steps:
    # 原有的步骤保留...

另外,你当前配置里的automatic_release_tag: "Safari-New-Release"会每次发布都更新同一个固定标签,这也是触发循环的原因之一。如果不需要固定标签,直接移除这个参数,改用动态标识(比如github.sha或时间戳)生成标题即可。


调整后的完整配置示例

name: Safari Extension CI

on:
  push:
    branches:
      - main
    tags:
      - '*'
    if: github.actor != 'github-actions[bot]'
  pull_request:
    branches:
      - main   
  workflow_dispatch:
    inputs:
      stop:
        description: 'Stop the loop'    

jobs:
  test:
    runs-on: macos-latest
    if: github.actor != 'my_bot_account' && !(github.event_name == 'workflow_dispatch' && github.event.inputs.stop == 'true')
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Install Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: '2.7'

      - name: Install Bundler
        run: gem install bundler
        working-directory: ./SafariExtApp
     
      - name: Install Node.js and NPM
        uses: actions/setup-node@v2
        with:
          node-version: '14.x'

      - name: Install dependencies
        run: npm install
        working-directory: ./SafariExtApp

      - name: Run tests
        run: npm test
        working-directory: ./SafariExtApp


  build-and-release:
    needs: test
    if: (github.event_name == 'release' || github.event_name == 'push') && github.actor != 'github-actions[bot]'
    runs-on: macos-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Install Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: '2.7'

      - name: Install Bundler
        run: gem install bundler
        working-directory: ./SafariExtApp
        
      - name: Install Node.js and NPM
        uses: actions/setup-node@v2
        with:
          node-version: '14.x'

      - name: Install dependencies
        run: npm install
        working-directory: ./SafariExtApp   

      - name: Upload artifacts
        uses: actions/upload-artifact@v2
        with:
          name: artifacts
          path: ./SafariExtApp/dist/*.zip

      - name: Release extension
        uses: marvinpinto/action-automatic-releases@latest
        with:
          repo_token: ${{ secrets.xyz }}
          files: ./SafariExtApp/dist/*.zip
          title: safariext${{ github.event.release.tag_name || github.sha }}-$(date +%s)
          prerelease: false
          draft: false

内容的提问来源于stack exchange,提问作者krishna dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 11:14:54