Jenkins Pipeline SonarQube key name - jenkins

When building a multibranch pipeline, I send each of my projects to SonarQube using the SonarQube plugin like so:
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr:'20'))
timeout(time: 30, unit: 'MINUTES')
}
tools {
maven 'Maven 3.3.9'
jdk 'JDK 1.8'
}
stages {
stage('Checkout') {
steps {
echo 'Checking out..'
checkout scm
echo "My branch is: ${env.BRANCH_NAME}"
}
}
stage('Build') {
steps {
echo 'Building..'
bat 'mvn clean verify -P!local'
}
}
stage('SonarQube analysis'){
steps{
echo 'Analysing...'
withSonarQubeEnv('SonarQube') {
bat 'mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar'
}
}
}
}
}
It works fine, but one thing I need it to do is to change the name of the project in SonarQube to be projectName/builtBranch instead of just the project name. Is there a way I can do this using the pipeline?

This doesn't seem to be a Jenkins issue; rather you should be able to set the various sonar.* properties (e.g. sonar.projectName or sonar.branch) when running the Maven plugin.
The documentation seems to have a full list:
https://docs.sonarqube.org/display/SONAR/Analysis+Parameters

Related

How to use javadoc in a a Jenkinfile pipeline

I would like to use this: Publish JavaDoc on Jenkins with maven in a scripted pipeline
For example to publish a JavaDoc: you can try something like:
// Define the pipeline
pipeline {
agent any
// Stage to compile the project and generate the JavaDoc
stages {
stage('Compile & generate JavaDoc') {
steps {
// Compile the project
sh 'mvn compile'
// Generate the JavaDoc
sh 'mvn javadoc:javadoc'
}
}
}
// Stage to publish the JavaDoc
stages {
stage('Publish JavaDoc') {
steps {
// Publish the JavaDoc
sh 'mvn javadoc:jar'
}
}
}
}

is there way to we can validate a pipeline file before jenkins starts executing the pipeline

I would like to ensure that a particular stage is not skipped from a JenkinsFile. For example i would like to ensure that dev has not skipped the Code scanning in the pipeline. This should happen before the pipeline is executed by Jenkins.
I could manually check this via looking for the "Test Results" on the page that I have included the image for below. This indicates that the job has published Test Results to the JUnit plugin.
If I were to write a Jenkinsfile, it might look something like this. But it is possible to attach these to the JUnit pipeline via manual methods as well:
pipeline {
agent any
stages {
stage('Compile') {
steps {
// Login to Repository
configFileProvider([configFile(fileId: 'nexus_maven_configuration', variable: 'MAVEN_SETTINGS')]) {
sh 'mvn -s $MAVEN_SETTINGS compile'
}
}
}
stage('Test') {
steps {
configFileProvider([configFile(fileId: 'nexus_maven_configuration', variable: 'MAVEN_SETTINGS')]) {
sh 'mvn -s $MAVEN_SETTINGS test'
}
}
}
}
post {
always {
junit '**/target/surefire-reports/*.xml'
archive 'target/*.jar'
}
}
}

Modify env.BRANCH_NAME variable with branch-env.BRANCH_NAME in jenkinsfile for a multibranch pipeline project

I have created a multibranch pipeline project and thus created jenkinsfile and put that in dev branch.
In one of the stage, I have to run mvn sonar:sonar -Dsonar.scm.branch=branch-${env.BRANCH_NAME} but it's giving error as bad substitution branch-${env.BRANCH_NAME}.
I need branch-${env.BRANCH_NAME} as a branch-name so that at sonar i can see branch-dev at branches section in sonar dashboard.
if i use mvn sonar:sonar -Dsonar.scm.branch=env.BRANCH_NAME then it provides output but it will act as short-lived branch in sonar. but at sonar we want branch as long-lived branch.
!/usr/bin/env groovy
pipeline {
agent { label 'ol73_slave-jdk8u192-git' }
options {
timestamps()
timeout(time: 2, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '10'))
disableConcurrentBuilds()
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Unit Test and Code Scan') {
steps {
echo "*****JUnit Tests, JaCoCo Code Coverage, & SonarQube Code Qualiy Scan*****"
withMaven(jdk: 'jdk8_u192', maven: 'maven-3.3.9', mavenSettingsConfig: '79ecf9bd-8cbc-4d5e-b7d1-200241e16b52') {
sh '''
cd DARC
mvn clean package sonar:sonar -Dsonar.host.url=***** -Dsonar.login=******* -Dsonar.exclusions=file:**/src/test/** -B -Pcoverage -Dsonar.branch.name=branch-${env.BRANCH_NAME}
'''
}
}
}
}
}
In groovy Strings are encapsulated in single quotes '' and GStrings in double quotes ""
In order to do interpolation you need to be using GStrings. In your example, this should simply be
sh """
cd DARC
mvn clean package sonar:sonar -Dsonar.host.url=***** -Dsonar.login=******* -Dsonar.exclusions=file:**/src/test/** -B -Pcoverage -Dsonar.branch.name=branch-${env.BRANCH_NAME}
"""

using msbuild in a Jenkins multi branch project

How can I use MSBuild in a Jenkins multi-branch project?
Here's my Jenkinsfile:
pipeline {
agent any
stages {
stage('restore') {
steps {
sh "echo 'TODO RUN TEST'"
}
}
stage('build') {
steps {
bat "\"${tool 'MSBuild'}\" .\\src\\MySollutionName.sln /p:Configuration=Release /p:Platform=\"Any CPU\" /p:ProductVersion=1.0.0.${env.BUILD_NUMBER}"
}
}
stage('test') {
steps {
sh "echo 'TODO RUN TEST'"
}
}
}
}
But I'm getting an error message:
No tool named MSBuild found
Is it possible to use MSBuild in Jenkins multi-branch project?
You need to first tell Jenkins what the MSBuild tool is:
https://wiki.jenkins.io/display/JENKINS/Custom+Tools+Plugin
Have you installed the MSBuild plugin?
https://wiki.jenkins.io/display/JENKINS/MSBuild+Plugin

Clone Jenkinsfile by changing default workspace from master to slave

I am working on Jenkins Pipeline Script and I have checked-in my jenkinsfile in Git repository and I need to clone to local work space. But by default its cloning to master (Unix) work space but I need it in slave (Windows) work space.
Is there any plugins to change the default Pipeline Script from SCM work space location to slave?
You can do something like this
pipeline {
agent any
options {
skipDefaultCheckout()
}
stages {
stage('checkout') {
steps {
node('windows') {
checkout scm
}
}
}
}
}
OR
pipeline {
agent 'windows'
stages {
stage('build') {
steps {
// build
}
}
}
}
In my case, the following pipeline configuration skips the default checkout on master, and checkout my code just on Jenkins slave.
node {
docker.image('php7.1.30:1.0.0').inside {
skipDefaultCheckout() // this avoid the checkout on master
stage("checkout"){
checkout scm // here the checkout happens on slave node
}
stage('NPM Install'){
sh label: 'NPM INSTALL', script: "npm install"
sh label: 'GRUNT INSTALL', script: "npm install -g grunt-cli"
}
stage('Executing grunt') {
sh label: 'GRUNT DEFAULT', script: "grunt default"
}
}
}

Resources