Here we will be creating a Groovy pipeline in Jenkins 2 to do the following steps :
Here are the steps we will :
Make sure we have at least a 2.0 Jenkins version (you can check that in the bottom-right corner of your page) such as :
On Jenkins home page, click on New Item
Enter project name and select Pipeline
In Build Triggers section, select Poll SCM option and add the following 5 minutes CRON schedule :
*/5 * * * *
In Pipeline section, choose either Pipeline Script or Pipeline Script from SCM
If you selected Pipeline Script from SCM on previous step, you now need to specify your SCM repository (Git, Mercurial, Subversion) URL in Repository URL such as http://github.com/example/example.git
. You also need to specify the Script Path of your Groovy script file in your example.git repository, e.g. pipelines/example.groovy
Copy the following Groovy code, either directly in the Groovy script window if you previously clicked Pipeline Script or in your example.groovy
if you choosed Pipeline Script from SCM
node('remote') {
// Note : this step is only needed if you're using direct Groovy scripting
stage 'Checkout Git project'
git url: 'https://github.com/jglick/simple-maven-project-with-tests.git'
def appVersion = version()
if (appVersion) {
echo "Building version ${appVersion}"
}
stage 'Build Maven project'
def mvnHome = tool 'M3'
sh "${mvnHome}/bin/mvn -B -Dmaven.test.failure.ignore verify"
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'])
}
def version() {
def matcher = readFile('pom.xml') =~ '<version>(.+)</version>'
matcher ? matcher[0][1] : null
}
Here you go, you should now be able to compile and test your first Jenkins project using Jenkins 2 Groovy pipeline.