Add SonarQube to jenkins job pipeline failed - jenkins

At the moment we try to add SonarQube analyzing to our jenkins job pipeline. But every time the build job failed with the message: "java.lang.NoSuchMethodError: **No such DSL method withSonarQubeEnv". We have reinstall all plugins and configurations. Nothing help.
So maybe someone of you can help us.
What we have done:
Do all steps of this tutorial from sonarqube: Analyzing with SonarQube Scanner for Jenkins
Install SonarQube Plugin 2.5
Add and configure SonarQube under Manage Jenkins > Configure System
Add SonarQube Scanner under Manage Jenkins > Global Tool Configuration
Add this code after the checkout of our project to the groovy file for the pipeline:
stage('SonarQube analysis')
// requires SonarQube Scanner 2.8+
def scannerHome = tool 'SonarQube Scanner 2.8';
withSonarQubeEnv('SonarQube 5.6.4') {
sh "${scannerHome}/bin/sonar-scanner"
}
Current Versions of our tools
Our Jenkins has the version 2.2
Our SonarQube Server has the version 5.6.4
The SonarQube Server run fine. We can analyzing our code over a local scanner.

I was trying to test with sonarqube 2.5 plugin but keep getting a different serialization error but have tested the below code which works fine and is without "withSonarQubeEnv" just in case you are looking for alternative:
stage('SonarQube analysis')
// requires SonarQube Scanner 2.8+
def scannerHome = tool 'SonarQube Scanner 2.8';
sh "${scannerHome}/bin/sonar-scanner"
PS: I'll try to test with sonarqube 2.5 plugin and let you know if I find something.

Related

Jenkins Unable to Detect the Ant

I have installed jenkins in windows machine, and i configured environment variable as well.
When i am checking ant -version in cmd i can able to get the response from the terminal "Apache Ant version 1.7.1 compiled on June 27 2008".
Jenkins Configuration
Ant plugin installed.
Ant home configured in jenkins
Ant config in jenkins
I am checking ant -version in pipeline script, but i am getting build failed in jenkins with following error message "ant' is not recognized as an internal or external command"
stage('studio'){
steps {
bat 'ant -version'
}
}
Can you please someone help on this issue.
You are mssing some details in the construction .. bat does not know about ant. See How to invoke Ant in a Jenkins Groovy Pipeline
def antVersion = 'Ant1.9.1'
withEnv( ["ANT_HOME=${tool antVersion}"] ) {
bat '%ANT_HOME%/bin/ant.bat target1 target2'`
}
Also, don't name it ANT_HOME, but something relevant like ant-1.7.1

Is there any way to convert maven project to pipeline in an automated way

Having some maven projects. I want to change it to a scripted pipeline in jenkins
To automate the following example you can use the Jenkins API via the groovy script console or an groovy system script to create you jobs programmatically.
Example for scripted Pipeline:
node{
stage ('Build') {
git url: 'https://github.com/cyrille-leclerc/multi-module-maven-project'
withMaven(
// Maven installation declared in the Jenkins "Global Tool Configuration"
maven: 'M3',
// Maven settings.xml file defined with the Jenkins Config File Provider Plugin
// Maven settings and global settings can also be defined in Jenkins Global Tools Configuration
mavenSettingsConfig: 'my-maven-settings',
mavenLocalRepo: '.repository') {
// Run the maven build
sh "mvn clean install"
} // withMaven will discover the generated Maven artifacts, JUnit Surefire & FailSafe & FindBugs reports...
}
}
You need the Pipeline Maven Plugin: https://wiki.jenkins.io/display/JENKINS/Pipeline+Maven+Plugin

No tool named SonarQube Scanner 2.8 found error

I followed these instructions to download the SonarQube Scanner plugin for Jenkins. I've configured these jenkins global settings for the SonarQube scanner correctly. The SonarQube server is setup and functioning properly.
https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline
But when the build runs, it produces this error: No tool named SonarQube Scanner 2.8 found.
I am using a Jenkins declarative pipeline script for pipeline build.
I am using Jenkins ver. 2.131. I am using "SonarQube Scanner for Jenkins" Plugin version 2.8.1. I believe the Jenkins server is a common linux flavor. I am NOT using any version of Maven, and don't require it to build my projects.
I figured the plugin installed the actual scanner files for me on Jenkins. Do I need to have some version of the scanner command files installed, beyond whatever the plugin provided me? Meaning, is there something other than the plugin that I need to install on by Jenkins server? I would hope the SonarQube plugin would give me everything I would need to run on Jenkins build.
Here's the relavent part of my script:
stages {
stage("SonarQube Analysis") {
agent any
steps {
script {
def scannerHome = tool 'SonarQube Scanner 2.8';
withSonarQubeEnv("foo") {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
}
Here is a screenshot of the global configuration:
I think you didn't add Scanner in Jenkins Global Tool Configuration. You can do it by doing the following steps:
click Manage Jenkins
choose Global Tool Configuration
scroll to SonarQube Scanner
add SonarQube Scanner 2.8
May be would be actual for maven's users
stage("SonarQube analysis") {
steps {
script {
def scannerHome = tool 'SonarQube Scanner';
withSonarQubeEnv('SonarQube Server') {
sh 'mvn clean package sonar:sonar'
}
}
}
}

sonar.projectbasedir not working in Jenkins

I have configured SonarRunner 2.3 in the Jenkins. In Execute SonarQube Scanner build process, I have updated my properties as
# Required metadata
sonar.projectKey=test
sonar.projectName=test
sonar.projectVersion=1.0
sonar.projectBaseDir=/opt/app/jenkins/var/lib/jenkins/jobs/test1-BuildJob/workspace
#Path to Source
sonar.sources=/opt/app/jenkins/var/lib/jenkins/jobs/test-BuildJob/workspace/report-api/src, /opt/app/jenkins/var/lib/jenkins/jobs/test1-BuildJob/workspace/test-dao/src, /opt/app/jenkins/var/lib/jenkins/jobs/test1-BuildJob/workspace/test-model/src, /opt/app/jenkins/var/lib/jenkins/jobs/test-BuildJob/workspace/test-frontend/src
My jenkins job runs in different workspace.
/opt/app/jenkins/var/lib/jenkins/jobs/TestJob/jobs/test-SonarAnalysis
How do i configure sonar to run the analysis in given sonar.projectbasedir?

Gradle tool in Jenkins Declarative Pipeline

I defined a Jenkins Declarative pipeline to CI/CD my project. I am using gradle as my build tool. However I don't want to use the Gradle Wrapper and check it int the VCS. So I planed on using the jenkins tools functionality as below so that I can update the version number if I need to in future. But it doesn't seem to work.
pipeline {
agent any
tools {
gradle "gradle-4.0"
}
stage("Compile") {
steps {
sh 'gradle project/build.gradle classes'
}
}
I get the error "script.sh: gradle: not found".
I tried to echo PATH and that doesn't contain the path of this autoinstalled gradle tool. Please help.
Looks like there is an issue on the gradle plugin for Jenkins on plugin version 1.26. Please see the link to the bug reported below.
https://issues.jenkins-ci.org/browse/JENKINS-42381

Resources