Skip to content

Instantly share code, notes, and snippets.

@silkentrance
Created March 25, 2019 13:53
Show Gist options
  • Save silkentrance/d14ea5f857a2f955c2512c017e4e090a to your computer and use it in GitHub Desktop.
Save silkentrance/d14ea5f857a2f955c2512c017e4e090a to your computer and use it in GitHub Desktop.
Maven custom build versions that include jira issue id from branch name
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