blob: b5046c17298cef6c00271b282ea4c0a4338cbe60 [file] [log] [blame]
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +02001package com.mirantis.mk
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +02002/**
3 * ReleaseWorkflow functions
4 *
5 */
6
7/**
8 * Update release metadata after image build
Ruslan Gustomiasovcef1bb42019-06-25 17:00:13 +02009 *
10 * @param key metadata key
11 * @param value metadata value
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020012 * @param params string map with credentialsID, metadataRepoUrl, metadataGerritBranch and crTopic
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020013 */
14
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +050015def updateReleaseMetadata(String key, String value, Map params) {
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020016 credentialsID = params['credentialsID'] ?: "mcp-ci-gerrit"
Mykyta Karpin28457922019-07-05 15:14:58 +030017 metadataRepoUrl = params['metadataRepoUrl'] ?: "ssh://mcp-ci-gerrit@gerrit.mcp.mirantis.net:29418/mcp/release-metadata"
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020018 metadataGerritBranch = params['metadataGerritBranch'] ?: "master"
Ruslan Gustomiasov2e9e3452019-06-27 14:29:31 +020019 comment = params['comment'] ?: ""
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020020 crTopic = params['crTopic'] ?: ""
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +050021 def common = new com.mirantis.mk.Common()
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020022 def python = new com.mirantis.mk.Python()
23 def gerrit = new com.mirantis.mk.Gerrit()
24 def git = new com.mirantis.mk.Git()
25 def changeAuthorName = "MCP-CI"
26 def changeAuthorEmail = "mcp-ci-jenkins@ci.mcp.mirantis.net"
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020027 def cred = common.getCredentials(credentialsID, 'key')
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020028 String gerritUser = cred.username
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020029 def gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0]
30 def metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/')
31 def gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0]
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020032 def workspace = common.getWorkspace()
33 def venvDir = "${workspace}/gitreview-venv"
34 def repoDir = "${venvDir}/repo"
35 def metadataDir = "${repoDir}/metadata"
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020036 def ChangeId
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020037 def commitMessage
38 def gitRemote
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020039 stage("Installing virtualenv") {
40 python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml'])
41 }
42 stage('Cleanup repo dir') {
43 dir(repoDir) {
44 deleteDir()
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020045 }
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020046 }
47 stage('Cloning release-metadata repository') {
48 git.checkoutGitRepository(repoDir, metadataRepoUrl, metadataGerritBranch, credentialsID, true, 10, 0)
49 dir(repoDir) {
50 gitRemote = sh(
51 script:
52 'git remote -v | head -n1 | cut -f1',
53 returnStdout: true,
54 ).trim()
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020055 }
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020056 }
57 stage('Creating CR') {
58 def gerritAuth = ['PORT': gerritPort, 'USER': gerritUser, 'HOST': gerritHost]
59 def changeParams = ['owner': gerritUser, 'status': 'open', 'project': metadataProject, 'branch': metadataGerritBranch, 'topic': crTopic]
60 def gerritChange = gerrit.findGerritChange(credentialsID, gerritAuth, changeParams)
61 git.changeGitBranch(repoDir, metadataGerritBranch)
62 if (gerritChange) {
63 def jsonChange = readJSON text: gerritChange
64 changeNum = jsonChange['number']
65 ChangeId = 'Change-Id: '
66 ChangeId += jsonChange['id']
67 //get existent change from gerrit
68 gerrit.getGerritChangeByNum(credentialsID, venvDir, repoDir, gitRemote, changeNum)
69 } else {
70 ChangeId = ''
71 git.createGitBranch(repoDir, crTopic)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020072 }
Alexandr Lovtsovc1cc7502019-07-02 12:41:53 +050073 cmdText = "python '${repoDir}/utils/app.py' --path '${metadataDir}' update --key '${key}' --value '${value}'"
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020074 python.runVirtualenvCommand(venvDir, cmdText)
75 commitMessage =
Ruslan Gustomiasov2e9e3452019-06-27 14:29:31 +020076 """${comment}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020077
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020078 |${ChangeId}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020079 """.stripMargin()
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020080 //commit change
81 if (gerritChange) {
82 git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false, true)
83 } else {
84 git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020085 }
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020086 //post change
87 gerrit.postGerritReview(credentialsID, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020088 }
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +050089}