blob: af3fcf9376de9349bee612af9b98902c608c9d6b [file] [log] [blame]
Ruslan Gustomiasov5d131b62019-08-21 11:51:26 +02001package com.mirantis.mk
2
3/**
4 *
5 * Functions to work with Helm
6 *
7 */
8
9/**
10 * Build index file for helm chart
Sergey Otpuschennikov50b248c2019-08-28 17:21:18 +040011 * @param extra_params additional params, e.g. --url repository_URL
12 * @param charts_dir path to a directory
Ruslan Gustomiasov5d131b62019-08-21 11:51:26 +020013 */
14
Sergey Otpuschennikov50b248c2019-08-28 17:21:18 +040015def helmRepoIndex(extra_params='', charts_dir='.'){
16 sh("helm repo index ${extra_params} ${charts_dir}")
17}
Mykyta Karpin3c78c0f2019-09-11 18:11:06 +030018
19/**
20 * Generates version for helm chart based on information from git repository. Tries to search
21 * first parent git tag using pattern '[0-9]*-{tagSuffix}', if found that tag will be used
22 * in final version, if not found - version will be formed as '{defaultVersion}-{tagSuffix}'. Number
23 * of commits since last tag or sha of current commit can be added to version.
24 *
25 * @param repoDir string, path to a directory with git repository of helm charts
26 * @param devVersion Boolean, if set to true development version will be calculated e.g 0.1.0-mcp-{sha of current commit}
27 * @param increment Boolean, if set to true patch version will be incremented (e.g 0.1.0 -> 0.1.1)
28 * @param defaultVersion string, value of version which will be used in case no tags found. should be semver2 compatible
29 * @param tagSuffix string, suffix which will be used for finding tags in git repository, also if tag not found, it
30 * it will be added to {defaultVersion} e.g {defaultVersion}-{tagSuffix}
31 */
32
33def generateChartVersionFromGit(repoDir, devVersion = true, increment = false, defaultVersion = '0.1.0', tagSuffix = 'mcp') {
34 def common = new com.mirantis.mk.Common()
35 def git = new com.mirantis.mk.Git()
36 String initialVersion = "${defaultVersion}-${tagSuffix}"
37 String countRange
38 String versionData
39 String tagPattern = "[0-9]*-${tagSuffix}"
40 dir(repoDir){
41 Map cmd = common.shCmdStatus("git describe --tags --first-parent --abbrev=0 --match ${tagPattern}")
42 String lastTag = cmd['stdout'].trim()
43
44 if (cmd['status'] != 0){
45 if (cmd['stderr'].contains('fatal: No names found, cannot describe anything')){
46 common.warningMsg("No parent git tag found, using initial version ${initialVersion}")
47 versionData = initialVersion
48 countRange = 'HEAD'
49 } else {
50 error("Something went wrong, cannot find git information ${cmd['stderr']}")
51 }
52 } else {
53 versionData = lastTag
54 countRange = "${lastTag}..HEAD"
55 }
56 List versionParts = versionData.tokenize('-')
57
58 if (versionParts.size() == 2 && common.isSemVer(versionData) && versionParts[1] == tagSuffix){
59 String commitsSinceTag = sh(script: "git rev-list --count ${countRange}", returnStdout: true).trim()
60 String commitSha = sh(script: 'git rev-parse --short=7 HEAD', returnStdout: true).trim()
61
62 if (commitsSinceTag == '0'){
63 return versionData
64 }
65
66 if (devVersion){
67 versionParts.add(commitSha)
68 } else {
69 versionParts.add(commitsSinceTag)
70 }
71 // Patch version will be incremented e.g. 0.1.0 -> 0.1.1
72 if (increment) {
73 versionParts[0] = git.incrementVersion(versionParts[0])
74 }
75 return versionParts.join('-')
76 }
77 error "Version ${versionData} doesn't contain required suffix ${tagSuffix} or not in semver2 format"
78 }
79}