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 |
Jakub Josef | 4251feb | 2017-03-10 16:10:53 +0100 | [diff] [blame] | 15 | |
| 16 | @NonCPS |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 17 | def isJobExists(jobName) { |
| 18 | return Jenkins.instance.items.find { it -> it.name.equals(jobName) } |
Mikhail Ivanov | f5cd07f | 2017-04-11 17:09:54 +0400 | [diff] [blame] | 19 | } |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 20 | |
Denis Egorenko | c33cb5a | 2018-09-27 18:33:12 +0400 | [diff] [blame] | 21 | def callJobWithExtraVars(String jobName) { |
Denis Egorenko | 28f8c81 | 2018-12-10 16:06:37 +0400 | [diff] [blame] | 22 | def gerritVars = env.getEnvironment().findAll{ it.key.startsWith('GERRIT_') } |
| 23 | gerritVars['GERRIT_CI_MERGE_TRIGGER'] = true |
azvyagintsev | 3f678fd | 2018-09-28 19:21:45 +0300 | [diff] [blame] | 24 | testJob = build job: jobName, parameters: [ |
Denis Egorenko | 28f8c81 | 2018-12-10 16:06:37 +0400 | [diff] [blame] | 25 | [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: JsonOutput.toJson(gerritVars) ] |
Denis Egorenko | c33cb5a | 2018-09-27 18:33:12 +0400 | [diff] [blame] | 26 | ] |
azvyagintsev | 3f678fd | 2018-09-28 19:21:45 +0300 | [diff] [blame] | 27 | if (testJob.getResult() != 'SUCCESS') { |
| 28 | error("Gate job ${testJob.getBuildUrl().toString()} finished with ${testJob.getResult()} !") |
| 29 | } |
| 30 | giveVerify = true |
Denis Egorenko | c33cb5a | 2018-09-27 18:33:12 +0400 | [diff] [blame] | 31 | } |
| 32 | |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 33 | |
| 34 | timeout(time: 12, unit: 'HOURS') { |
| 35 | node(slaveNode) { |
| 36 | try { |
| 37 | // test if change is not already merged |
| 38 | ssh.prepareSshAgentKey(CREDENTIALS_ID) |
| 39 | ssh.ensureKnownHosts(GERRIT_HOST) |
| 40 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true) |
| 41 | def doSubmit = false |
Denis Egorenko | 81a6585 | 2018-12-20 12:31:38 +0400 | [diff] [blame] | 42 | def skipProjectsVerify = ['mk/docker-jnlp-slave'] |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 43 | stage("test") { |
| 44 | if (gerritChange.status != "MERGED" && !SKIP_TEST.equals("true")) { |
| 45 | // test max CodeReview |
| 46 | if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Code-Review", "+")) { |
| 47 | doSubmit = true |
| 48 | def gerritProjectArray = GERRIT_PROJECT.tokenize("/") |
| 49 | def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1] |
Denis Egorenko | 81a6585 | 2018-12-20 12:31:38 +0400 | [diff] [blame] | 50 | if (gerritProject in skipProjectsVerify) { |
| 51 | common.successMsg("Project ${gerritProject} doesn't require verify, skipping...") |
| 52 | giveVerify = true |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 53 | } else { |
Denis Egorenko | 81a6585 | 2018-12-20 12:31:38 +0400 | [diff] [blame] | 54 | def jobsNamespace = JOBS_NAMESPACE |
| 55 | def plural_namespaces = ['salt-formulas', 'salt-models'] |
| 56 | // remove plural s on the end of job namespace |
| 57 | if (JOBS_NAMESPACE in plural_namespaces) { |
| 58 | jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1) |
| 59 | } |
| 60 | // salt-formulas tests have -latest on end of the name |
| 61 | if (JOBS_NAMESPACE.equals("salt-formulas")) { |
| 62 | gerritProject = gerritProject + "-latest" |
| 63 | } |
| 64 | def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject) |
| 65 | if (env.GERRIT_PROJECT == 'mk/cookiecutter-templates' || env.GERRIT_PROJECT == 'salt-models/reclass-system') { |
| 66 | callJobWithExtraVars('test-salt-model-ci-wrapper') |
Denis Egorenko | c33cb5a | 2018-09-27 18:33:12 +0400 | [diff] [blame] | 67 | } else { |
Denis Egorenko | 81a6585 | 2018-12-20 12:31:38 +0400 | [diff] [blame] | 68 | if (isJobExists(testJob)) { |
| 69 | common.infoMsg("Test job ${testJob} found, running") |
| 70 | def patchsetVerified = gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+") |
| 71 | build job: testJob, parameters: [ |
| 72 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"], |
| 73 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC] |
| 74 | ] |
| 75 | giveVerify = true |
| 76 | } else { |
| 77 | common.infoMsg("Test job ${testJob} not found") |
| 78 | } |
Denis Egorenko | c33cb5a | 2018-09-27 18:33:12 +0400 | [diff] [blame] | 79 | } |
azvyagintsev | 5173bc5 | 2018-09-21 13:24:59 +0300 | [diff] [blame] | 80 | } |
| 81 | } else { |
| 82 | common.errorMsg("Change don't have a CodeReview, skipping gate") |
| 83 | } |
| 84 | } else { |
| 85 | common.infoMsg("Test job skipped") |
| 86 | } |
| 87 | } |
| 88 | stage("submit review") { |
| 89 | if (gerritChange.status == "MERGED") { |
| 90 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them") |
| 91 | } else if (doSubmit) { |
| 92 | if (giveVerify) { |
| 93 | common.warningMsg("Change ${GERRIT_CHANGE_NUMBER} don't have a Verified, but tests were successful, so adding Verified and submitting") |
| 94 | ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --verified +1 --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)) |
| 95 | } else { |
| 96 | ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)) |
| 97 | } |
| 98 | common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)) |
| 99 | } |
| 100 | } |
| 101 | } catch (Throwable e) { |
| 102 | // If there was an error or exception thrown, the build failed |
| 103 | currentBuild.result = "FAILURE" |
| 104 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 105 | throw e |
| 106 | } |
| 107 | } |
azvyagintsev | 3f678fd | 2018-09-28 19:21:45 +0300 | [diff] [blame] | 108 | } |