Add method for charts versioning
Method allows to get semver2 compatible version for helm charts
from git repository. As base version is used a first parent git tag
found by pattern, if no tag found, default value will be used. Default
value is {defaultVersion}-{tagSuffix} supplied via method args.
Change-Id: I009473acbd4149e8eccb2343fa2487773c9622e0
Related-Prod: https://mirantis.jira.com/browse/PROD-33012
Depends-On: I1299631c9aa556e1b8dff4a2fa80b0dbb9632b7f
diff --git a/src/com/mirantis/mk/Helm.groovy b/src/com/mirantis/mk/Helm.groovy
index 899bc36..af3fcf9 100644
--- a/src/com/mirantis/mk/Helm.groovy
+++ b/src/com/mirantis/mk/Helm.groovy
@@ -15,3 +15,65 @@
def helmRepoIndex(extra_params='', charts_dir='.'){
sh("helm repo index ${extra_params} ${charts_dir}")
}
+
+/**
+ * Generates version for helm chart based on information from git repository. Tries to search
+ * first parent git tag using pattern '[0-9]*-{tagSuffix}', if found that tag will be used
+ * in final version, if not found - version will be formed as '{defaultVersion}-{tagSuffix}'. Number
+ * of commits since last tag or sha of current commit can be added to version.
+ *
+ * @param repoDir string, path to a directory with git repository of helm charts
+ * @param devVersion Boolean, if set to true development version will be calculated e.g 0.1.0-mcp-{sha of current commit}
+ * @param increment Boolean, if set to true patch version will be incremented (e.g 0.1.0 -> 0.1.1)
+ * @param defaultVersion string, value of version which will be used in case no tags found. should be semver2 compatible
+ * @param tagSuffix string, suffix which will be used for finding tags in git repository, also if tag not found, it
+ * it will be added to {defaultVersion} e.g {defaultVersion}-{tagSuffix}
+ */
+
+def generateChartVersionFromGit(repoDir, devVersion = true, increment = false, defaultVersion = '0.1.0', tagSuffix = 'mcp') {
+ def common = new com.mirantis.mk.Common()
+ def git = new com.mirantis.mk.Git()
+ String initialVersion = "${defaultVersion}-${tagSuffix}"
+ String countRange
+ String versionData
+ String tagPattern = "[0-9]*-${tagSuffix}"
+ dir(repoDir){
+ Map cmd = common.shCmdStatus("git describe --tags --first-parent --abbrev=0 --match ${tagPattern}")
+ String lastTag = cmd['stdout'].trim()
+
+ if (cmd['status'] != 0){
+ if (cmd['stderr'].contains('fatal: No names found, cannot describe anything')){
+ common.warningMsg("No parent git tag found, using initial version ${initialVersion}")
+ versionData = initialVersion
+ countRange = 'HEAD'
+ } else {
+ error("Something went wrong, cannot find git information ${cmd['stderr']}")
+ }
+ } else {
+ versionData = lastTag
+ countRange = "${lastTag}..HEAD"
+ }
+ List versionParts = versionData.tokenize('-')
+
+ if (versionParts.size() == 2 && common.isSemVer(versionData) && versionParts[1] == tagSuffix){
+ String commitsSinceTag = sh(script: "git rev-list --count ${countRange}", returnStdout: true).trim()
+ String commitSha = sh(script: 'git rev-parse --short=7 HEAD', returnStdout: true).trim()
+
+ if (commitsSinceTag == '0'){
+ return versionData
+ }
+
+ if (devVersion){
+ versionParts.add(commitSha)
+ } else {
+ versionParts.add(commitsSinceTag)
+ }
+ // Patch version will be incremented e.g. 0.1.0 -> 0.1.1
+ if (increment) {
+ versionParts[0] = git.incrementVersion(versionParts[0])
+ }
+ return versionParts.join('-')
+ }
+ error "Version ${versionData} doesn't contain required suffix ${tagSuffix} or not in semver2 format"
+ }
+}
\ No newline at end of file