Unable to execute sonarscanner analysis in jenkins CI/CD pipeline

Hi,

I am trying to build a CI/CD pipeline( GCP used) but I am getting struck. I have followed the steps and made the settings as explained here:

However, the build is failing at sonar quality check stage on Jenkins.

jenkins code:

pipeline{
    agent any 
    environment{
        VERSION = "${env.BUILD_ID}"
    }
    stages{
        stage("sonar quality check"){
            agent {
                docker {
                    image 'openjdk:11'
                }
            }
            steps{
                script{
                    withSonarQubeEnv(credentialsId: 'sonar-token') {
                            sh 'chmod +x gradlew'
                            sh './gradlew sonarqube'
                            sh  'java -version'
                    }

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

                }  
            }
        }		 
	}
}

Jenkins console output :

Jenkins does not seem to be running inside a container
$ docker run -t -d -u 1001:1002 -w /var/lib/jenkins/workspace/javaapplication@2 -v /var/lib/jenkins/workspace/javaapplication@2:/var/lib/jenkins/workspace/javaapplication@2:rw,z -v /var/lib/jenkins/workspace/javaapplication@2@tmp:/var/lib/jenkins/workspace/javaapplication@2@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** openjdk:11 cat
$ docker top 2a18574dd0cbc94634f9a16aa6e5ddcde66c9ffb453474c92bb0a4f6d2b9e32d -eo pid,comm
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] withSonarQubeEnv
Injecting SonarQube environment variables using the configuration: sonarserver
[Pipeline] {
[Pipeline] sh
+ chmod +x gradlew
[Pipeline] sh
+ ./gradlew sonarqube
Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could not be reused, use --status for details
> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :compileTestJava UP-TO-DATE

> Task :sonarqube FAILED
SonarQube server [http://34.125.91.25:9000] can not be reached

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':sonarqube'.
> Unable to execute SonarScanner analysis

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.1.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 19s
4 actionable tasks: 1 executed, 3 up-to-date
[Pipeline] }
WARN: Unable to locate 'report-task.txt' in the workspace. Did the SonarScanner succeed?
[Pipeline] // withSonarQubeEnv
[Pipeline] }
[Pipeline] // script
[Pipeline] }
$ docker stop --time=1 2a18574dd0cbc94634f9a16aa6e5ddcde66c9ffb453474c92bb0a4f6d2b9e32d
$ docker rm -f 2a18574dd0cbc94634f9a16aa6e5ddcde66c9ffb453474c92bb0a4f6d2b9e32d
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

Any help or input will be appreciated.

Thanks

Hi,

Welcome to the community!

There’s actually a pretty clear cause in your log:

You need to take a look at the ability to connect to your SonarQube server.

 
HTH,
Ann

Thanks for your reply. I have tried again improvising what you have suggested. However, there seems to be a issue. Here is the console output:

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:sonarqube’.

Unable to execute SonarScanner analysis
BUILD FAILED in 15s
4 actionable tasks: 1 executed, 3 up-to-date
[Pipeline] }
WARN: Unable to locate ‘report-task.txt’ in the workspace. Did the SonarScanner succeed?
[Pipeline] // withSonarQubeEnv
[Pipeline] }
[Pipeline] // script
[Pipeline] }
$ docker stop --time=1 d798abd706d07821b3e3eacc76931f5c46b7044f41ec42f2b235560abf0b7783
$ docker rm -f d798abd706d07821b3e3eacc76931f5c46b7044f41ec42f2b235560abf0b7783
ERROR: script returned exit code 1
Finished: FAILURE

Hi,

That was part of the console last time too. But before that was the error about not being able to contact the SonarQube server. You’re going to need to fix that first.

 
Ann

Hi Ann, thanks for the input I have fixed that error and double checked everything. Is there anything else, you would like to suggest, I am still getting the same output

Hi,

Please provide full analysis logs.

 
Ann

Hi,

Please find the full analysis logs. I have double checked everything end to end once again

Started by user [admin](http://34.125.183.65:8080/user/admin)


[Pipeline] Start of Pipeline

[Pipeline] node

Running on Jenkins in /var/lib/jenkins/workspace/javaapplication

[Pipeline] {

[Pipeline] stage

[Pipeline] { (Declarative: Checkout SCM)

[Pipeline] checkout

Selected Git installation does not exist. Using Default

The recommended git tool is: NONE

No credentials specified

 > git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/javaapplication/.git # timeout=10

Fetching changes from the remote Git repository

 > git config remote.origin.url https://github.com/1992prashant/CICD_Java_gradle_application.git # timeout=10


 > git --version # timeout=10

 > git --version # 'git version 2.25.1'

 > git fetch --tags --force --progress -- https://github.com/1992prashant/CICD_Java_gradle_application.git +refs/heads/*:refs/remotes/origin/* # timeout=10

 > git rev-parse refs/remotes/origin/devops^{commit} # timeout=10

Checking out Revision b614b2ee6d7cb888e883cd9d412ff8a26fca7bdf (refs/remotes/origin/devops)

 > git config core.sparsecheckout # timeout=10

 > git checkout -f b614b2ee6d7cb888e883cd9d412ff8a26fca7bdf # timeout=10

Commit message: "made changes to sonarqube block"

 > git rev-list --no-walk b614b2ee6d7cb888e883cd9d412ff8a26fca7bdf # timeout=10

[Pipeline] }

[Pipeline] // stage

[Pipeline] withEnv

[Pipeline] {

[Pipeline] withEnv

[Pipeline] {

[Pipeline] stage

[Pipeline] { (sonar quality check)

[Pipeline] node

Running on Jenkins in /var/lib/jenkins/workspace/javaapplication@2

[Pipeline] {

[Pipeline] checkout

Selected Git installation does not exist. Using Default

The recommended git tool is: NONE

No credentials specified

 > git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/javaapplication@2/.git # timeout=10

Fetching changes from the remote Git repository

 > git --version # timeout=10

 > git --version # 'git version 2.25.1'

 > git fetch --tags --force --progress -- https://github.com/1992prashant/CICD_Java_gradle_application.git +refs/heads/*:refs/remotes/origin/* # timeout=10

 > git rev-parse refs/remotes/origin/devops^{commit} # timeout=10

Checking out Revision b614b2ee6d7cb888e883cd9d412ff8a26fca7bdf (refs/remotes/origin/devops)

 > git config core.sparsecheckout # timeout=10

 > git checkout -f b614b2ee6d7cb888e883cd9d412ff8a26fca7bdf # timeout=10

Commit message: "made changes to sonarqube block"

[Pipeline] withEnv

[Pipeline] {

[Pipeline] isUnix

[Pipeline] withEnv

[Pipeline] {

[Pipeline] sh

+ docker inspect -f . openjdk:11

.

[Pipeline] }

[Pipeline] // withEnv

[Pipeline] withDockerContainer

Jenkins does not seem to be running inside a container

$ docker run -t -d -u 1001:1002 -w /var/lib/jenkins/workspace/javaapplication@2 -v /var/lib/jenkins/workspace/javaapplication@2:/var/lib/jenkins/workspace/javaapplication@2:rw,z -v /var/lib/jenkins/workspace/javaapplication@2@tmp:/var/lib/jenkins/workspace/javaapplication@2@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** openjdk:11 cat

$ docker top babe4978d755b3441b32a462ab6c56713c03fd2675172a207706644beb8c3198 -eo pid,comm

[Pipeline] {

[Pipeline] script

[Pipeline] {

[Pipeline] withSonarQubeEnv

Injecting SonarQube environment variables using the configuration: sonarserver

[Pipeline] {

[Pipeline] sh

+ chmod +x gradlew

[Pipeline] sh

+ ./gradlew sonarqube

Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could not be reused, use --status for details

> Task :compileJava UP-TO-DATE

> Task :processResources UP-TO-DATE

> Task :classes UP-TO-DATE

> Task :compileTestJava UP-TO-DATE

> Task :sonarqube FAILED

FAILURE: Build failed with an exception.

* What went wrong:

Execution failed for task ':sonarqube'.

> Unable to execute SonarScanner analysis

* Try:

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.


Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.1.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 14s

4 actionable tasks: 1 executed, 3 up-to-date

[Pipeline] }

WARN: Unable to locate 'report-task.txt' in the workspace. Did the SonarScanner succeed?

[Pipeline] // withSonarQubeEnv

[Pipeline] }

[Pipeline] // script

[Pipeline] }

$ docker stop --time=1 babe4978d755b3441b32a462ab6c56713c03fd2675172a207706644beb8c3198

$ docker rm -f babe4978d755b3441b32a462ab6c56713c03fd2675172a207706644beb8c3198

[Pipeline] // withDockerContainer

[Pipeline] }

[Pipeline] // withEnv

[Pipeline] }

[Pipeline] // node

[Pipeline] }

[Pipeline] // stage

[Pipeline] }

[Pipeline] // withEnv

[Pipeline] }

[Pipeline] // withEnv

[Pipeline] }

[Pipeline] // node

[Pipeline] End of Pipeline

ERROR: script returned exit code 1

Finished: FAILURE

Hi,

This only says that the ‘sonarqube’ task failed. It doesn’t give the analysis logs. We need to see the output from the sonarqube task.

 
Ann

@ganncamp Facing the exact same issue, I was able to curl & ping from the Jenkins server to sonarqube server. I am exactly using the same code which Prashant is using above.

Please let me know if you need any other additional info ?

Thanks

Hi,

Welcome to the community!

There’s a lot going on in this thread, and none of it definitive to my mind. Please start a new thread and provide all your details.

 
Thx,
Ann

Thank you, I have opened up a new thread.

https://community.sonarsource.com/t/sonarscanner-analysis-in-jenkins-ci-cd-pipeline/60180