Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

How to execute sonarqube scanner using jenkins pipeline?


SonarScanner: Command line


node {
  stage('SCM') {
    git 'https://github.com/foo/bar.git'
  }
  stage('SonarQube analysis') {
    def scannerHome = tool 'SonarScanner 4.0';
    withSonarQubeEnv('My SonarQube Server') { // If you have configured more than one global server connection, you can specify its name
      sh "${scannerHome}/bin/sonar-scanner"
    }
  }
}Code language: JavaScript (javascript)

SonarScanner for Gradle:


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'
    }
  }
}Code language: PHP (php)

SonarScanner for Maven:


node {
  stage('SCM') {
    git 'https://github.com/foo/bar.git'
  }
  stage('SonarQube analysis') {
    withSonarQubeEnv(credentialsId: 'f225455e-ea59-40fa-8af7-08176e86507a', installationName: 'My SonarQube Server') { // You can override the credential to be used
      sh 'mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.7.0.1746:sonar'
    }
  }
}Code language: PHP (php)

SonarScanner for .NET:


node {
  stage('SCM') {
    git 'https://github.com/foo/bar.git'
  }
  stage('Build + SonarQube analysis') {
    def sqScannerMsBuildHome = tool 'Scanner for MSBuild 4.6'
    withSonarQubeEnv('My SonarQube Server') {
      bat "${sqScannerMsBuildHome}\\SonarQube.Scanner.MSBuild.exe begin /k:myKey"
      bat 'MSBuild.exe /t:Rebuild'
      bat "${sqScannerMsBuildHome}\\SonarQube.Scanner.MSBuild.exe end"
    }
  }
}Code language: JavaScript (javascript)

pipeline {
  agent any
  stages {
    stage('SonarQube analysis') {
      steps {
        script {
          // requires SonarQube Scanner 2.8+
          scannerHome = tool 'SonarQube Scanner 2.8'
        }
        withSonarQubeEnv('SonarQube Scanner') {
          sh "${scannerHome}/bin/sonar-scanner"
        }
      }
    }
  }
}Code language: JavaScript (javascript)

The tool name “SonarQube Scanner 2.8” needs to match the “Name” field of a SonarQube Scanner Installation on the Global Tools Configuration page. The name used in the withSonarQubeEnv step needs to match the “Name” field of a SonarQube server defined on the Configure System page.


You're mixing Scripted Pipeline with Declarative Pipeline syntax.

While the snippet you posted from SonarQube documentation would work, you will need to adapt it since you're using Declarative (as indicated by the "Not a valid stage section definition" error).

Normally, you'd define a tools section in your Pipeline, but it looks like the SonarQube plugin doesn't support Declarative, nor does it add itself to the PATH.

Since you can't normally define variables in Declarative Pipeline, the script step has to be used to call the tool step and store the path to the installed tool. For example:

pipeline {
  agent any
  stages {
    stage('SonarQube analysis') {
      steps {
        script {
          // requires SonarQube Scanner 2.8+
          scannerHome = tool 'SonarQube Scanner 2.8'
        }
        withSonarQubeEnv('SonarQube Scanner') {
          sh "${scannerHome}/bin/sonar-scanner"
        }
      }
    }
  }
}
The tool name "SonarQube Scanner 2.8" needs to match the "Name" field of a SonarQube Installation on the Global Tools Configuration page. The name used in the withSonarQubeEnv step needs to match the "Name" field of a SonarQube server defined on the Configure System page.

If the SonarQube plugin did support Declarative, and added itself to PATH, the Pipeline could be a wee bit simpler:

pipeline {
  agent any
  stages {
    stage('SonarQube analysis') {
      tools {
        sonarQube 'SonarQube Scanner 2.8'
      }
      steps {
        withSonarQubeEnv('SonarQube Scanner') {
          sh 'sonar-scanner'
        }
      }
    }
  }
}Code language: PHP (php)

Example using declarative pipeline:


      pipeline {
        agent none
        stages {
          stage("build & SonarQube analysis") {
            agent any
            steps {
              withSonarQubeEnv('My SonarQube Server') {
                sh 'mvn clean package sonar:sonar'
              }
            }
          }
          stage("Quality Gate") {
            steps {
              timeout(time: 1, unit: 'HOURS') {
                waitForQualityGate abortPipeline: true
              }
            }
          }
        }
      }Code language: JavaScript (javascript)

Example using scripted pipeline:


      stage("build & SonarQube analysis") {
          node {
              withSonarQubeEnv('My SonarQube Server') {
                 sh 'mvn clean package sonar:sonar'
              }
          }
      }

      stage("Quality Gate"){
          timeout(time: 1, unit: 'HOURS') {
              def qg = waitForQualityGate()
              if (qg.status != 'OK') {
                  error "Pipeline aborted due to quality gate failure: ${qg.status}"
              }
          }
      }Code language: JavaScript (javascript)


Please use below code to run the sonar-scanner on windows


node {    
       stage('SonarQube analysis') {
       // requires SonarQube Scanner 2.8+
       def scannerHome = tool 'SONAR_RUNNER';
       withSonarQubeEnv('SonarQube') {
            bat "\"${scannerHome}\\bin\\sonar-scanner.bat\""
       }
}Code language: JavaScript (javascript)

SonarQube integration with Jenkins Pipeline


node(label:'master') {
  try{  
    stage('Static Analysis') {
      withSonarQubeEnv('SonarQube1') 
      {
        bat 'mvn clean package sonar:sonar
   	echo 'Static Analysis Completed' 
      }
   
    stage("Quality Gate"){
      timeout(time: 1, unit: 'HOURS') 
      {
        waitForQualityGate abortPipeline: true
        def qg= waitForQualityGate()
        if (qg.status!= 'OK'){
          error "Pipeline aborted due to quality gate failure: ${qg.status}"
        }
      }         
      echo 'Quality Gate Passed' 
    }
  } 
}Code language: PHP (php)

SonarQube Scans in Jenkins Declarative Pipeline using SonarScanner


stage('SonarCloud') {
  environment {
    SCANNER_HOME = tool 'SonarQubeScanner'
    ORGANIZATION = "igorstojanovski-github"
    PROJECT_NAME = "igorstojanovski_jenkins-pipeline-as-code"
  }
  steps {
    withSonarQubeEnv('SonarCloudOne') {
        sh '''$SCANNER_HOME/bin/sonar-scanner -Dsonar.organization=$ORGANIZATION \
        -Dsonar.java.binaries=build/classes/java/ \
        -Dsonar.projectKey=$PROJECT_NAME \
        -Dsonar.sources=.'''
    }
  }
}
stage("Quality Gate") {
  steps {
    timeout(time: 1, unit: 'MINUTES') {
        waitForQualityGate abortPipeline: true
    }
  }
}Code language: JavaScript (javascript)

stage('SonarQube') {
  environment {
    SCANNER_HOME = tool 'sonar scanner jenkins'
    ORGANIZATION = "poc.net"
    PROJECT_NAME = "org.sonarqube:poc.net"
  }
  steps {
    withSonarQubeEnv('sonarqube') {
        bat "${SCANNER_HOME}/bin/sonar-scanner -Dsonar.organization=${ORGANIZATION} \
        -Dsonar.java.binaries=compiled \
        -Dsonar.projectKey=${PROJECT_NAME} \
        -Dsonar.sources=POCStudentCrud"
    }
  }
}
Code language: PHP (php)

stage('SonarCloud') {
  environment {
    SCANNER_HOME = tool 'SonarQubeScanner'
    ORGANIZATION = "igorstojanovski-github"
    PROJECT_NAME = "igorstojanovski_jenkins-pipeline-as-code"
  }
  steps {
    withSonarQubeEnv('SonarCloudOne') {
        sh '''$SCANNER_HOME/bin/sonar-scanner -Dsonar.organization=$ORGANIZATION \
        -Dsonar.java.binaries=build/classes/java/ \
        -Dsonar.projectKey=$PROJECT_NAME \
        -Dsonar.sources=.'''
    }
  }
}Code language: PHP (php)

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Top 10 Subscription Management Software Tools in 2026: Features, Pros, Cons & Comparison

Introduction Subscription management software is designed to streamline and optimize the process of managing recurring billing, customer subscriptions, and related business operations. In 2026, with the rapid…

Read More

Top 10 AI Data Integration Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, AI data integration tools are pivotal for businesses navigating the complexities of modern data ecosystems. These tools combine artificial intelligence with data integration processes…

Read More

Top 10 Fleet Management Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, the logistics and transportation industries are evolving rapidly, and managing a fleet of vehicles has never been more complex. Fleet management software has become…

Read More

Top 10 AI Academic Plagiarism Checkers Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, AI academic plagiarism checkers have become indispensable tools for students, educators, researchers, and institutions striving to uphold academic integrity. With the rise of AI-generated…

Read More

Top 10 Travel Management Software Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, travel management software (TMS) has become a crucial tool for businesses, travel agencies, and frequent travelers. These tools automate the booking, tracking, and management…

Read More

Top 10 No-Code Platforms Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, no-code platforms have become essential for businesses and individuals looking to build powerful applications, websites, and automations without the need for programming knowledge. These…

Read More
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Chandra
Chandra
3 years ago

how to give the Sonarqube url instead of ‘SonarCloudOne’ configured in global configuration

1
0
Would love your thoughts, please comment.x
()
x