Jakub Josef | f2a66e9 | 2017-03-08 17:21:07 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Gerrit gating pipeline |
| 3 | * CREDENTIALS_ID - Gerrit credentails ID |
Jakub Josef | b62d00a | 2017-03-13 12:21:40 +0100 | [diff] [blame] | 4 | * JOBS_NAMESPACE - Gerrit gating jobs namespace (mk, contrail, ...) |
Jakub Josef | f2a66e9 | 2017-03-08 17:21:07 +0100 | [diff] [blame] | 5 | * |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 6 | **/ |
Alexander Evseev | 02fe5eb | 2018-11-15 13:58:36 +0100 | [diff] [blame] | 7 | import groovy.json.JsonOutput |
Jakub Josef | f2a66e9 | 2017-03-08 17:21:07 +0100 | [diff] [blame] | 8 | |
| 9 | def common = new com.mirantis.mk.Common() |
| 10 | def gerrit = new com.mirantis.mk.Gerrit() |
Jakub Josef | eda0768 | 2017-03-09 14:50:54 +0100 | [diff] [blame] | 11 | def ssh = new com.mirantis.mk.Ssh() |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 12 | |
azvyagintsev | 003090f | 2018-12-14 12:32:27 +0200 | [diff] [blame] | 13 | slaveNode = env.SLAVE_NODE ?: 'virtual' |
azvyagintsev | 3f678fd | 2018-09-28 19:21:45 +0300 | [diff] [blame] | 14 | giveVerify = false |
azvyagintsev | 4edfe4f | 2019-02-08 11:51:13 +0200 | [diff] [blame] | 15 | defGerritPort = env.GERRIT_PORT ?: '29418' |
Jakub Josef | 4251feb | 2017-03-10 16:10:53 +0100 | [diff] [blame] | 16 | |
| 17 | @NonCPS |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 18 | def isJobExists(jobName) { |
| 19 | return Jenkins.instance.items.find { it -> it.name.equals(jobName) } |
Mikhail Ivanov | f5cd07f | 2017-04-11 17:09:54 +0400 | [diff] [blame] | 20 | } |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 21 | |
Denis Egorenko | c33cb5a | 2018-09-27 18:33:12 +0400 | [diff] [blame] | 22 | def callJobWithExtraVars(String jobName) { |
azvyagintsev | 4edfe4f | 2019-02-08 11:51:13 +0200 | [diff] [blame] | 23 | def gerritVars = env.getEnvironment().findAll { it.key.startsWith('GERRIT_') } |
Denis Egorenko | 28f8c81 | 2018-12-10 16:06:37 +0400 | [diff] [blame] | 24 | gerritVars['GERRIT_CI_MERGE_TRIGGER'] = true |
azvyagintsev | 3f678fd | 2018-09-28 19:21:45 +0300 | [diff] [blame] | 25 | testJob = build job: jobName, parameters: [ |
azvyagintsev | 4edfe4f | 2019-02-08 11:51:13 +0200 | [diff] [blame] | 26 | [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: JsonOutput.toJson(gerritVars)] |
Denis Egorenko | c33cb5a | 2018-09-27 18:33:12 +0400 | [diff] [blame] | 27 | ] |
azvyagintsev | 3f678fd | 2018-09-28 19:21:45 +0300 | [diff] [blame] | 28 | if (testJob.getResult() != 'SUCCESS') { |
| 29 | error("Gate job ${testJob.getBuildUrl().toString()} finished with ${testJob.getResult()} !") |
| 30 | } |
| 31 | giveVerify = true |
Denis Egorenko | c33cb5a | 2018-09-27 18:33:12 +0400 | [diff] [blame] | 32 | } |
| 33 | |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 34 | timeout(time: 12, unit: 'HOURS') { |
| 35 | node(slaveNode) { |
| 36 | try { |
| 37 | // test if change is not already merged |
azvyagintsev | 4edfe4f | 2019-02-08 11:51:13 +0200 | [diff] [blame] | 38 | ssh.prepareSshAgentKey(env.CREDENTIALS_ID) |
| 39 | // TODO: those should be refactored, and covered in gerrit module. |
| 40 | ssh.ensureKnownHosts("${env.GERRIT_HOST}:${defGerritPort}") |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 41 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true) |
| 42 | def doSubmit = false |
Denis Egorenko | 81a6585 | 2018-12-20 12:31:38 +0400 | [diff] [blame] | 43 | def skipProjectsVerify = ['mk/docker-jnlp-slave'] |
arazumovskaya | 3686455 | 2019-05-17 13:16:36 +0000 | [diff] [blame] | 44 | |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 45 | stage("test") { |
Anastasia Razumovskaya | 1ce07a6 | 2019-05-22 17:21:48 +0400 | [diff] [blame] | 46 | //notification about Start job |
| 47 | ssh.agentSh(String.format("ssh -p %s %s@%s gerrit review %s,%s -m \"'Build Started %s'\"", defGerritPort, GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER, BUILD_URL)) |
arazumovskaya | 3686455 | 2019-05-17 13:16:36 +0000 | [diff] [blame] | 48 | //check Code-Review |
azvyagintsev | a04dc5a | 2019-05-20 11:41:09 +0300 | [diff] [blame] | 49 | if (!gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Code-Review", "+")) { |
| 50 | throw new Exception('Change don\'t have a CodeReview+1, reject gate') |
Anastasia Razumovskaya | 0442a19 | 2019-05-15 14:35:49 +0400 | [diff] [blame] | 51 | } |
arazumovskaya | 3686455 | 2019-05-17 13:16:36 +0000 | [diff] [blame] | 52 | //check Verify |
| 53 | if (!gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")) { |
azvyagintsev | a04dc5a | 2019-05-20 11:41:09 +0300 | [diff] [blame] | 54 | throw new Exception('Change don\'t have initial Verify+1, reject gate') |
arazumovskaya | 3686455 | 2019-05-17 13:16:36 +0000 | [diff] [blame] | 55 | } else if (gerritChange.status != "MERGED" && !env.SKIP_TEST.toBoolean()) { |
| 56 | //Verify-label off |
azvyagintsev | a04dc5a | 2019-05-20 11:41:09 +0300 | [diff] [blame] | 57 | ssh.agentSh(String.format("ssh -p %s %s@%s gerrit review %s,%s --verified 0", defGerritPort, GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)) |
arazumovskaya | 3686455 | 2019-05-17 13:16:36 +0000 | [diff] [blame] | 58 | //Do stage (test) |
| 59 | doSubmit = true |
| 60 | def gerritProjectArray = GERRIT_PROJECT.tokenize("/") |
| 61 | def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1] |
| 62 | if (gerritProject in skipProjectsVerify) { |
| 63 | common.successMsg("Project ${gerritProject} doesn't require verify, skipping...") |
| 64 | giveVerify = true |
| 65 | } else { |
| 66 | def jobsNamespace = JOBS_NAMESPACE |
| 67 | def plural_namespaces = ['salt-formulas', 'salt-models'] |
| 68 | // remove plural s on the end of job namespace |
| 69 | if (JOBS_NAMESPACE in plural_namespaces) { |
| 70 | jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1) |
| 71 | } |
| 72 | // salt-formulas tests have -latest on end of the name |
| 73 | if (JOBS_NAMESPACE.equals("salt-formulas")) { |
| 74 | gerritProject = gerritProject + "-latest" |
| 75 | } |
| 76 | def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject) |
| 77 | if (env.GERRIT_PROJECT == 'mk/cookiecutter-templates' || env.GERRIT_PROJECT == 'salt-models/reclass-system') { |
| 78 | callJobWithExtraVars('test-salt-model-ci-wrapper') |
| 79 | } else { |
azvyagintsev | a04dc5a | 2019-05-20 11:41:09 +0300 | [diff] [blame] | 80 | if (isJobExists(testJob)) { |
arazumovskaya | 3686455 | 2019-05-17 13:16:36 +0000 | [diff] [blame] | 81 | common.infoMsg("Test job ${testJob} found, running") |
| 82 | def patchsetVerified = gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+") |
| 83 | build job: testJob, parameters: [ |
| 84 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"], |
| 85 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC] |
| 86 | ] |
| 87 | giveVerify = true |
| 88 | } else { |
azvyagintsev | a04dc5a | 2019-05-20 11:41:09 +0300 | [diff] [blame] | 89 | common.infoMsg("Test job ${testJob} not found") |
| 90 | } |
arazumovskaya | 3686455 | 2019-05-17 13:16:36 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
azvyagintsev | a04dc5a | 2019-05-20 11:41:09 +0300 | [diff] [blame] | 93 | } else { |
| 94 | common.infoMsg('Test job skipped') |
arazumovskaya | 3686455 | 2019-05-17 13:16:36 +0000 | [diff] [blame] | 95 | } |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 96 | } |
arazumovskaya | 3686455 | 2019-05-17 13:16:36 +0000 | [diff] [blame] | 97 | |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 98 | stage("submit review") { |
| 99 | if (gerritChange.status == "MERGED") { |
| 100 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them") |
| 101 | } else if (doSubmit) { |
| 102 | if (giveVerify) { |
| 103 | common.warningMsg("Change ${GERRIT_CHANGE_NUMBER} don't have a Verified, but tests were successful, so adding Verified and submitting") |
azvyagintsev | 4edfe4f | 2019-02-08 11:51:13 +0200 | [diff] [blame] | 104 | ssh.agentSh(String.format("ssh -p %s %s@%s gerrit review --verified +1 --submit %s,%s", defGerritPort, GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)) |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 105 | } else { |
azvyagintsev | 4edfe4f | 2019-02-08 11:51:13 +0200 | [diff] [blame] | 106 | ssh.agentSh(String.format("ssh -p %s %s@%s gerrit review --submit %s,%s", defGerritPort, GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)) |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 107 | } |
| 108 | common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)) |
| 109 | } |
| 110 | } |
| 111 | } catch (Throwable e) { |
| 112 | // If there was an error or exception thrown, the build failed |
| 113 | currentBuild.result = "FAILURE" |
| 114 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 115 | throw e |
| 116 | } |
| 117 | } |
azvyagintsev | 3f678fd | 2018-09-28 19:21:45 +0300 | [diff] [blame] | 118 | } |