2 min read
How to integrate sonarqube with jenkins
  1. Install SonarQube Scanner plugin in jenkins
  2. Sonar Server > Administration > Security > Users > Token > Update Tokens > Generate
  3. Manage Jenkins > Configure System > SonarQube Servers
    • Check Environment variables injection
    • Add Sonarqube installations with Server authentication
      • Kind: Secret text, Secret: token generated in step 2
  4. Configure Jenkinsfile with withSonarQubeEnv step
node {
  stage('SCM') {
    git 'https://github.com/foo/bar.git'
  }
  stage('SonarQube analysis') {
    withSonarQubeEnv() { // Will pick the global server connection you have configured
      sh './gradlew sonarqube'
    }
  }
}
  1. Sonar Server > Administration > Configuration > Webhook
    • URL: http://jenkins:8080/sonarqube-webhook Secret to be empty
  2. Update Jenkinsfile with following
pipeline {
  agent { label 'linux' }
  options {
    buildDiscarder(logRotator(numToKeepStr: '5'))
  }
  stages {
    stage('SonarQube analysis') {
    withSonarQubeEnv() { // Will pick the global server connection you have configured
      sh './gradlew sonarqube'
    }
  }
    stage("Quality Gate") {
      steps {
        timeout(time: 2, unit: 'MINUTES') {
          waitForQualityGate abortPipeline: true
        }
      }
    }
  }
}

Reference

  1. How to Integrate SonarQube With Jenkins - YouTube
  2. java-web-app/Jenkinsfile-2 at sonar ยท darinpope/java-web-app
  3. SonarScanner for Jenkins | SonarQube Docs