blob: daf98c2a2b8906a952f380ec0c64a02ae3e31218 [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 }
Mykyta Karpin6f050b22019-09-24 13:57:20 +030079}
80
81/**
82 * Takes a list of dependencies and a version, and sets a version for each dependency in requirements.yaml. If dependency isn't
83 * found in requirements.yaml or requirements.yaml does not exist - does nothing.
84 *
85 * @param chartPath string, path to a directory with helm chart
86 * @param dependencies list of hashes with names and versions of dependencies in format:
87 * [['name': 'chart-name1', 'version': '0.1.0-myversion'], ['name': 'chart-name2', 'version': '0.2.0-myversion']]
88 */
89
90def setChartDependenciesVersion(chartPath, List dependencies){
91 def common = new com.mirantis.mk.Common()
92 if (!dependencies){
93 error 'No list of target dependencies is specified'
94 }
95 def reqsFilePath = "${chartPath}/requirements.yaml"
96 def chartYaml = readYaml file: "${chartPath}/Chart.yaml"
97 def reqsUpdateNeeded = false
98 def reqsMap = [:]
99 if (fileExists(reqsFilePath)){
100 reqsMap = readYaml file: reqsFilePath
101 for (i in dependencies) {
102 for (item in reqsMap.get('dependencies', [])){
103 if (item['name'] == i['name']){
Mykyta Karpin882dd362019-09-25 11:27:55 +0300104 common.infoMsg("Set version ${i['version']} for dependency ${i['name']} in chart ${chartYaml['name']}")
Mykyta Karpin6f050b22019-09-24 13:57:20 +0300105 item['version'] = i['version']
106 reqsUpdateNeeded = true
107 }
108 }
109 }
110 }
111 if (reqsUpdateNeeded){
112 sh "rm ${reqsFilePath}"
113 writeYaml file: reqsFilePath, data: reqsMap
114 } else {
115 common.warningMsg("requirements.yaml doesn't exist at path ${reqsFilePath} or chart doesn't contain ${dependencies}, nothing to set")
116 }
Mykyta Karpin3c78c0f2019-09-11 18:11:06 +0300117}