Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * ReleaseWorkflow functions |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Update release metadata after image build |
Ruslan Gustomiasov | 5a7a307 | 2019-06-25 16:57:29 +0200 | [diff] [blame^] | 10 | * |
| 11 | * @param key metadata key |
| 12 | * @param value metadata value |
| 13 | * @param image image name |
| 14 | * @param imageRelease image release name |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
Ruslan Gustomiasov | 5a7a307 | 2019-06-25 16:57:29 +0200 | [diff] [blame^] | 17 | def updateReleaseMetadata(key, value, image, imageRelease){ |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 18 | def python = new com.mirantis.mk.Python() |
| 19 | def gerrit = new com.mirantis.mk.Gerrit() |
| 20 | def git = new com.mirantis.mk.Git() |
| 21 | def changeAuthorName = "MCP-CI" |
| 22 | def changeAuthorEmail = "mcp-ci-jenkins@ci.mcp.mirantis.net" |
| 23 | def cred = common.getCredentials(env["CREDENTIALS_ID"], 'key') |
| 24 | String gerritUser = cred.username |
| 25 | def gerritHost = env["METADATA_REPO_URL.tokenize"]('@')[-1].tokenize(':')[0] |
| 26 | def metadataProject = env["METADATA_REPO_URL"].tokenize('/')[-2..-1].join('/') |
| 27 | def gerritPort = env["METADATA_REPO_URL"].tokenize(':')[-1].tokenize('/')[0] |
| 28 | def workspace = common.getWorkspace() |
| 29 | def venvDir = "${workspace}/gitreview-venv" |
| 30 | def repoDir = "${venvDir}/repo" |
| 31 | def metadataDir = "${repoDir}/metadata" |
| 32 | def imageChangeId |
| 33 | def commitMessage |
| 34 | def gitRemote |
| 35 | if (env["RELEASE_METADATA_CR"].toBoolean()) { |
| 36 | stage("Installing virtualenv") { |
| 37 | python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml']) |
| 38 | } |
| 39 | |
| 40 | stage('Cleanup repo dir') { |
| 41 | dir(repoDir) { |
| 42 | deleteDir() |
| 43 | } |
| 44 | } |
| 45 | stage('Cloning release-metadata repository') { |
Ruslan Gustomiasov | 5a7a307 | 2019-06-25 16:57:29 +0200 | [diff] [blame^] | 46 | git.checkoutGitRepository(repoDir, env["METADATA_REPO_URL"], env["METADATA_GERRIT_BRANCH"], env["CREDENTIALS_ID"], true, 10, 0) |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 47 | dir(repoDir) { |
| 48 | gitRemote = sh( |
| 49 | script: |
| 50 | 'git remote -v | head -n1 | cut -f1', |
| 51 | returnStdout: true, |
| 52 | ).trim() |
| 53 | } |
| 54 | } |
| 55 | stage('Creating CRs') { |
| 56 | for (openstackRelease in resultBuiltImages.keySet()) { |
| 57 | def crTopic = "nightly_update_images_" + openstackRelease |
| 58 | //Check if CR already exist |
| 59 | |
| 60 | def gerritAuth = ['PORT': gerritPort, 'USER': gerritUser, 'HOST': gerritHost] |
| 61 | def changeParams = ['owner': gerritUser, 'status': 'open', 'project': metadataProject, 'branch': env["METADATA_GERRIT_BRANCH"], 'topic': crTopic] |
| 62 | def gerritChange = gerrit.findGerritChange(env["CREDENTIALS_ID"], gerritAuth, changeParams) |
| 63 | |
| 64 | git.changeGitBranch(repoDir, env["METADATA_GERRIT_BRANCH"]) |
| 65 | if (gerritChange) { |
| 66 | def jsonChange = readJSON text: gerritChange |
| 67 | changeNum = jsonChange['number'] |
| 68 | imageChangeId = 'Change-Id: ' |
| 69 | imageChangeId += jsonChange['id'] |
| 70 | //get existent change from gerrit |
| 71 | gerrit.getGerritChangeByNum(env["CREDENTIALS_ID"], venvDir, repoDir, gitRemote, changeNum) |
| 72 | } else { |
| 73 | imageChangeId = '' |
| 74 | git.createGitBranch(repoDir, crTopic) |
| 75 | } |
| 76 | |
| 77 | for (component in resultBuiltImages[openstackRelease].keySet()) { |
| 78 | resultBuiltImages[openstackRelease][component].each { |
Ruslan Gustomiasov | 5a7a307 | 2019-06-25 16:57:29 +0200 | [diff] [blame^] | 79 | cmdText = "python ${repoDir}/utils/app.py --path ${metadataDir} update --key images:${image}:${imageRelease}:${component}:${key} --value ${value}" |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 80 | python.runVirtualenvCommand(venvDir, cmdText) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | commitMessage = |
| 85 | """[oscore] Auto-update ${metadataProject} |
| 86 | |
| 87 | |${imageChangeId} |
| 88 | """.stripMargin() |
| 89 | //commit change |
| 90 | if (gerritChange) { |
| 91 | git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false, true) |
| 92 | } else { |
| 93 | git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false) |
| 94 | } |
| 95 | //post change |
| 96 | gerrit.postGerritReview(CREDENTIALS_ID, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, METADATA_GERRIT_BRANCH) |
| 97 | |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |