Minimal Jenkinsfile for a Gradle Build

Published:
Tags:
Gradle Java Jenkins Spring Boot
Reading time:
About 1 min

Starting work on a new project for a client, and their devops organization is off working on some new framework for CI/CD deploys. So in the meantime we just need a super minimal Jenkins build. 

The requirements I have for this are:

  1. Run the build 
  2. Run the tests 
  3. Display test report 
  4. Execute on every push to git, including PR branches 
  5. Email failures 

With that in mind, here is a quite minimal Jenkins script for our gradle build:

//Inspired by https://gist.github.com/aerobless/37561bb0fb45b7e8732beaafad1cba26
pipeline {
agent any
triggers {
//Needed by Bitbucket to see the builds on PRs https://stackoverflow.com/a/54710254
pollSCM('')
}
stages {
stage('test') {
steps {
script {
sh './gradlew clean check --stacktrace'
junit '**/build/test-results/test/*.xml'
}
}
}
}
post {
always { //Send an email to the person that broke the build
step([$class : 'Mailer',
notifyEveryUnstableBuild: true,
recipients : [emailextrecipients([[$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider']])].join(' '),
sendToIndividuals: true])
}
}
}
view raw Jenkinsfile hosted with ❤ by GitHub

I'm not 100% sure if that last email block is needed, but it worked and I don't feel like messing with it. 

Also our builds are running against BitBucket using a Jenkins plugin, and even though we have it setup without needing polling, we still needed that Bitbucket poll hack. So here you go!