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