在 Jenkinsfile 中设置 SonarQube webhook [英] Set a SonarQube webhook in Jenkinsfile

查看:27
本文介绍了在 Jenkinsfile 中设置 SonarQube webhook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 Jenkins 多分支管道,每次推送到 bitbucket 时,都会在项目的该分支上执行 SonarQube 分析.Jenkins 为每个分支正确创建了新作业,并在 SonarQube 中创建了一个新项目,并将分支名称附加到项目名称中.

I'm trying to create a Jenkins multibranch pipeline where on every push to bitbucket, a SonarQube analysis is performed on that branch of the project. Jenkins correctly creates the new job for each branch and a new project is created in SonarQube with the branch name appended to the project name.

我遇到的问题是,当 SonarQube 创建新项目时,默认情况下未设置报告 Quality Gate 状态的 webhook,因此我必须手动进入每个 SonarQube 项目并设置 Webhook url.当我的团队每天创建许多分支时,这是一个问题.

The issue I'm having is that when SonarQube creates the new project, the webhook to report the Quality Gate status is not set by default, so I have to manually go into each SonarQube project and set the Webhook url. This is an issue when my team makes many branches a day.

有没有办法在我的 Jenksfile 中指定我希望 SonarQube 项目有一个 webhook?

Is there a way to specify in my Jenksfile that I want the SonarQube project to have a webhook?

stage('SonarQube Analysis') {
        steps {
            withSonarQubeEnv('Sonarqube Server') {
                script {
                    def sonarScanner = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
                    sh "${sonarScanner}/bin/sonar-scanner " +
                    "-Dsonar.projectKey=ProjectName-${GIT_BRANCH} " +
                    "-Dsonar.projectName=ProjectName-${GIT_BRANCH} " +
                    "-Dsonar.projectVersion=0.0.0 " +
                    "-Dsonar.sources=**/src " +
                    "-Dsonar.java.binaries=**/build " +
                    "-Dsonar.exclusions=excluded_dirs/** " +
                    "-Dsonar.sourceEncoding=UTF-8"
                }
            }
            timeout(time: 5, unit: 'MINUTES') {
                script {
                    def qg = waitForQualityGate()
                    if (qg.status != 'OK') {
                        error "Pipeline aborted due to a quality gate failure: ${qg.status}"
                    }
                }
            }
        }
    }

目前,我的 Jenkins 构建在 5 分钟后超时.设置 webhook 后,需要几秒钟才能收到回复.我的 webhook url 是正确的,我只想让 Jenkinsfile 设置它,而不是我手动设置.

Currently, my Jenkins build times out after 5 minutes. When the webhook is set, it takes a few seconds to hear back. My webhook url is correct, I just want the Jenkinsfile to set it, not me manually.

不幸的是,我不是 SonarQube 的管理员,只是我的项目

Unfortunately, I am not an admin in SonarQube, only my projects

推荐答案

作为 sonarqube 中的管理员,转到 https://my-sonarqube.tld/admin/webhooks将 url 配置为 https://my-jenkins-domain.tld/sonarqube-webhook/

As admin in sonarqube, go to https://my-sonarqube.tld/admin/webhooks configure the url to be https://my-jenkins-domain.tld/sonarqube-webhook/

这应该适用于所有项目.如果您仍未收到货品,请检查最近的货品(同一页面中的选项)并查看错误.

This should then apply to all projects. If you are still not receiving deliveries, check recent deliveries (option in same page) and view error.

您的 jenkins 需要有一个有效的证书才能建立安全连接

Your jenkins will need to have a valid certificate for a secure connection to be established

另请参阅:https://docs.sonarqube.org/latest/project-管理/webhook/

或者,您可以在每次调用/扫描项目时设置一个 webhook.在 cli -Dsonar.webhooks.project=https://my-jenkins-domain.tld/sonarqube-webhook/ 或 sonar-project.properties onar.webhooks.project=https://my-jenkins-domain.tld/sonarqube-webhook/

Alternatively, you can set a webhook per invocation/scan of a project. Either on the cli -Dsonar.webhooks.project=https://my-jenkins-domain.tld/sonarqube-webhook/ or in sonar-project.properties onar.webhooks.project=https://my-jenkins-domain.tld/sonarqube-webhook/

这篇关于在 Jenkinsfile 中设置 SonarQube webhook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆