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

如何配置GitHub Actions让后续任务基于自动格式化新提交运行?

解决GitHub Actions中Build任务基于旧提交执行的问题

你的问题根源在于两个核心点:

  1. 格式化任务配置里的skip-commit: true会阻止googlejavaformat-action提交格式化后的代码修改,远程分支无法更新为格式化后的版本;
  2. 即便格式化任务成功提交了新代码,Build任务默认会checkout触发工作流的原始提交,而非分支最新代码,因此Checkstyle会因代码未格式化而失败。

解决方案步骤

1. 修正格式化任务的提交配置

将formatting任务中的skip-commit: true改为skip-commit: false,确保Action能将格式化后的修改提交并推送到远程分支。

2. 让Build任务拉取分支最新代码

修改build任务的actions/checkout步骤,指定拉取当前分支的最新版本,而非触发工作流的原始提交。

修改后的完整配置

name: Format and Build

on:
  push:
    # 可选:排除bot提交触发工作流,避免循环执行
    if: "!contains(github.event.head_commit.message, 'AUTO formatted by bot')"
  workflow_dispatch:

jobs:
  formatting:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: axel-op/googlejavaformat-action@v3
        with:
          skip-commit: false  # 改为false,允许提交格式化修改
          args: "--skip-sorting-imports --replace"
          commit-message: "refactor: AUTO formatted by bot"
          version: "1.16.0"
          github-token: ${{ secrets.GITHUB_TOKEN }}

  build:
    needs: formatting
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
        with:
          ref: ${{ github.ref }}  # 指定拉取当前分支
          fetch-depth: 0  # 拉取完整历史,确保获取最新提交
      - name: Build with Gradle
        run: ./gradlew build --no-daemon

额外说明

  • fetch-depth: 0用于确保Checkout步骤获取远程分支的所有最新提交,避免浅克隆导致拉不到刚推送的格式化提交;
  • on.push中的过滤条件可排除格式化bot的提交触发工作流,防止出现循环执行的情况。

内容的提问来源于stack exchange,提问作者Martin Maťovčík

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 18:45:27