From 1ddb5691ca8e603e4c4d66c7f9384d13dacdc009 Mon Sep 17 00:00:00 2001 From: William Petit Date: Mon, 17 Oct 2022 22:34:17 +0200 Subject: [PATCH] fix(cpkg): reuse latest version number --- vars/cpkg.groovy | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/vars/cpkg.groovy b/vars/cpkg.groovy index 651e3ab..a9cee19 100644 --- a/vars/cpkg.groovy +++ b/vars/cpkg.groovy @@ -1,8 +1,8 @@ +import java.util.regex.Matcher + // Basic port of https://forge.cadoles.com/Cadoles/cpkg def call(Map params = [:]) { - def currentRef = sh(script: 'git rev-parse HEAD', returnStdout: true).trim() - def baseRef = params.baseRef ? params.baseRef : currentRef def distRepo = params.distRepo ? params.distRepo : 'dev' def dist = params.dist ? params.dist : 'eole' def distVersion = params.distVersion ? params.distVersion : '2.7.0' @@ -12,7 +12,7 @@ def call(Map params = [:]) { def gitEmail = params.gitEmail ? params.gitEmail : 'jenkins@cadoles.com' def gitUsername = params.gitUsername ? params.gitUsername : 'Jenkins' def skipCi = params.containsKey('skipCi') ? params.skipCi : false - def skipPush = params.containsKey('skipPush') ? params.skipPush: true + def skipPush = params.containsKey('skipPush') ? params.skipPush : true // Define dist branch based on provided informations and base branch name def distBranch = "dist/${dist}/${distVersion}/${distBranchName}" @@ -28,7 +28,7 @@ def call(Map params = [:]) { sh("git config --add remote.origin.fetch +refs/heads/${distBranch}:refs/remotes/origin/${distBranch}") // Update branches - sh("git fetch --all") + sh('git fetch --all') // Merge currentRef into distBranch and push sh("git checkout -b '${distBranch}' 'origin/${distBranch}'") @@ -40,7 +40,7 @@ def call(Map params = [:]) { sh("git merge ${currentRef}") if (!skipPush) { - sh("git push") + sh('git push') } else { println("Skipping push. Set skipPush param to 'true' to enable remote repository update.") } @@ -61,15 +61,22 @@ def call(Map params = [:]) { println("Last version number is '${lastVersionNumber}'") - def versionNumber = sh( - script: "git describe --always ${currentRef}", - returnStdout: true, - ).split('/').last().trim() + String versionRoot = extractVersionRoot(lastVersionNumber) + String versionNumber = '' - def isCommitRef = !versionNumber.matches(/^[0-9]+\.[0-9]+\.[0-9]+.*$/) + if (versionRoot) { + versionNumber = versionRoot + } else { + versionNumber = sh( + script: "git describe --always ${currentRef}", + returnStdout: true, + ).split('/').last().trim() - if (isCommitRef) { - versionNumber = "0.0.0-${versionNumber}" + Boolean isCommitRef = !versionNumber.matches(/^[0-9]+\.[0-9]+\.[0-9]+.*$/) + + if (isCommitRef) { + versionNumber = "0.0.0-${versionNumber}" + } } versionNumber = "${versionNumber}-b${env.BUILD_NUMBER}" @@ -82,7 +89,7 @@ def call(Map params = [:]) { result['newTag'] = tag - def tagComment="Build ${versionNumber} ${distRepo} package for ${dist}-${distVersion}." + def tagComment = "Build ${versionNumber} ${distRepo} package for ${dist}-${distVersion}." if (skipCi) { tagComment += ' [ci skip]' } @@ -91,7 +98,7 @@ def call(Map params = [:]) { // Push tag if (!skipPush) { - sh("git push --tags -f") + sh('git push --tags -f') } else { println("Skipping push. Set skipPush param to 'true' to enable remote repository update.") } @@ -117,4 +124,15 @@ def call(Map params = [:]) { } return result -} \ No newline at end of file +} + +@NonCPS +String extractVersionRoot(String fullVersion) { + Matcher fullVersionMatcher = fullVersion =~ /^([0-9]+\.[0-9]+\.[0-9]+).*$/ + + if (!fullVersionMatcher.matches()) { + return "" + } + + return fullVersionMatcher.group(1) +}