diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..3467d80 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,185 @@ +#!groovy + +// Required Jenkins plugins: +// https://wiki.jenkins-ci.org/display/JENKINS/Timestamper +// https://wiki.jenkins-ci.org/display/JENKINS/Static+Code+Analysis+Plug-ins +// https://wiki.jenkins-ci.org/display/JENKINS/Checkstyle+Plugin ? +// https://wiki.jenkins-ci.org/display/JENKINS/FindBugs+Plugin +// https://wiki.jenkins-ci.org/display/JENKINS/PMD+Plugin ? +// https://wiki.jenkins-ci.org/display/JENKINS/DRY+Plugin ? +// https://wiki.jenkins-ci.org/display/JENKINS/Task+Scanner+Plugin +// https://wiki.jenkins-ci.org/display/JENKINS/Javadoc+Plugin +// https://wiki.jenkins-ci.org/display/JENKINS/JaCoCo+Plugin ? + + +init() + +def branch_name +def branch_name_base +def build_number +def build_url +def git_commit +def job_name +def tag +def version +def build_type +def display_name + +def init() { + + // Keep the 5 most recent builds + properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '5']]]) + + build_number = env.BUILD_NUMBER + build_url = env.BUILD_URL + job_name = "${env.JOB_NAME}" + branch_name = env.BRANCH_NAME + branch_name_docker = branch_name.replaceAll(/\//,'.') + persist = "/var/lib/jenkins/PERSIST/${branch_name_docker}" + + // execute the branch type specific pipeline code + try { + + if (branch_name.indexOf('release/')==0) build_type='release' + if (branch_name.indexOf('feature/')==0) build_type='feature' + if (branch_name.indexOf('develop')==0) build_type='develop' + if (branch_name.indexOf('hotfix')==0) build_type='hotfix' + if (branch_name.indexOf('bugfix')==0) build_type='bugfix' + if (branch_name.indexOf('master')==0) build_type='master' + + // common pipeline elements + node('master') { + Initialize() + SetVersion(build_type) + print_vars() // after SetVersion - all variables now defined + set_result('INPROGRESS') + Build() // builds database via flyway migration + } + + if (branch_name.indexOf('develop')==0) { + node('master') { + Deploy(); + } + } else if (branch_name.indexOf('release/')==0) { + node('master') { + Deploy(); + } + } + + node('master') { + set_result('SUCCESS') + } + + } catch (err) { + node() { + set_result('FAILURE') + } + throw err + } +} + +def Build() { + stage ('build') { + mvn "install -DskipTests=true -Dbuild.revision=${git_commit}" + step([$class: 'ArtifactArchiver', artifacts: '**/target/*.jar', fingerprint: true]) + } +} + +def Initialize() { + stage ('initialize') { + + // get new code + checkout scm + + git_commit = getSha1() + } +} + +def Deploy() { + stage ('deploy') { + mvn "deploy -DskipTests=true -Dbuild.number=${build_number} -Dbuild.revision=${git_commit}" + } +} + +def getSha1() { + sha1 = sh(script: 'git rev-parse HEAD', returnStdout: true).trim() + echo "sha1 is ${sha1}" + return sha1 +} + +def mvn(args) { + // add node and maven tools to path before calling maven + def mvnHome = tool name: 'maven-3.6.1' + env.PATH = "${mvnHome}/bin:${env.PATH}" + sh "mvn ${args}" +} + +def mvn_initial(args) { + // add node and maven tools to path before calling maven + def mvnHome = tool name: 'maven-3.6.1' + env.PATH = "${mvnHome}/bin:${env.PATH}" + sh "mvn ${args}" +} + +def set_result(status) { + if ( status == 'SUCCESS' ) { + currentBuild.result = status + notify_bitbucket('SUCCESSFUL') + } else if ( status == 'FAILURE' ) { + currentBuild.result = status + notify_bitbucket('FAILED') + } else if ( status == 'INPROGRESS' ) { + notify_bitbucket('INPROGRESS') + } else { + error ("unknown status") + } + + // save in persistence file for access the status page + // make sure the directory exists first + sh "mkdir -p $persist && echo $status > $persist/build.result" +} + +def notify_bitbucket(state) { +} + +def print_vars() { + echo "build_number = ${build_number}" + echo "build_url = ${build_url}" + echo "job_name = ${job_name}" + echo "branch_name = ${branch_name}" + echo "branch_name_base = ${branch_name_base}" + echo "build_type = ${build_type}" + echo "display_name = ${currentBuild.displayName}" + echo "version = ${version}" + echo "git_commit = ${git_commit}" + +} + +def SetVersion( v ) { + stage ('set version') { + echo "set version ${v}" + branch_name_base = (branch_name =~ /([^\/]+$)/)[0][0] + if ( v == 'release' ) { + // for release branches, where the branch is named "release/1.2.3", + // derive the version and display name derive from the numeric suffix and append the build number + // 3.2.1.100 + version = branch_name_base + "." + build_number + "-RELEASE"; + //version = branch_name.substring('release/'.length()) + "." + build_number + currentBuild.displayName = version + } else if (v == 'develop') { + version = branch_name_base + "." + build_number + "-SNAPSHOT"; + currentBuild.displayName = version + } else { + // for all other branches the version number is 0 with an appended build number + // and for the display name use the jenkins default #n and add the branch name + // #101 - feature/user/foo + //version = '0.' + build_number + version = branch_name_base + "." + build_number + currentBuild.displayName = "#" + build_number + " - " + branch_name_base + } + display_name = currentBuild.displayName + mvn_initial "versions:set -DnewVersion=${version}" + } +} + +return this \ No newline at end of file diff --git a/README.md b/README.md index 012af7c..2422491 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,6 @@ Maven net.locusworks applogger - 0.0.1-SNAPSHOT + ${latest_version} ``` \ No newline at end of file diff --git a/pom.xml b/pom.xml index acd4c52..e922773 100644 --- a/pom.xml +++ b/pom.xml @@ -8,20 +8,37 @@ Application Logger Logger library for applications + + ssh://git@bitbucket.locusworks.net:7999/lwc/applogger.git + + + locusworks + www.locusworks.net + + ${project.basedir} 1.8 1.8 - 2.11.0 - 1.7.25 + 2.12.0 + 1.7.26 + http://nexus.locusworks.net + + org.apache.maven.plugins + maven-surefire-plugin + 2.20.1 + + always + + org.apache.maven.plugin maven-compiler-plugin - 3.6.1 + 3.7.0 1.8 1.8 @@ -58,11 +75,29 @@ - - git@bitbucket.org:locus2k/applogger.git - - - locusworks - www.locusworks.net - + + + nexus-proxy-public + ${nexus.repo}/repository/maven-public/ + + + + + nexus-proxy-public + ${nexus.repo}/repository/maven-public/ + + + + + + nexus-snapshot + ${nexus.repo}/repository/locusworks-snapshot/ + + + nexus-release + ${nexus.repo}/repository/locusworks-release/ + + + + \ No newline at end of file