I've installed the opensource branch plugin on sonarqube 7.0.0 (the 1.0.1 release): https://github.com/msanez/sonar-branch-community
I've configured a pipeline step in Jenkins for a multibranch pipeline:
stage('Sonar Analyse'){
tools {
jdk 'ORACLE-JDK8-x86_64'
}
steps {
withSonarQubeEnv('SonarQube Test') {
dir('path') {
sh 'mvn -B sonar:sonar -Dsonar.branch.name=my-multi-branch'
}
}
}
}
After a run I can see the new branch in sonarqube. I can switch between the master and my multi-branch. While the master contains info about vulnerabilities, coverage, tests, code smells etc I can't see anything for my new branch:
We couldn't find any results matching selected criteria. Try to change
filters to get some results.
This is showing up when I click on my-multi-branch while all filters are reset. Am I missing some configuration in sonarqube/jenkins/maven?
I've switched between short and long living branches but same issue.
Have you tried using the "when" condition?
For example:
stage ('sonar-branch'){
when {
not {
branch 'master'
}
}
steps {
sh 'mvn -B sonar:sonar -Dsonar.branch=${env.BRANCH_NAME}'
}
}
Take a look at this link.
Related
In the following pipeline, I am trying to check out only develop branch to build the given project. How do I make sure that the pipeline only run develop, master and release branches?
Should I add separate stages for master branch and another for release branch. Instead I am trying to let this pipeline build only when there are changes in develop or master or release branches and ignore building for any other branches.
In Jenkins > Freestyle project > Source code management > Git > User can enter specific branches in Branch specifier. How can I implement similar one using pipeline?
pipeline {
agent any
tools {
maven "${mvnHome}"
jdk 'jdk8'
}
stages {
stage('Checkout project') {
steps {
git branch: 'develop',
credentialsId: 'someid',
url: 'https://project.git'
}
}
stage('build') {
steps {
sh '''
mvn clean deploy
'''
}
}
}
}
You can use a Multibranch Pipeline project and add at its Branch Sources → Git → Behaviours:
Filter by name (with regular expression)
A Java regular expression to restrict the names. Names that do not match the supplied regular expression will be ignored.
NOTE: this filter will be applied to all branch like things, including change requests
Filter by name (with wildcards)
Include
Space-separated list of name patterns to consider. You may use * as a wildcard; for example: master release*
NOTE: this filter will be applied to all branch like things, including change requests
Exclude
Space-separated list of name patterns to ignore even if matched by the includes list. For example: release alpha-* beta-*
NOTE: this filter will be applied to all branch like things, including change requests
Well, you can write conditions in groovy to do this using when statement. Something like this
stage('build'){
when {
expression {
env.BRANCH_NAME == 'develop' || env.BRANCH_NAME == 'master'
}
}
steps{
script{
sh 'mvn clean deploy '
}
}
}
I am trying to convert jenkins maven project to pipeline project, we have mvn clean install step and next violation plugin can someone help me How to include violation report in pipeline project (check style and findbugs)
In declarative style, using the new Warnings Next Generation plugin, you would do something like
pipeline {
agent any
stages {
... pre-conditions & other stuff previously handled by your jenkins maven job ...
stage('Build') {
steps {
withMaven {
sh 'mvn clean install'
}
}
}
... post-conditions previously handled your jenkins maven job ...
}
post {
always {
recordIssues(
enabledForFailure: true, aggregatingResults: true,
tools: [java(), checkStyle(pattern: 'checkstyle-result.xml', reportEncoding: 'UTF-8'), findBugs(pattern: 'findbugs.xml')]
)
}
}
}
See the pipeline documentation page for more details about syntax etc
I am interested in analysising my Jenkins builds via SonarQube. Initially, I have used the following code
stage('SonarCloud') {
steps {
withSonarQubeEnv('SonarQube') {
sh 'mvn clean package sonar:sonar '
}
}
}
However, I have asked here about how can I specify the quality gate that server uses for my analysis. From the answer provided, I have modified my code to look like this
stage('SonarCloud') {
steps {
withSonarQubeEnv('SonarQube') {
script{
def projectName = "Some-Exp"
// Creating a fresh project and placing it on the server - Works fine
sh "curl -u ${env.SONAR_AUTH_TOKEN} '${env.SONAR_HOST_URL}/api/projects/create' -d 'name=${projectName}&project=${projectName}&branch=${env.BRANCH_NAME}'"
// Specifying Quality Gate that to be used when performing our analysis - Does not quite work
sh "curl -u ${env.SONAR_AUTH_TOKEN} '${env.SONAR_HOST_URL}/api/qualitygates/select' -d 'gateId=2&projectKey=${projectName}'"
// Analysing our project - Creates the entirely new project, much like the initial code did
sh "mvn sonar:sonar -Dsonar.host.url=${env.SONAR_HOST_URL}"
}
}
}
}
The code creates and places a project on SonarQube server, but the said project still has a default quality gate, and it contains no analysis (in fact, current code creates an identical output to the one created by sh 'mvn clean package sonar:sonar ' line that I have used initially). There are no errors or anything. The problem is that the code does not do what I would like it to do.
This post mentioned that I need to add my project to profile group, before analysing it (which makes a lot sense). Tried to add sh "curl -u ${env.SONAR_AUTH_TOKEN} '${env.SONAR_HOST_URL}/api/qualityprofiles/add_project'" with some parameters but it didn't help that much.
I wonder what am I missing. I think the final line needs to be parametrised but I could not find anything that would make it work.
I have created a sample Maven project and run sonar analysis from Jenkins. Also, I have used the Web API to assign the QualityGate.
You can use the below Jenkinsfile as an example, to do sonar analysis.
Jenkinsfile
pipeline {
agent any
tools {
maven 'MAVEN_HOME1'
}
stages {
stage('Git') {
steps {
git credentialsId: 'gitlab-test', url: 'https://example.com/gitlab/repo1/simple-java-maven-app.git'
}
}
stage('Maven Install') {
steps {
sh "mvn install"
}
}
stage('Create Sonar Proejct') {
steps {
sh 'curl -X POST -u "admin:admin" "https://example.com/sonarqube/api/projects/create?name=stackoverflow&project=stackoverflow"'
}
}
stage('Set Quality Gate') {
steps {
sh 'curl -u "admin:admin" -X POST "https://example.com/sonarqube/api/qualitygates/select?projectKey=stackoverflow&gateId=10100"'
}
}
stage('Sonarqube Analysis') {
steps {
sh """mvn -U install sonar:sonar -Dsonar.host.url=https://example.com/sonarqube/ -Dsonar.login=7yha3f47967iuednd8cd -Dsonar.projectKey=stackoverflow -Dsonar.projectName=stackoverflow -Dsonar.sources=. -Dsonar.java.binaries=**/* -Dsonar.language=java -Dsonar.exclusions=src/test/java/com/mycompany/app/AppTest.java"""
}
}
}
}
Please find below the SonarQube Analysis Result and other screenshots, for your reference.
Screenshots:
Jenkins Console Output:
List of Available QualityGate:
Note: In the above image, "id":10040,"name":"SonarQube way" is the default QualityGate. I have used "id":10100,"name":"SASSonarQube way" for setting Quality Gate to analyze the project stackoverflow using Web API. All are marked in yellow
SonarQube Analysis
In above image, you can see the Quality Gate SASSonarQube way has been used to do sonar analysis. Marked in yellow
I'm trying to set up a Jenkins Declarative Pipeline with maven. So far I can get maven to run, but I can't get it to use my defined Maven Settings.xml.
pipeline{
agent any
tools{
maven 'Apache Maven 3.3'
// without mavenSettingsConfig, my settings.xml is not used. With it, this blows up
mavenSettingsConfig: 'Global Maven Settings'
jdk 'jdk9
}
stages {
stage('Preparation'){
steps{
//code checkout stuff here--this works fine
}
}
stage('Build'){
steps{
sh "mvn clean install -P foo"
}
}
}
}
The problem seems to be mavenSettingsConfig. Without that property, I can't figure out how to set the settings.xml, and my custom maven stuff doesn't work. (Profile foo, for example.) With the mavenSettingsConfig, it blows up:
BUG! exception in phase 'canonicalization' in source unit 'WorkflowScript' unexpected NullpointerException....
The documentation has a big TODO in it where it would provide an example for this! So how do I do it?
(Documentation TODO at https://wiki.jenkins.io/display/JENKINS/Pipeline+Maven+Plugin. It actually says "TODO provide a sample with Jenkins Declarative Pipeline")
my advice is to use the Config File Provider plugin: https://wiki.jenkins.io/display/JENKINS/Config+File+Provider+Plugin
With it, you define your config file once in Jenkins' "Config File Management" screen and then have code like this in your pipeline:
stage('Build'){
steps{
configFileProvider([configFile(fileId: 'my-maven-settings-dot-xml', variable: 'MAVEN_SETTINGS_XML')]) {
sh 'mvn -U --batch-mode -s $MAVEN_SETTINGS_XML clean install -P foo'
}
}
}
Hope it helps
you have to declare and maven installation in your jenkins
Managed Jenkins > Global Tools configuration and add maven installation named like M3.
declare a maven installation
After you have to registry your settings file :
manage jenkins > Managed files
And add your setting File
After this you can use the WithMaven function with your registry file like this:
steps {
withMaven(maven: 'M3', mavenSettingsConfig: 'mvn-setting-xml') {
sh "mvn clean install "
}
}
Also possible, to use the secret file credentials from Credentials Binding Plugin
Create a secret file in jenkins:
Then you can use this settings file like this
pipeline {
environment {
MVN_SET = credentials('maven_settings')
}
agent {
docker 'maven:3-alpine'
}
stages {
stage('mvn test settings') {
steps {
sh 'mvn -s $MVN_SET help:effective-settings'
}
}
}
}
I had this issue all you have to do is add this small piece of code in your line
def mvnSettings = 'Location of the file'
sh "mvn clean install --settings ${mvnSettings} -P foo"
So now whenever maven runs it will locate the settings.xml file in the PATH that you specified
P.S. its a maven command which you can use to run on command Line
Hope it helps :)
Combining the accepted answer of #Francois Marot and the link provided by the OP, we get:
pipeline {
stages {
stage ('Build') {
steps {
withMaven() {
bat 'mvn clean install'
}
}
}
}
}
This uses both the "Managed Files" plugin and the "Global Tool Configuration" plugin (Maven configuration, Maven installation, JDK installation) to specify the settings.xml file implicitly.
I am currently trying to implement pipeline in jenkins using jenkinsfile and i am executing a maven project on windows machine. I am creating a pipeline job in jenkins and i have checked in this file in my github repository and when i am running the job in jenkins , i am getting following error.
My jenkinsfile:
pipeline {
agent any
stages {
stage('Compile stage') {
steps {
maven(maven : 'Maven_3.5.2'){
bat "mvn clean compile"
}
}
}
stage('testing stage') {
steps {
maven(maven : 'Maven_3.5.2'){
bat "mvn test"
}
}
}
stage('deployment stage') {
steps {
maven(maven : 'Maven_3.5.2'){
bat "mvn deploy"
}
}
}
}
}
I am getting below error when i am running it through jenkins job-
Jenkins error:
java.lang.NoSuchMethodError: No such DSL method 'withMaven' found
among steps [archive, bat, build, catchError, checkout, deleteDir,
dir, dockerFingerprintFrom, dockerFingerprintRun, echo, emailext,
emailextrecipients, envVarsForTool, error, fileExists, getContext,
git, input, isUnix, library, libraryResource, load, mail, milestone,
node, parallel, powershell, properties, pwd, readFile, readTrusted,
resolveScm, retry, script, sh, sleep, stage, stash, step, svn,
timeout, timestamps, tm, tool, unarchive, unstash,
validateDeclarativePipeline, waitUntil, withContext, withCredentials,
withDockerContainer, withDockerRegistry, withDockerServer, withEnv,
wrap, writeFile, ws] or symbols [all, allOf, always, ant,
antFromApache, antOutcome, antTarget, any, anyOf, apiToken,
architecture, archiveArtifacts, artifactManager, authorizationMatrix,
batchFile, booleanParam, branch,
Any help?
This means you don't have withMaven as an available DSL method. Most of the time this means you don't have a plugin installed. In this case, the Pipeline Maven Integration plugin is required. https://plugins.jenkins.io/pipeline-maven/
Try this:
pipeline {
agent any
tools {
maven 'Maven_3.5.2'
}
stages {
stage('Compile stage') {
steps {
bat "mvn clean compile"
}
}
stage('testing stage') {
steps {
bat "mvn test"
}
}
stage('deployment stage') {
steps {
bat "mvn deploy"
}
}
}
}
Reference: https://jenkins.io/doc/book/pipeline/syntax/
On top of Rob Hales' answer, it is called "Pipeline Maven Integration Plugin" in Jenkins ver. 2.73.3 or later
You need to install all listed "pipeline" plugin and error no longer will be there.