-
-
Save naviat/279852fa79682b202b3441b7f6a710eb to your computer and use it in GitHub Desktop.
Jenkinsfile - Test - Package - Sonar - Quality Gate - Artifactory
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
pipeline { | |
agent any | |
tools { | |
maven 'Maven_3.6.2' | |
jdk 'Java_1.8u161' | |
} | |
environment { | |
GIT_VERSION = sh (returnStdout: true, script: 'git rev-parse HEAD | cut -c 1-10').trim() | |
APP_GROUP = sh (returnStdout: true, script: 'grep -im1 "<groupId>" pom.xml | sed "s/<groupId>//g" | sed "s/<\\/groupId>//g" | xargs | tr -d "\n"') | |
APP_NAME = sh (returnStdout: true, script: 'grep -im1 "<artifactId>" pom.xml | sed "s/<artifactId>//g" | sed "s/<\\/artifactId>//g" | xargs | tr -d "\n"') | |
APP_VERSION = sh (returnStdout: true, script: 'grep -im1 "<version>" pom.xml | sed "s/<version>//g" | sed "s/<\\/version>//g" | xargs | tr -d "\n"') | |
PROJECT_NAME = "${APP_GROUP}.${APP_NAME}" | |
PROJECT_KEY = "${APP_NAME}" | |
SONAR_URL = "http://jenkins:9000" | |
} | |
options { | |
gitLabConnection('Gitlab') | |
gitlabBuilds(builds: ['Test', 'Package', 'Sonar', 'Quality Gate', 'Artifactory']) | |
} | |
triggers { | |
gitlab(triggerOnPush: true, triggerOnMergeRequest: false, branchFilterType: 'NameBasedFilter', includeBranchesSpec: "master", secretToken: 'GITHUB_TOKEN') | |
} | |
stages { | |
stage('Test'){ | |
steps{ | |
gitlabCommitStatus(name: 'Test', connection: gitLabConnection('Gitlab')){ | |
echo "#####################################" | |
echo "########### MAVEN TEST ############" | |
echo "#####################################" | |
sh 'mvn clean test' | |
} | |
} | |
} | |
stage('Package'){ | |
steps{ | |
gitlabCommitStatus(name: 'Build', connection: gitLabConnection('Gitlab')){ | |
echo "#####################################" | |
echo "########### MAVEN Package #########" | |
echo "#####################################" | |
sh("mvn install -U -DskipTests") | |
} | |
} | |
} | |
stage('Sonar'){ | |
steps{ | |
gitlabCommitStatus(name: 'Sonar', connection: gitLabConnection('Gitlab')){ | |
echo "#####################################" | |
echo "############# SONAR ##############" | |
echo "#####################################" | |
withSonarQubeEnv('SonarQube ') { | |
sh(''' | |
echo "sonar.projectVersion=${APP_VERSION}" >> sonar-project.properties | |
echo "sonar.host.url=${SONAR_URL}" >> sonar-project.properties | |
echo "sonar.projectKey=${PROJECT_KEY}" >> sonar-project.properties | |
echo "sonar.login=a50ea1d92535ca11117785fede9c8807587b47a9" >> sonar-project.properties | |
echo "sonar.java.coveragePlugin=jacoco" >> sonar-project.properties | |
echo "sonar.projectName=${PROJECT_NAME}" >> sonar-project.properties | |
echo "sonar.dynamicAnalysis=reuseReports" >> sonar-project.properties | |
mvn sonar:sonar -Dproject.settings=sonar-project.properties | |
''') | |
} | |
} | |
} | |
} | |
stage('Quality Gate'){ | |
steps{ | |
gitlabCommitStatus(name: 'Quality Gate', connection: gitLabConnection('Gitlab')){ | |
echo "#####################################" | |
echo "######### Quality Gate ###########" | |
echo "#####################################" | |
timeout(time: 1, unit: 'MINUTES') { | |
waitForQualityGate abortPipeline: true | |
} | |
} | |
} | |
} | |
stage('Artifactory'){ | |
steps{ | |
gitlabCommitStatus(name: 'Artifactory', connection: gitLabConnection('Gitlab')){ | |
echo "#####################################" | |
echo "########## Artifactory ###########" | |
echo "#####################################" | |
rtMavenResolver ( | |
id: 'resolver-ARTIFACT_NAME', | |
serverId: 'Artifactory', | |
releaseRepo: 'libs-release', | |
snapshotRepo: 'libs-snapshot' | |
) | |
rtMavenDeployer ( | |
id: 'deployer-ARTIFACT_NAME', | |
serverId: 'Artifactory', | |
releaseRepo: 'libs-release-local', | |
snapshotRepo: 'libs-snapshot-local' | |
) | |
rtMavenRun ( | |
tool: 'Maven_3.6.2', | |
pom: 'pom.xml', | |
goals: 'source:jar install -U -DskipTests', | |
resolverId: 'resolver-ARTIFACT_NAME', | |
deployerId: 'deployer-ARTIFACT_NAME' | |
) | |
} | |
} | |
} | |
} | |
post { | |
always { | |
echo "Eliminando contenido del workspace..." | |
cleanWs() | |
script { | |
def artifactUrl = env.BUILD_URL + "artifact/" | |
def msg = "**Status:** " + currentBuild.currentResult.toLowerCase() + "\n" | |
msg += "**Branch:** " + env.GIT_BRANCH + "\n" | |
msg += "**Changes:** \n" | |
if (!currentBuild.changeSets.isEmpty()) { | |
currentBuild.changeSets.first().getLogs().each { | |
msg += "- `" + it.getCommitId().substring(0, 8) + "` *" + it.getComment().substring(0, it.getComment().length()-1) + "*\n" | |
} | |
} else { | |
msg += "no changes for this run\n" | |
} | |
if (msg.length() > 1024) msg.take(msg.length() - 1024) | |
def filename | |
msg += "\n **Artifacts:**\n" | |
currentBuild.rawBuild.getArtifacts().each { | |
filename = it.getFileName() | |
msg += "- [${filename}](${artifactUrl}${it.getFileName()})\n" | |
} | |
discordSend description: "${msg}", link: env.BUILD_URL, successful: currentBuild.resultIsBetterOrEqualTo('SUCCESS'), title: "${PROJECT_KEY}:" + env.GIT_BRANCH + " #${BUILD_NUMBER}", webhookURL: "DISCORD_URL" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment