|
| 1 | +#!/usr/bin/env groovy |
| 2 | +import jenkins.model.* |
| 3 | +import groovy.json.JsonSlurper |
| 4 | +import hudson.plugins.sonar.SonarInstallation |
| 5 | +import hudson.plugins.sonar.SonarRunnerInstallation |
| 6 | +import hudson.plugins.sonar.SonarRunnerInstaller |
| 7 | +import hudson.plugins.sonar.model.TriggersConfig |
| 8 | +import hudson.tools.InstallSourceProperty |
| 9 | + |
| 10 | +import java.util.logging.Level |
| 11 | +import java.util.logging.Logger |
| 12 | + |
| 13 | +final def LOG = Logger.getLogger("LABS") |
| 14 | + |
| 15 | +def disableSonar = System.getenv("DISABLE_SONAR"); |
| 16 | +if(disableSonar != null && disableSonar.toUpperCase() == "TRUE") { |
| 17 | + LOG.log(Level.INFO, 'Skipping SonarQube configuration') |
| 18 | + return |
| 19 | +} |
| 20 | + |
| 21 | +LOG.log(Level.INFO, 'Configuring SonarQube') |
| 22 | +def sonarConfig = Jenkins.instance.getDescriptor('hudson.plugins.sonar.SonarGlobalConfiguration') |
| 23 | + |
| 24 | +def sonarHost = System.getenv("SONARQUBE_URL"); |
| 25 | +if(sonarHost == null) { |
| 26 | + //default |
| 27 | + sonarHost = "http://sonarqube:9000" |
| 28 | +} |
| 29 | + |
| 30 | +def tokenName = 'Jenkins' |
| 31 | + |
| 32 | +// Make a POST request to delete any existing admin tokens named "Jenkins" |
| 33 | +LOG.log(Level.INFO, 'Delete existing SonarQube Jenkins token') |
| 34 | +def revokeToken = new URL("${sonarHost}/api/user_tokens/revoke").openConnection() |
| 35 | +def message = "name=Jenkins&login=admin" |
| 36 | +revokeToken.setRequestMethod("POST") |
| 37 | +revokeToken.setDoOutput(true) |
| 38 | +revokeToken.setRequestProperty("Accept", "application/json") |
| 39 | +def authString = "admin:admin".bytes.encodeBase64().toString() |
| 40 | +revokeToken.setRequestProperty("Authorization", "Basic ${authString}") |
| 41 | +revokeToken.getOutputStream().write(message.getBytes("UTF-8")) |
| 42 | +def rc = revokeToken.getResponseCode() |
| 43 | + |
| 44 | +// Create a new admin token named "Jenkins" and capture the value |
| 45 | +LOG.log(Level.INFO, 'Generate new auth token for SonarQube/Jenkins integration') |
| 46 | +def generateToken = new URL("${sonarHost}/api/user_tokens/generate").openConnection() |
| 47 | +message = "name=${tokenName}&login=admin" |
| 48 | +generateToken.setRequestMethod("POST") |
| 49 | +generateToken.setDoOutput(true) |
| 50 | +generateToken.setRequestProperty("Content-Type", "application/x-www-form-urlencoded") |
| 51 | +generateToken.setRequestProperty("Authorization", "Basic ${authString}") |
| 52 | +generateToken.getOutputStream().write(message.getBytes("UTF-8")) |
| 53 | +rc = generateToken.getResponseCode() |
| 54 | + |
| 55 | +def token = null |
| 56 | + |
| 57 | +if (rc == 200) { |
| 58 | + LOG.log(Level.INFO, 'Successfully generated SonarQube auth token') |
| 59 | + def jsonBody = generateToken.getInputStream().getText() |
| 60 | + def jsonParser = new JsonSlurper() |
| 61 | + def data = jsonParser.parseText(jsonBody) |
| 62 | + token = data.token |
| 63 | + |
| 64 | + // Add the SonarQube server config to Jenkins |
| 65 | + SonarInstallation sonarInst = new SonarInstallation( |
| 66 | + "sonar", sonarHost, token, "", "", new TriggersConfig(), "") |
| 67 | + sonarConfig.setInstallations(sonarInst) |
| 68 | + sonarConfig.setBuildWrapperEnabled(true) |
| 69 | + sonarConfig.save() |
| 70 | + |
| 71 | + // Sonar Runner |
| 72 | + // Source: http://pghalliday.com/jenkins/groovy/sonar/chef/configuration/management/2014/09/21/some-useful-jenkins-groovy-scripts.html |
| 73 | + def inst = Jenkins.getInstance() |
| 74 | + |
| 75 | + def sonarRunner = inst.getDescriptor("hudson.plugins.sonar.SonarRunnerInstallation") |
| 76 | + |
| 77 | + def installer = new SonarRunnerInstaller("3.0.3.778") |
| 78 | + def prop = new InstallSourceProperty([installer]) |
| 79 | + def sinst = new SonarRunnerInstallation("sonar-scanner-tool", "", [prop]) |
| 80 | + sonarRunner.setInstallations(sinst) |
| 81 | + |
| 82 | + sonarRunner.save() |
| 83 | + |
| 84 | + LOG.log(Level.INFO, 'SonarQube configuration complete') |
| 85 | +} else { |
| 86 | + LOG.log(Level.INFO, "Request failed: ${rc}") |
| 87 | + LOG.log(Level.INFO, generateToken.getErrorStream().getText()) |
| 88 | +} |
0 commit comments