Skip to content

Instantly share code, notes, and snippets.

@lvthillo
Created March 19, 2018 17:39
Show Gist options
  • Save lvthillo/7e5f39bdec456df4c336ed78521d42cc to your computer and use it in GitHub Desktop.
Save lvthillo/7e5f39bdec456df4c336ed78521d42cc to your computer and use it in GitHub Desktop.
Reusable declarative Jenkins pipeline for maven projects
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)
}
}
}
}
@niinad
Copy link

niinad commented Mar 22, 2024

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.

  1. Line 41 - is for Resolve artifacts from artifactory or Post Build deploy artifacts to artifactory (Actually i need both - Post build if build succeeds)
  2. In build stage how do i add "Enable triggering of downstream projects" and "Provided Settings- settings.xml"

Thanking in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment