blob: 5d54776da43ead7b1f6e2c95bb1d97a436cbc58e [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
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020015def updateReleaseMetadata(key, value, String[] params) {
16 credentialsID = params['credentialsID'] ?: "mcp-ci-gerrit"
17 metadataRepoUrl = params['metadataRepoUrl'] ?: "ssh://mcp-ci-gerrit@gerrit.mcp.mirantis.net:29418/mcp/release-matadata"
18 metadataGerritBranch = params['metadataGerritBranch'] ?: "master"
19 crTopic = params['crTopic'] ?: ""
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020020 def python = new com.mirantis.mk.Python()
21 def gerrit = new com.mirantis.mk.Gerrit()
22 def git = new com.mirantis.mk.Git()
23 def changeAuthorName = "MCP-CI"
24 def changeAuthorEmail = "mcp-ci-jenkins@ci.mcp.mirantis.net"
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020025 def cred = common.getCredentials(credentialsID, 'key')
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020026 String gerritUser = cred.username
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020027 def gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0]
28 def metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/')
29 def gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0]
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020030 def workspace = common.getWorkspace()
31 def venvDir = "${workspace}/gitreview-venv"
32 def repoDir = "${venvDir}/repo"
33 def metadataDir = "${repoDir}/metadata"
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020034 def ChangeId
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020035 def commitMessage
36 def gitRemote
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020037 stage("Installing virtualenv") {
38 python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml'])
39 }
40 stage('Cleanup repo dir') {
41 dir(repoDir) {
42 deleteDir()
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020043 }
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020044 }
45 stage('Cloning release-metadata repository') {
46 git.checkoutGitRepository(repoDir, metadataRepoUrl, metadataGerritBranch, credentialsID, true, 10, 0)
47 dir(repoDir) {
48 gitRemote = sh(
49 script:
50 'git remote -v | head -n1 | cut -f1',
51 returnStdout: true,
52 ).trim()
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020053 }
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020054 }
55 stage('Creating CR') {
56 def gerritAuth = ['PORT': gerritPort, 'USER': gerritUser, 'HOST': gerritHost]
57 def changeParams = ['owner': gerritUser, 'status': 'open', 'project': metadataProject, 'branch': metadataGerritBranch, 'topic': crTopic]
58 def gerritChange = gerrit.findGerritChange(credentialsID, gerritAuth, changeParams)
59 git.changeGitBranch(repoDir, metadataGerritBranch)
60 if (gerritChange) {
61 def jsonChange = readJSON text: gerritChange
62 changeNum = jsonChange['number']
63 ChangeId = 'Change-Id: '
64 ChangeId += jsonChange['id']
65 //get existent change from gerrit
66 gerrit.getGerritChangeByNum(credentialsID, venvDir, repoDir, gitRemote, changeNum)
67 } else {
68 ChangeId = ''
69 git.createGitBranch(repoDir, crTopic)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020070 }
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020071 cmdText = "python ${repoDir}/utils/app.py --path ${metadataDir} update --key ${key} --value ${value}"
72 python.runVirtualenvCommand(venvDir, cmdText)
73 commitMessage =
74 """[oscore] Auto-update ${metadataProject}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020075
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020076 |${ChangeId}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020077 """.stripMargin()
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020078 //commit change
79 if (gerritChange) {
80 git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false, true)
81 } else {
82 git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020083 }
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020084 //post change
85 gerrit.postGerritReview(credentialsID, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020086 }
87}