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

GitHub Actions中Black工具仅检测不执行格式化的解决方法

问题:GitHub Actions无法自动格式化不符合Black规范的文件

在使用GitHub Actions时,无法自动对不符合Black格式规范的文件执行格式化操作。当前Workflow配置如下:

name: Neptune-AI WorkFlow

on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
      
      - name: Install Python Dependencies
        run: |
            if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

      - name: Lint with flake8
        run: |
          flake8 . --max-line-length=85 --statistics
        continue-on-error: false
      
      - name: black formatter
        uses: rickstaa/action-black@v1
        with:
          black_args: ". --check"
      - name: Annotate diff changes using reviewdog
        if: steps.action_black.outputs.is_formatted == 'true'
        uses: reviewdog/action-suggester@v1
        with:
          tool_name: blackfmt

执行时出现错误提示:

[action-black] Checking python code using the black formatter...
would reformat /github/workspace/neptunelearn/markov_models/markov_switch.py

Oh no! 💥 💔 💥
1 file would be reformatted, 24 files would be left unchanged.


解决方案

当前配置中black_args: ". --check"仅会检查文件格式是否符合规范,不会实际修改文件。要实现自动格式化并提交修改,需调整Workflow配置:

  1. 修改Black的执行参数:移除--check,让Black直接格式化文件
  2. 添加提交步骤:将格式化后的文件提交并推回仓库(需配置Git权限和用户信息)

修改后的完整Workflow配置如下:

name: Neptune-AI WorkFlow

on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
        # 拉取完整历史,确保提交能正常推送
        with:
          fetch-depth: 0
      
      - name: Install Python Dependencies
        run: |
            if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

      - name: Lint with flake8
        run: |
          flake8 . --max-line-length=85 --statistics
        continue-on-error: false
      
      - name: black formatter
        uses: rickstaa/action-black@v1
        with:
          black_args: "."  # 移除--check,直接格式化所有文件
      
      - name: Commit formatted files
        if: steps.action_black.outputs.is_formatted == 'true'
        run: |
          # 配置Git用户信息
          git config --global user.name "GitHub Actions"
          git config --global user.email "actions@github.com"
          # 提交修改
          git add .
          git commit -m "Auto-format code with Black"
          # 推回仓库
          git push
      
      - name: Annotate diff changes using reviewdog
        if: steps.action_black.outputs.is_formatted == 'true'
        uses: reviewdog/action-suggester@v1
        with:
          tool_name: blackfmt

关键说明:

  • fetch-depth: 0:确保Actions能获取完整的Git历史,避免推送失败
  • 移除--check参数后,Black会直接修改不符合规范的文件
  • 新增的提交步骤仅在有文件被格式化时触发(通过steps.action_black.outputs.is_formatted == 'true'判断)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 20:42:28