Created
March 25, 2019 13:53
-
-
Save silkentrance/d14ea5f857a2f955c2512c017e4e090a to your computer and use it in GitHub Desktop.
Maven custom build versions that include jira issue id from branch name
This file contains 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
pipeline { | |
agent any | |
environment { | |
V_DEPLOY_VERSION = buildDeployVersion(readMavenPom().getVersion()) | |
} | |
stages { | |
stage('Version') { | |
steps { | |
sh 'printenv' | |
sh 'mvn versions:set -DnewVersion=$V_DEPLOY_VERSION' | |
} | |
} | |
stage('Build') { | |
steps { | |
sh 'mvn clean install' | |
} | |
} | |
} | |
} | |
@NonCPS | |
def extractIssueIdFromBranchName() { | |
def matcher = env['BRANCH_NAME'] =~ '^(.*?)([A-Z]+-[0-9]+).*$' | |
matcher ? matcher[0][2] : null | |
} | |
@NonCPS | |
def extractBranchFromBranchName() { | |
def matcher = env['BRANCH_NAME'] =~ '^(develop)$' | |
matcher ? 'DEV' : null | |
} | |
@NonCPS | |
def extractVersionFromPomVersion(pomVersion) { | |
def matcher = pomVersion =~ '^([0-9]+.[0-9]+.[0-9]+)(-SNAPSHOT)?$' | |
matcher ? matcher[0][1] : pomVersion | |
} | |
@NonCPS | |
def buildTag(branch, issueId) { | |
(branch ? branch : issueId) + '-' + env['BUILD_NUMBER'] + '-SNAPSHOT' | |
} | |
@NonCPS | |
def buildDeployVersion(pomVersion) { | |
def result = null | |
def branch = extractBranchFromBranchName() | |
def issueId = extractIssueIdFromBranchName() | |
def version = extractVersionFromPomVersion(pomVersion) | |
if (branch || issueId) { | |
def tag = buildTag(branch, issueId) | |
result = version + '.' + tag | |
} else { | |
result = version | |
} | |
result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment