Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 1 | package com.mirantis.mk |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 2 | /** |
| 3 | * ReleaseWorkflow functions |
| 4 | * |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * Update release metadata after image build |
Ruslan Gustomiasov | cef1bb4 | 2019-06-25 17:00:13 +0200 | [diff] [blame] | 9 | * |
| 10 | * @param key metadata key |
| 11 | * @param value metadata value |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 12 | * @param params string map with credentialsID, metadataRepoUrl, metadataGerritBranch and crTopic |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Alexandr Lovtsov | 35cdebc | 2019-07-01 21:32:03 +0500 | [diff] [blame] | 15 | def updateReleaseMetadata(String key, String value, Map params) { |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 16 | credentialsID = params['credentialsID'] ?: "mcp-ci-gerrit" |
Mykyta Karpin | 2845792 | 2019-07-05 15:14:58 +0300 | [diff] [blame^] | 17 | metadataRepoUrl = params['metadataRepoUrl'] ?: "ssh://mcp-ci-gerrit@gerrit.mcp.mirantis.net:29418/mcp/release-metadata" |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 18 | metadataGerritBranch = params['metadataGerritBranch'] ?: "master" |
Ruslan Gustomiasov | 2e9e345 | 2019-06-27 14:29:31 +0200 | [diff] [blame] | 19 | comment = params['comment'] ?: "" |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 20 | crTopic = params['crTopic'] ?: "" |
Alexandr Lovtsov | 35cdebc | 2019-07-01 21:32:03 +0500 | [diff] [blame] | 21 | def common = new com.mirantis.mk.Common() |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 22 | 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 Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 27 | def cred = common.getCredentials(credentialsID, 'key') |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 28 | String gerritUser = cred.username |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 29 | def gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0] |
| 30 | def metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/') |
| 31 | def gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0] |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 32 | def workspace = common.getWorkspace() |
| 33 | def venvDir = "${workspace}/gitreview-venv" |
| 34 | def repoDir = "${venvDir}/repo" |
| 35 | def metadataDir = "${repoDir}/metadata" |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 36 | def ChangeId |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 37 | def commitMessage |
| 38 | def gitRemote |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 39 | stage("Installing virtualenv") { |
| 40 | python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml']) |
| 41 | } |
| 42 | stage('Cleanup repo dir') { |
| 43 | dir(repoDir) { |
| 44 | deleteDir() |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 45 | } |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 46 | } |
| 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 Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 55 | } |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 56 | } |
| 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 Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 72 | } |
Alexandr Lovtsov | c1cc750 | 2019-07-02 12:41:53 +0500 | [diff] [blame] | 73 | cmdText = "python '${repoDir}/utils/app.py' --path '${metadataDir}' update --key '${key}' --value '${value}'" |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 74 | python.runVirtualenvCommand(venvDir, cmdText) |
| 75 | commitMessage = |
Ruslan Gustomiasov | 2e9e345 | 2019-06-27 14:29:31 +0200 | [diff] [blame] | 76 | """${comment} |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 77 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 78 | |${ChangeId} |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 79 | """.stripMargin() |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 80 | //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 Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 85 | } |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 86 | //post change |
| 87 | gerrit.postGerritReview(credentialsID, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch) |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 88 | } |
Alexandr Lovtsov | 35cdebc | 2019-07-01 21:32:03 +0500 | [diff] [blame] | 89 | } |