Last active
March 7, 2018 20:30
-
-
Save Jotschi/d240bf432d66a13be4c46889aa091065 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Let jenkins know that you want to execute the commands on a dedicated jenkins slave | |
node('dockerSlave') { | |
def mvnHome = tool 'M3' | |
// Cleanup local checkout - TODO there should also be a dedicated jenkins command to invoke this action | |
sh "rm -rf *" | |
sh "rm -rf .git" | |
// Clone from git | |
checkout scm | |
// Checkout specific local branch | |
checkout([$class: 'GitSCM', branches: [[name: '*/master']], | |
extensions: [[$class: 'CleanCheckout'],[$class: 'LocalBranch', localBranch: "master"]]]) | |
// Prepare the version by extracting major and minor number and creating a new number: [major].[minor]-[BUILDNUMBER] | |
stage 'Set Version' | |
def originalV = version(); | |
def major = originalV[0]; | |
def minor = originalV[1]; | |
def v = "${major}.${minor}-${env.BUILD_NUMBER}" | |
if (v) { | |
echo "Building version ${v}" | |
} | |
// Update the project pom.xml files | |
sh "${mvnHome}/bin/mvn -B versions:set -DgenerateBackupPoms=false -DnewVersion=${v}" | |
// Add the pom.xml files and create a commit+tag | |
sh 'git add .' | |
sh "git commit -m 'Raise version'" | |
sh "git tag v${v}" | |
// Create the release build | |
stage 'Release Build' | |
// Use the SSH Agent Plugin to forward the used ssh credentials | |
// from the jenkins master to the jenkins slave. Otherwise you may | |
// not be able to push/pull, clone | |
sshagent(['601b6ce9-37f7-439a-ac0b-8e368947d98d']) { | |
// Invoke the maven build without tests and deploy the artifacts | |
sh "${mvnHome}/bin/mvn -B -DskipTests clean deploy" | |
// Push the commit and the created tag | |
sh "git push origin master" | |
sh "git push origin v${v}" | |
} | |
} | |
// Parse the pom.xml and extract the version information. | |
def version() { | |
def matcher = readFile('pom.xml') =~ '<version>(.+)-.*</version>' | |
matcher ? matcher[0][1].tokenize(".") : null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you could do the cleanup in line 5/6 with deleteDir()