使用管道代码,
stage ('SonarQube') {
withSonarQubeEnv {
dir ('mydir/') {
sh "'${mvnHome}/bin/mvn' sonar:sonar -Dsonar.login=something -Dsonar.projectKey=someproj -Dsonar.projectName=somename"
}
}
timeout(time: 15, unit: 'MINUTES') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
在第一个mvn部分正确进行并在waitforqualitygate()操作中断:
org.sonarqube.ws.client.HttpException: Error 401 on http://mysonarserver/sonar/api/ce/task?id=somecode
该链接是可点击的,并导致填充的json结构。
为什么构建失败?似乎在声纳中正确设置了Webhook,其他声纳项目工作正常,jenkis中的webhook似乎也很活跃。
答案 0 :(得分:2)
如official documentation of the SonarQube Scanner for Jenkins中所述,您必须在waitForQualityGate()
之外使用withSonarQubeEnv
:
node {
stage('SCM') {
git 'https://github.com/foo/bar.git'
}
stage('SonarQube analysis') {
withSonarQubeEnv('My SonarQube Server') {
sh 'mvn clean package sonar:sonar'
} // SonarQube taskId is automatically attached to the pipeline context
}
}
// No need to occupy a node
stage("Quality Gate"){
timeout(time: 1, unit: 'HOURS') { // Just in case something goes wrong, pipeline will be killed after a timeout
def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}