Created
February 11, 2019 21:52
-
-
Save jvenkat255/f32a98db8e02a1fa1dbea81c28789d54 to your computer and use it in GitHub Desktop.
jenkinsfile and sonarqube
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node { | |
echo "=======================================" | |
echo "JENKINS_HOME = ${env.JENKINS_HOME}" | |
echo "JOB_NAME = ${env.JOB_NAME}" | |
echo "REPO_GIT = " + REPO_GIT | |
echo "DEFAULT_GIT_BRANCH = "+ DEFAULT_GIT_BRANCH | |
echo "SONAR_SERVER = "+ SONAR_SERVER | |
echo "=======================================" | |
def sonarInstance=hudson.plugins.sonar.SonarInstallation.get(SONAR_SERVER).name; | |
def sonarServerUrl=hudson.plugins.sonar.SonarInstallation.get(SONAR_SERVER).serverUrl; | |
def sonarDatabaseUrl=hudson.plugins.sonar.SonarInstallation.get(SONAR_SERVER).databaseUrl; | |
def sonarDatabaseLogin=hudson.plugins.sonar.SonarInstallation.get(SONAR_SERVER).databaseLogin; | |
def sonarDatabasePassword=hudson.plugins.sonar.SonarInstallation.get(SONAR_SERVER).databasePassword; | |
echo "sonarInstance: "+ sonarInstance | |
echo "sonarServerUrl: "+ sonarServerUrl | |
echo "sonarDatabaseUrl: "+ sonarDatabaseUrl | |
stage 'Checkout' | |
git url: REPO_GIT , branch: DEFAULT_GIT_BRANCH | |
def mvnHome = tool 'M311' | |
stage 'Build' | |
// Run the maven build | |
sh "${mvnHome}/bin/mvn org.jacoco:jacoco-maven-plugin:0.7.4.201502262128:prepare-agent clean verify -Dmaven.test.failure.ignore=true" | |
stage 'ccj-deps-matrix' | |
sh "${mvnHome}/bin/mvn fr.generali.ccj.maven.plugin:ccj-deps-matrix:0.0.8:deps-matrix -DoutputPath=${env.JENKINS_HOME}/userContent/deps-matrix -DappName=${env.JOB_NAME}" | |
stage 'Sonar' | |
sh ''' | |
GIT_BRANCH=`git rev-parse --abbrev-ref HEAD` | |
SONAR_BRANCH=`printf '%s' $GIT_BRANCH | sed s/[^0-9a-zA-Z:_.\\-]/'_'/g` | |
echo "GIT_BRANCH=${GIT_BRANCH}" > my-build-vars.properties | |
echo "SONAR_BRANCH=${SONAR_BRANCH}" >> my-build-vars.properties | |
''' | |
def props = getBuildProperties("my-build-vars.properties") | |
echo "my-build-vars.properties=${props}" | |
def sonarBranchParam = getSonarBranchParameter(props.getProperty('SONAR_BRANCH')) | |
sh "${mvnHome}/bin/mvn sonar:sonar ${sonarBranchParam} -Dsonar.jdbc.url='${sonarDatabaseUrl}' -Dsonar.host.url=${sonarServerUrl} -Dsonar.jdbc.username=${sonarDatabaseLogin} -Dsonar.jdbc.password=${sonarDatabasePassword}" | |
} | |
def getSonarBranchParameter(branch) { | |
sonarBranchParam = "" | |
if ("develop".equals(branch)) { | |
echo "branch is develop, sonar.branch not mandatory" | |
} else { | |
echo "branch is not develop" | |
sonarBranchParam="-Dsonar.branch=" + branch | |
} | |
return sonarBranchParam | |
} | |
def Properties getBuildProperties(filename) { | |
def properties = new Properties() | |
properties.load(new StringReader(readFile(filename))) | |
return properties | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment