blob: 7cbdfa09b7b79ec9dd6f161bdfd5be8f1e5f9c92 [file] [log] [blame]
Jakub Joseff2a66e92017-03-08 17:21:07 +01001/**
2 * Gerrit gating pipeline
3 * CREDENTIALS_ID - Gerrit credentails ID
Jakub Josefb62d00a2017-03-13 12:21:40 +01004 * JOBS_NAMESPACE - Gerrit gating jobs namespace (mk, contrail, ...)
Jakub Joseff2a66e92017-03-08 17:21:07 +01005 *
azvyagintsev5173bc52018-09-21 13:24:59 +03006 **/
Alexander Evseev02fe5eb2018-11-15 13:58:36 +01007import groovy.json.JsonOutput
Jakub Joseff2a66e92017-03-08 17:21:07 +01008
9def common = new com.mirantis.mk.Common()
10def gerrit = new com.mirantis.mk.Gerrit()
Jakub Josefeda07682017-03-09 14:50:54 +010011def ssh = new com.mirantis.mk.Ssh()
azvyagintsev5173bc52018-09-21 13:24:59 +030012
azvyagintsev003090f2018-12-14 12:32:27 +020013slaveNode = env.SLAVE_NODE ?: 'virtual'
azvyagintsev3f678fd2018-09-28 19:21:45 +030014giveVerify = false
azvyagintsev4edfe4f2019-02-08 11:51:13 +020015defGerritPort = env.GERRIT_PORT ?: '29418'
Jakub Josef4251feb2017-03-10 16:10:53 +010016
17@NonCPS
azvyagintsev5173bc52018-09-21 13:24:59 +030018def isJobExists(jobName) {
19 return Jenkins.instance.items.find { it -> it.name.equals(jobName) }
Mikhail Ivanovf5cd07f2017-04-11 17:09:54 +040020}
azvyagintsev5173bc52018-09-21 13:24:59 +030021
Denis Egorenkoc33cb5a2018-09-27 18:33:12 +040022def callJobWithExtraVars(String jobName) {
azvyagintsev4edfe4f2019-02-08 11:51:13 +020023 def gerritVars = env.getEnvironment().findAll { it.key.startsWith('GERRIT_') }
Denis Egorenko28f8c812018-12-10 16:06:37 +040024 gerritVars['GERRIT_CI_MERGE_TRIGGER'] = true
azvyagintsev3f678fd2018-09-28 19:21:45 +030025 testJob = build job: jobName, parameters: [
azvyagintsev4edfe4f2019-02-08 11:51:13 +020026 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: JsonOutput.toJson(gerritVars)]
Denis Egorenkoc33cb5a2018-09-27 18:33:12 +040027 ]
azvyagintsev3f678fd2018-09-28 19:21:45 +030028 if (testJob.getResult() != 'SUCCESS') {
29 error("Gate job ${testJob.getBuildUrl().toString()} finished with ${testJob.getResult()} !")
30 }
31 giveVerify = true
Denis Egorenkoc33cb5a2018-09-27 18:33:12 +040032}
33
azvyagintsev5173bc52018-09-21 13:24:59 +030034timeout(time: 12, unit: 'HOURS') {
35 node(slaveNode) {
36 try {
37 // test if change is not already merged
azvyagintsev4edfe4f2019-02-08 11:51:13 +020038 ssh.prepareSshAgentKey(env.CREDENTIALS_ID)
39 // TODO: those should be refactored, and covered in gerrit module.
40 ssh.ensureKnownHosts("${env.GERRIT_HOST}:${defGerritPort}")
azvyagintsev5173bc52018-09-21 13:24:59 +030041 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
42 def doSubmit = false
Denis Egorenko81a65852018-12-20 12:31:38 +040043 def skipProjectsVerify = ['mk/docker-jnlp-slave']
arazumovskaya36864552019-05-17 13:16:36 +000044
azvyagintsev5173bc52018-09-21 13:24:59 +030045 stage("test") {
arazumovskaya36864552019-05-17 13:16:36 +000046 //check Code-Review
azvyagintseva04dc5a2019-05-20 11:41:09 +030047 if (!gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Code-Review", "+")) {
48 throw new Exception('Change don\'t have a CodeReview+1, reject gate')
Anastasia Razumovskaya0442a192019-05-15 14:35:49 +040049 }
arazumovskaya36864552019-05-17 13:16:36 +000050 //check Verify
51 if (!gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")) {
azvyagintseva04dc5a2019-05-20 11:41:09 +030052 throw new Exception('Change don\'t have initial Verify+1, reject gate')
arazumovskaya36864552019-05-17 13:16:36 +000053 } else if (gerritChange.status != "MERGED" && !env.SKIP_TEST.toBoolean()) {
54 //Verify-label off
azvyagintseva04dc5a2019-05-20 11:41:09 +030055 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))
arazumovskaya36864552019-05-17 13:16:36 +000056 //Do stage (test)
57 doSubmit = true
58 def gerritProjectArray = GERRIT_PROJECT.tokenize("/")
59 def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1]
60 if (gerritProject in skipProjectsVerify) {
61 common.successMsg("Project ${gerritProject} doesn't require verify, skipping...")
62 giveVerify = true
63 } else {
64 def jobsNamespace = JOBS_NAMESPACE
65 def plural_namespaces = ['salt-formulas', 'salt-models']
66 // remove plural s on the end of job namespace
67 if (JOBS_NAMESPACE in plural_namespaces) {
68 jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1)
69 }
70 // salt-formulas tests have -latest on end of the name
71 if (JOBS_NAMESPACE.equals("salt-formulas")) {
72 gerritProject = gerritProject + "-latest"
73 }
74 def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject)
75 if (env.GERRIT_PROJECT == 'mk/cookiecutter-templates' || env.GERRIT_PROJECT == 'salt-models/reclass-system') {
76 callJobWithExtraVars('test-salt-model-ci-wrapper')
77 } else {
azvyagintseva04dc5a2019-05-20 11:41:09 +030078 if (isJobExists(testJob)) {
arazumovskaya36864552019-05-17 13:16:36 +000079 common.infoMsg("Test job ${testJob} found, running")
80 def patchsetVerified = gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")
81 build job: testJob, parameters: [
82 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"],
83 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
84 ]
85 giveVerify = true
86 } else {
azvyagintseva04dc5a2019-05-20 11:41:09 +030087 common.infoMsg("Test job ${testJob} not found")
88 }
arazumovskaya36864552019-05-17 13:16:36 +000089 }
90 }
azvyagintseva04dc5a2019-05-20 11:41:09 +030091 } else {
92 common.infoMsg('Test job skipped')
arazumovskaya36864552019-05-17 13:16:36 +000093 }
azvyagintsev5173bc52018-09-21 13:24:59 +030094 }
arazumovskaya36864552019-05-17 13:16:36 +000095
azvyagintsev5173bc52018-09-21 13:24:59 +030096 stage("submit review") {
97 if (gerritChange.status == "MERGED") {
98 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
99 } else if (doSubmit) {
100 if (giveVerify) {
101 common.warningMsg("Change ${GERRIT_CHANGE_NUMBER} don't have a Verified, but tests were successful, so adding Verified and submitting")
azvyagintsev4edfe4f2019-02-08 11:51:13 +0200102 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))
azvyagintsev5173bc52018-09-21 13:24:59 +0300103 } else {
azvyagintsev4edfe4f2019-02-08 11:51:13 +0200104 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))
azvyagintsev5173bc52018-09-21 13:24:59 +0300105 }
106 common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
107 }
108 }
109 } catch (Throwable e) {
110 // If there was an error or exception thrown, the build failed
111 currentBuild.result = "FAILURE"
112 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
113 throw e
114 }
115 }
azvyagintsev3f678fd2018-09-28 19:21:45 +0300116}