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

如何在GitHub Actions中存储flake8退出码为变量而非终止工作流?

让flake8失败后GitHub Action工作流继续执行后续检查任务

当前你的工作流里,flake8检查失败会直接终止整个流程,导致后续的代码检查任务没法运行。要解决这个问题,只需要给flake8的步骤加上continue-on-error: true配置,这样就算flake8检查不通过,后续任务依然能正常执行。如果需要整个工作流最终根据flake8的结果标记失败,还可以在后续步骤里做状态校验。

修改后的完整工作流配置如下:

on:
  push:
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@main
      with:
        ref: ${{ github.head_ref }}
    - name: Setup Python
      uses: actions/setup-python@main
      with:
        python-version: '3.9'
    - name: Install dependencies
      run: |
        pip install -r requirements.txt
    - name: Lint with flake8
      id: flake8-lint
      continue-on-error: true  # 核心配置:flake8失败后不终止工作流
      run: |
        flake8 . --count --max-complexity=15 --max-line-length=127 --statistics
    ## 后续代码检查任务可以正常执行
    - name: 后续检查任务1
      run: |
        echo "执行后续检查任务1"
    - name: 后续检查任务2
      run: |
        echo "执行后续检查任务2"
    # 可选:如果需要整个工作流在flake8失败时最终标记为失败
    - name: 校验flake8结果
      if: steps.flake8-lint.outcome == 'failure'
      run: |
        echo "flake8代码检查未通过"
        exit 1

关键配置说明:

  • continue-on-error: true:让该步骤执行失败时,工作流不会中断,后续步骤继续运行
  • id: flake8-lint:给flake8步骤设置唯一ID,方便后续步骤引用它的执行状态
  • 最后的可选校验步骤:通过if条件判断flake8步骤是否失败,如果失败就执行exit 1,让整个工作流最终标记为失败,同时不影响中间后续检查任务的执行

内容的提问来源于stack exchange,提问作者Alex F

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 13:24:26