【问题标题】:Jenkins doesn't pick up Quality Gate failure詹金斯没有发现质量门失败
【发布时间】:2017-06-24 17:22:08
【问题描述】:

如果代码没有 90% 的测试覆盖率,我希望我的 Jenkins 构建失败。为此,我安装了 Quality Gates 插件,它应该检查 SonarQube 分析。

我在JenkinsQuality Gates下有如下配置:

Name: SonarQubeServer
SonarQube Server URL: http://my-server.com:9000
SonarQube account login: admin
SonarQube account password: ****

SonarQube 显示:Quality Gate Failed

Jenkins 显示:SonarQube analysis completed: SUCCESS 并且构建通过。

知道为什么 Jenkins 不知道质量门失败了吗?

【问题讨论】:

    标签: jenkins sonarqube


    【解决方案1】:

    最终我意识到我应该为我使用它的每项工作添加Quality Gates 作为Post Build Action

    【讨论】:

    • 您能否在答案中添加一个实际的声明性管道示例来说明您所做的事情?
    【解决方案2】:

    插件质量门仅返回状态:通过或失败,因此您可以根据这两个标志的结果从 jenkins 构建其他作业。但是如果你想在覆盖率 >90% 的情况下通过标志,你必须从 sonarqube 而不是 jenkins 配置它。在这种情况下,您可以想象这种情况:

    测试覆盖率 标志:失败。詹金斯不叫其他工作。

    测试覆盖率 >90 -> 标志:通过。詹金斯打电话给其他工作。

    我认为这可以以某种方式帮助您。

    【讨论】:

      【解决方案3】:

      您可以使用 Shell 命令执行此操作:如果有人需要,请分享此信息

      使用 Sonar Rest api 在质量门未通过时将构建标记为失败。在 Sonar Step 之后添加“Execute Shell”并使用以下代码 提示:在此步骤之前引入 10s 的休眠时间,只是为了确保 Sonar 站点更新任务结果状态。

      从工作区的 report-task.txt 中获取 TASKURL

      url=$(cat $WORKSPACE/.sonar/report-task.txt | grep ceTaskUrl | cut -c11-)

      从 Sonar 服务器获取任务属性

      curl -u admin:${admin_pwd} -L $url | python -m json.tool

      设置任务状态以检查声纳扫描是否成功完成。

      curl -u admin:${admin_pwd} -L $url -o task.json

      status=$(python -m json.tool

      回显 ${状态}

      如果 SonarScan 成功完成,则设置分析 ID 和 URL。

      如果 [ $status = 成功 ];那么

      analysisID=$(python -m json.tool

      analysisUrl="https://sonar.net/api/qualitygates/project_status?analysisId=${analysisID}

      echo ${分析ID}

      回声 ${analysisUrl}

      其他

      echo "声纳运行不成功"

      退出 1

      fi

      使用分析 URL 获取 SonarGate 详细信息

      curl -u admin:$admin_pwd ${analysisUrl} | python -m json.tool

      curl -u admin:$admin_pwd ${analysisUrl} | python -m json.tool | grep -i "状态" |切-c28- | sed 's/.$//' >> tmp.txt

      猫 tmp.txt

      sed -n '/ERROR/p' tmp.txt >> error.txt

      猫错误.txt

      如果 [ $(cat error.txt | wc -l) -eq 0 ];那么

      echo "质量门已通过!将 SonarQube 作业状态设置为成功!"

      其他

      退出 1

      echo "Quality Gate 失败!将 SonarQube 作业状态设置为失败!"

      fi

      清理文件

      未设置网址

      未设置状态

      未设置分析ID

      未设置分析网址

      task.json

      tmp.txt

      错误.txt

      【讨论】:

        【解决方案4】:

        回应 Sri 在他的解决方案中有一些类型/错误。 这是使用 sonar-scanner 构建的 sonar4.5.5

        if [ -e tmp.txt ];
        then
        rm tmp.txt
        rm error.txt
        rm task.json
        fi
        
        sleep 5
        
        cat $WORKSPACE/.scannerwork/report-task.txt
        
        url=$(cat $WORKSPACE/.scannerwork/report-task.txt | grep ceTaskUrl | cut -c11- )
        echo $url
        
        curl -u admin:pswd -L $url | python -m json.tool
        curl -u admin:pswd -L $url -o task.json
        status=$(python -m json.tool < task.json | grep -i "status" | cut --delimiter=: --fields=2 | sed 's/"//g' | sed 's/,//g' )
        echo ${status}
        
        if [ $status = SUCCESS ]; then
        analysisID=$(python -m json.tool < task.json | grep -i "analysisId" | cut -c24- | sed 's/"//g' | sed 's/,//g')
        analysisUrl="http://sonarserver/sonarqube/api/qualitygates/project_status?analysisId=${analysisID}"
        echo ${analysisID}
        echo ${analysisUrl}
        
        else
        echo "Sonar run was not success"
        exit 1
        
        fi
        
        
        curl -u admin:pswd ${analysisUrl} | python -m json.tool
        curl -u admin:pswd ${analysisUrl} | python -m json.tool | grep -i "status" | cut -c28- | sed 's/.$//' >> tmp.txt
        cat tmp.txt
        sed -n '/ERROR/p' tmp.txt >> error.txt
        cat error.txt
        if [ $(cat error.txt | wc -l) -eq 0 ]; then
        echo "Quality Gate Passed ! Setting up SonarQube Job Status to Success ! "
        else
        echo "Quality Gate Failed ! Setting up SonarQube Job Status to Failure ! "
        exit 1
        fi
        

        【讨论】:

          猜你喜欢
          • 2018-01-23
          • 1970-01-01
          • 2023-02-21
          • 1970-01-01
          • 2017-07-12
          • 2014-07-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多