Created
March 19, 2018 17:39
-
-
Save lvthillo/7e5f39bdec456df4c336ed78521d42cc to your computer and use it in GitHub Desktop.
Reusable declarative Jenkins pipeline for maven projects
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
def call(body) { | |
def rtMaven = '' | |
def buildInfo = '' | |
def server = '' | |
// evaluate the body block, and collect configuration into the object | |
def pipelineParams= [:] | |
body.resolveStrategy = Closure.DELEGATE_FIRST | |
body.delegate = pipelineParams | |
body() | |
pipeline { | |
// pipeline can run on every available jenkins slave | |
agent any | |
// keep only the 3 last builds | |
options { | |
buildDiscarder(logRotator(numToKeepStr: '3')) | |
} | |
// create a boolean parameter SKIP_TESTS. By default it is false (not checked) and it will execute the tests | |
parameters { | |
booleanParam(name: 'SKIP_TESTS', defaultValue: false, description: 'Check if you want to skip tests') | |
} | |
stages { | |
// this stage will checkout the git repository of your maven project | |
stage('Checkout Git repository') { | |
steps { | |
git branch: pipelineParams.branch, credentialsId: pipelineParams.scmCredentials, url: pipelineParams.scmUrl | |
} | |
} | |
/* here we will configure our Artifactory and its release/snapshot repository. | |
we will also define the maven goal and use our parameter value*/ | |
stage('Maven Build') { | |
steps { | |
script { | |
server = Artifactory.server "jfrog-artifactory" | |
rtMaven = Artifactory.newMavenBuild() | |
rtMaven.deployer server: server, releaseRepo: 'company-release', snapshotRepo: 'company-snapshot' | |
rtMaven.tool = 'Maven 3.5.2' | |
buildInfo = rtMaven.run pom: pipelineParams.pom, goals: 'clean install -DskipTests=$SKIP_TESTS' | |
} | |
} | |
} | |
// publish the buildinfo to Artifactory | |
stage('Upload') { | |
steps { | |
script { | |
server.publishBuildInfo buildInfo | |
} | |
} | |
} | |
} | |
// clean our workspace on the slave and trigger the slackNotifier | |
post { | |
always { | |
cleanWs() | |
slackNotifier(currentBuild.currentResult) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @lvthillo
I am new to world of coding and Jenkins groovy scirpt, currently i am trying to update my existing Maven project into DSL.
Firstly, thank you for the above script but i had couple of questions.
Thanking in advance