Hello and welcome to this community, @prox40.
I think it’s possible to configure Jenkins to not fail the build when SonarQube reports a warning.
I guess this could be achieved by adjusting the Quality Gate settings in SonarQube and the pipeline script in Jenkins.
Adjust Quality Gate in SonarQube:
Quality Gates are a key feature in SonarQube that allows you to set a threshold for your project’s quality. If the project doesn’t meet the criteria, SonarQube will report a failed status.
I thought you could adjust these settings to not fail when a warning is reported but I haven’t found the proof in the documentation.
Adjust Jenkins Pipeline Script:
In your Jenkins pipeline script, you would have to ensure that the build doesn’t fail when SonarQube reports a warning. I think this could be done by adjusting the sonar-scanner
or mvn sonar:sonar
command in your script.
stage('SonarQube analysis') {
steps {
script {
try {
// Run sonar-scanner or mvn sonar:sonar command
sh 'mvn sonar:sonar'
} catch (Exception e) {
// Log the error but don't fail the build
echo "SonarQube analysis reported a warning: ${e}"
}
}
}
}
Not pretty, heh?
You could also use the catchError
step in Jenkins Pipeline to allow the build to continue even if the SonarQube step fails.
The catchError
step changes the result of the step that it encloses to UNSTABLE
if an error occurs during its execution, but it allows the pipeline to continue.
stage('SonarQube analysis') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE') {
// Run sonar-scanner or mvn sonar:sonar command
sh 'mvn sonar:sonar'
}
}
}
Here, if the mvn sonar:sonar
command fails, the catchError
step will catch the error, mark the build result as SUCCESS
and the stage result as UNSTABLE
, and allow the pipeline to continue.
Be aware that this approach will treat any error in the mvn sonar:sonar
command as a warning, not just Quality Gate warnings, which is not paramount.
If you want to specifically handle Quality Gate warnings, you would need to check the analysis result and adjust the build status accordingly.
This would likely require parsing the SonarQube analysis report or using the SonarQube API to get the Quality Gate status.