Context: In Sonar Qube, there exists a custom Quality Gate which is called say abcd
. This is NOT the default quality gate. And in Jenkins, I had configured this
Using approach, suggested by A_Di-Matteo , I faced an issue: when trying to select a gate for brand new feature branch, Sonar throw an error saying that project does not exist. So one can assign a gate only after project has been created. In this case, i use a hack: manually create a project using Sonar Web API just before assigning a gate, and only then performing mvn sonar:sonar step. Here is creation of dummy new project:
def createNewProject(def config, def branch) {
String projectName = new XmlSlurper().parseText(readFile('pom.xml')).name as String
def url = "${config.sonarHost}/api/projects/create"
sh "curl -u ${config.sonarToken}: ${url} -d 'name=${projectName}&project=${projectKey()}&branch=${branch}'"
}
Next step is assigning a Gate for this dummy project:
def setSonarQualityGate(def config, def projectFullName, def gateId) {
def url = "${config.sonarHost}/api/qualitygates/select"
sh "curl -u ${config.sonarToken}: ${url} -d 'gateId=${gateId}&projectKey=${projectFullName}'"
}
And only after that I execute analysis itself:
def runSonarAnalysis(def config, def branch) {
echo "Run Sonar analysis"
sh "mvn sonar:sonar -Dsonar.host.url=${config.sonarHost} -Dsonar.branch=${branch}"
}