node {

  stage('SonarQube analysis') {
    def scannerHome = tool 'sonarscanner';
    withSonarQubeEnv('sonar') { 
      sh "${scannerHome}/bin/sonar-scanner"
    }
  }
  stage("Quality Gate"){
  timeout(1) { // Just in case something goes wrong, pipeline will be killed after a timeout
    def qg = waitForQualityGate('sonar') // Reuse taskId previously collected by withSonarQubeEnv
    if (qg.status != 'OK') {
      error "Pipeline aborted due to quality gate failure: ${qg.status}"
        }
    }
  }
}