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

如何在Windows版GitHub Runner中用官方Action检查SonarQube质量门状态

解决Windows自托管Runner上SonarQube质量门检查失败问题

问题根源

官方sonarsource/sonarqube-quality-gate-action依赖bash环境执行,而你的Windows自托管Runner默认使用PowerShell/Cmd,缺少bash环境导致报错bash: command not found。

解决方案

方案1:为质量门检查步骤指定bash shell(需Windows Runner已安装bash)

如果你的Windows服务器已安装Git(自带Git Bash)或WSL,可以在质量门检查步骤中强制使用bash:

- name: SonarQube Quality Gate check
  id: sonarqube-quality-gate-check
  uses: sonarsource/sonarqube-quality-gate-action@master
  with:
    scanMetadataReportFile: .sonarqube\out\.sonar\report-task.txt
  timeout-minutes: 5
  shell: bash  # 强制使用bash执行该Action

注意:需确保bash可执行文件在Runner的PATH中,比如Git Bash默认路径是C:\Program Files\Git\bin\bash.exe,可将该路径添加到系统环境变量PATH中。

方案2:用PowerShell脚本替代官方Action(无需bash环境)

直接编写PowerShell脚本调用SonarQube API检查质量门状态,完全适配Windows环境:

  1. 替换原有的SonarQube Quality Gate check步骤为以下内容:
- name: SonarQube Quality Gate check
  id: sonarqube-quality-gate-check
  env:
    SONAR_TOKEN: ${{ secrets.US_SONAR_TOKEN }}
    SONAR_HOST_URL: https://sonarqube.host.com
  run: |
    # 读取扫描元数据文件
    $reportTaskPath = ".sonarqube\out\.sonar\report-task.txt"
    $reportContent = Get-Content $reportTaskPath -Raw
    # 提取ceTaskUrl和projectKey
    $ceTaskUrl = [regex]::Match($reportContent, 'ceTaskUrl=(.*)').Groups[1].Value
    $projectKey = [regex]::Match($reportContent, 'projectKey=(.*)').Groups[1].Value

    # 设置API请求头
    $headers = @{
      "Authorization" = "Bearer $env:SONAR_TOKEN"
    }

    # 轮询质量门状态(最多等待5分钟,每10秒查询一次)
    $maxAttempts = 30
    $attempt = 0
    $status = "PENDING"
    while ($attempt -lt $maxAttempts -and $status -eq "PENDING") {
      Start-Sleep -Seconds 10
      $attempt++
      # 获取任务状态
      $taskResponse = Invoke-RestMethod -Uri "$ceTaskUrl" -Headers $headers -Method Get
      $status = $taskResponse.task.status
      
      if ($status -eq "SUCCESS") {
        # 获取质量门状态
        $qualityGateResponse = Invoke-RestMethod -Uri "$env:SONAR_HOST_URL/api/qualitygates/project_status?projectKey=$projectKey" -Headers $headers -Method Get
        $gateStatus = $qualityGateResponse.projectStatus.status
        
        # 设置输出变量
        echo "quality-gate-status=$gateStatus" >> $env:GITHUB_OUTPUT
        
        # 如果质量门失败,终止工作流
        if ($gateStatus -eq "FAILED") {
          Write-Error "SonarQube Quality Gate failed"
          exit 1
        } elseif ($gateStatus -eq "WARN") {
          Write-Warning "SonarQube Quality Gate has warnings"
        } else {
          Write-Host "SonarQube Quality Gate passed"
        }
        break
      } elseif ($status -eq "FAILED") {
        Write-Error "SonarQube analysis task failed"
        exit 1
      }
    }

    if ($attempt -ge $maxAttempts) {
      Write-Error "SonarQube Quality Gate check timed out after 5 minutes"
      exit 1
    }
  shell: powershell
  1. 后续步骤中使用输出变量的逻辑保持不变:
- name: "Example show SonarQube Quality Gate Status value"
  run: echo "The Quality Gate status is ${{ steps.sonarqube-quality-gate-check.outputs.quality-gate-status }}"

验证说明

两种方案都能实现质量门检查功能:

  • 方案1依赖bash环境,适合已安装Git或WSL的Windows服务器;
  • 方案2完全基于PowerShell,无需额外依赖,更适配纯Windows环境。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 12:48:30