由于您在10个月内没有收到答案,我将尽可能地提供帮助 这是我的GitLab工作示例,但您应该能够更改这一点,因为插件相似(https://wiki.jenkins.io/display/JENKINS/GitHub+Plugin#GitHubPlugin-Settingcommitstatus):
#!groovy
pipeline {
options {
buildDiscarder(
logRotator(artifactDaysToKeepStr: '21', artifactNumToKeepStr: '4', daysToKeepStr: '21', numToKeepStr: '4')
)
gitLabConnection('GitLab')
}
agent any
tools {
maven 'Default Maven'
jdk 'DefaultJDK'
}
stages {
stage('Build') {
steps {
sh "mvn clean install -U"
}
}
stage('Source Code Analysis') {
steps {
withMaven() {
sh "mvn " +
"-Dsonar.branch='${env.BRANCH_NAME}' " +
"-Dsonar.analysis.mode=preview " +
"-Dsonar.gitlab.commit_sha=\$(git log --pretty=format:%H origin/master..'${env.BRANCH_NAME}' | tr '\\n' ',') " +
"-Dsonar.gitlab.ref_name='${env.BRANCH_NAME}' " +
"sonar:sonar"
}
withMaven() {
sh "mvn -Dsonar.branch='${env.BRANCH_NAME}' sonar:sonar"
}
}
}
}
post {
success {
echo 'posting success to GitLab'
updateGitlabCommitStatus(name: 'jenkins-build', state: 'success')
}
failure {
echo 'posting failure to GitLab'
updateGitlabCommitStatus(name: 'jenkins-build', state: 'failed')
}
always {
deleteDir()
}
}
}
这包括各种位,但涵盖了您尝试执行的操作,声纳分析以两部分预览进行(对提交的注释和这些注释在打开时转移到合并请求),然后是正常的分析后记
在pom项目中,我还定义了:
<sonar.gitlab.project_id>${gitlab.project_id}</sonar.gitlab.project_id>
<sonar.gitlab.unique_issue_per_inline>true</sonar.gitlab.unique_issue_per_inline>
<sonar.gitlab.user_token>GITLAB_USER_TOKEN</sonar.gitlab.user_token>
<sonar.gitlab.url>${git.hostname.url}</sonar.gitlab.url>
如果您添加这些并替换缺少的位,我相信这将解决您的问题。
编辑:我相信你需要以下github选项,而不是GitLab选项:
-Dsonar.analysis.mode=preview \
-Dsonar.github.pullRequest=$PULL_REQUEST_ID \
-Dsonar.github.repository=myOrganisation/myProject \
-Dsonar.github.oauth=$GITHUB_ACCESS_TOKEN \
-Dsonar.host.url=https://server/sonarqube \
-Dsonar.login=$SONARQUBE_ACCESS_TOKEN