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 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame^] | 15 | def 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 Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 20 | 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 Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame^] | 25 | def cred = common.getCredentials(credentialsID, 'key') |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 26 | String gerritUser = cred.username |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame^] | 27 | def gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0] |
| 28 | def metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/') |
| 29 | def gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0] |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 30 | def workspace = common.getWorkspace() |
| 31 | def venvDir = "${workspace}/gitreview-venv" |
| 32 | def repoDir = "${venvDir}/repo" |
| 33 | def metadataDir = "${repoDir}/metadata" |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame^] | 34 | def ChangeId |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 35 | def commitMessage |
| 36 | def gitRemote |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame^] | 37 | stage("Installing virtualenv") { |
| 38 | python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml']) |
| 39 | } |
| 40 | stage('Cleanup repo dir') { |
| 41 | dir(repoDir) { |
| 42 | deleteDir() |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 43 | } |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame^] | 44 | } |
| 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 Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 53 | } |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame^] | 54 | } |
| 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 Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 70 | } |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame^] | 71 | 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 Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 75 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame^] | 76 | |${ChangeId} |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 77 | """.stripMargin() |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame^] | 78 | //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 Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 83 | } |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame^] | 84 | //post change |
| 85 | gerrit.postGerritReview(credentialsID, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch) |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 86 | } |
| 87 | } |