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

GitHub Actions Windows执行器删除Gradle缓存报Device or resource busy如何解决

解决方案

你可以选择以下任意一种方案适配Windows环境的删除需求:

方案1:使用PowerShell执行清理(Windows环境专属,成功率更高)

Windows下PowerShell自带强制删除占用文件的能力,还可以先终止占用进程,你可以把你的清理步骤修改为跨平台适配的版本:

- name: Turn off the Gradle Daemon
  shell: bash
  run: ./gradlew --stop
- name: Cleanup Gradle Cache
  shell: pwsh
  run: |
    $gradleCachePath = Join-Path $env:USERPROFILE ".gradle\caches\modules-2"
    # 终止所有占用锁文件的进程
    Get-ChildItem -Path $gradleCachePath -Filter "modules-2.lock" -ErrorAction SilentlyContinue | ForEach-Object {
      $lockFile = $_.FullName
      $processes = Get-CimInstance -ClassName Win32_Process | Where-Object { $_.GetRelated("Win32_Handle").FileName -eq $lockFile }
      if ($processes) {
        $processes | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }
        # 等待进程完全退出释放资源
        Start-Sleep -Seconds 2
      }
    }
    # 强制删除目标文件
    Remove-Item -Path (Join-Path $gradleCachePath "modules-2.lock") -Force -ErrorAction SilentlyContinue
    Remove-Item -Path (Join-Path $gradleCachePath "gc.properties") -Force -ErrorAction SilentlyContinue

方案2:Git Bash环境下的强制删除方案

如果你坚持要用Git Bash执行清理,可以调用Windows自带工具强制终止占用后删除,同时兼容Linux、macOS环境:

- name: Cleanup Gradle Cache
  shell: bash
  run: |
    # Windows环境先强制杀所有残留Gradle关联Java进程
    if [[ "$RUNNER_OS" == "Windows" ]]; then
      taskkill //F //IM java.exe 2>/dev/null || true
      sleep 3
      # 用Windows原生del命令强制删除,比rm兼容性更好
      cmd.exe /c "del /f /q %USERPROFILE%\.gradle\caches\modules-2\modules-2.lock" 2>/dev/null || true
      cmd.exe /c "del /f /q %USERPROFILE%\.gradle\caches\modules-2\gc.properties" 2>/dev/null || true
    fi
    # 兜底用rm删除,兼容Linux、macOS环境
    rm -f ~/.gradle/caches/modules-2/modules-2.lock 2>/dev/null || true
    rm -f ~/.gradle/caches/modules-2/gc.properties 2>/dev/null || true

两种方案都添加了错误忽略配置,就算目标文件不存在也不会导致CI步骤失败,符合持续集成环境的运行要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 11:54:03