blob: 011e248c2851a30f04b8942d7fd537de36e14999 [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
Vladimir Khlyunev34dbf982021-02-04 01:24:54 +040015doSubmit = false
azvyagintsev4edfe4f2019-02-08 11:51:13 +020016defGerritPort = env.GERRIT_PORT ?: '29418'
Jakub Josef4251feb2017-03-10 16:10:53 +010017
18@NonCPS
azvyagintsev5173bc52018-09-21 13:24:59 +030019def isJobExists(jobName) {
20 return Jenkins.instance.items.find { it -> it.name.equals(jobName) }
Mikhail Ivanovf5cd07f2017-04-11 17:09:54 +040021}
azvyagintsev5173bc52018-09-21 13:24:59 +030022
Denis Egorenkoc33cb5a2018-09-27 18:33:12 +040023def callJobWithExtraVars(String jobName) {
azvyagintsev4edfe4f2019-02-08 11:51:13 +020024 def gerritVars = env.getEnvironment().findAll { it.key.startsWith('GERRIT_') }
Denis Egorenko28f8c812018-12-10 16:06:37 +040025 gerritVars['GERRIT_CI_MERGE_TRIGGER'] = true
azvyagintsev3f678fd2018-09-28 19:21:45 +030026 testJob = build job: jobName, parameters: [
azvyagintsev4edfe4f2019-02-08 11:51:13 +020027 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: JsonOutput.toJson(gerritVars)]
Denis Egorenkoc33cb5a2018-09-27 18:33:12 +040028 ]
azvyagintsev3f678fd2018-09-28 19:21:45 +030029 if (testJob.getResult() != 'SUCCESS') {
30 error("Gate job ${testJob.getBuildUrl().toString()} finished with ${testJob.getResult()} !")
31 }
32 giveVerify = true
Denis Egorenkoc33cb5a2018-09-27 18:33:12 +040033}
34
azvyagintsev5173bc52018-09-21 13:24:59 +030035timeout(time: 12, unit: 'HOURS') {
36 node(slaveNode) {
Vladimir Khlyunev34dbf982021-02-04 01:24:54 +040037 def sshGerritConnectCmd = "ssh -p ${defGerritPort} ${GERRIT_NAME}@${GERRIT_HOST}"
38 def sshGerritQueryCmd = "${sshGerritConnectCmd} gerrit query --current-patch-set --format=JSON change:${GERRIT_CHANGE_NUMBER}"
39 def sshGerritReviewCmd = "${sshGerritConnectCmd} gerrit review ${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}"
40 def gerritChange = ''
azvyagintsev5173bc52018-09-21 13:24:59 +030041 try {
Vladimir Khlyunev34dbf982021-02-04 01:24:54 +040042 sshagent (credentials: [env.CREDENTIALS_ID]) {
43 // test if change is not already merged
44 ssh.ensureKnownHosts("${env.GERRIT_HOST}:${defGerritPort}")
45 gerritChange = common.parseJSON(sh(script: sshGerritQueryCmd, returnStdout: true))
46 def skipProjectsVerify = ['mk/docker-jnlp-slave']
arazumovskaya36864552019-05-17 13:16:36 +000047
Vladimir Khlyunev34dbf982021-02-04 01:24:54 +040048 stage("test") {
49 //notification about Start job
50 sh "${sshGerritReviewCmd} -m \"'Build Started $BUILD_URL'\""
51 //check Code-Review
52 if (!gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Code-Review", "+")) {
53 throw new Exception('Change don\'t have a CodeReview+1, reject gate')
54 }
55 //check Verify
56 if (!gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")) {
57 throw new Exception('Change don\'t have initial Verify+1, reject gate')
58 } else if (gerritChange.status != "MERGED" && !env.SKIP_TEST.toBoolean()) {
59 //Verify-label off
60 sh "$sshGerritReviewCmd --verified 0"
61 //Do stage (test)
62 doSubmit = true
63 def gerritProjectArray = GERRIT_PROJECT.tokenize("/")
64 def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1]
65 if (gerritProject in skipProjectsVerify) {
66 common.successMsg("Project ${gerritProject} doesn't require verify, skipping...")
67 giveVerify = true
arazumovskaya36864552019-05-17 13:16:36 +000068 } else {
Vladimir Khlyunev34dbf982021-02-04 01:24:54 +040069 def jobsNamespace = JOBS_NAMESPACE
70 def plural_namespaces = ['salt-formulas', 'salt-models']
71 // remove plural s on the end of job namespace
72 if (JOBS_NAMESPACE in plural_namespaces) {
73 jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1)
74 }
75 // salt-formulas tests have -latest on end of the name
76 if (JOBS_NAMESPACE.equals("salt-formulas")) {
77 gerritProject = gerritProject + "-latest"
78 }
79 def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject)
80 if (env.GERRIT_PROJECT == 'mk/cookiecutter-templates' || env.GERRIT_PROJECT == 'salt-models/reclass-system') {
81 callJobWithExtraVars('test-salt-model-ci-wrapper')
arazumovskaya36864552019-05-17 13:16:36 +000082 } else {
Vladimir Khlyunev34dbf982021-02-04 01:24:54 +040083 if (isJobExists(testJob)) {
84 common.infoMsg("Test job ${testJob} found, running")
85 build job: testJob, parameters: [
86 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"],
87 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
88 ]
89 giveVerify = true
90 } else {
91 common.infoMsg("Test job ${testJob} not found")
92 }
azvyagintseva04dc5a2019-05-20 11:41:09 +030093 }
arazumovskaya36864552019-05-17 13:16:36 +000094 }
95 }
Vladimir Khlyunev34dbf982021-02-04 01:24:54 +040096 else {
97 common.infoMsg('Test job skipped')
98 }
arazumovskaya36864552019-05-17 13:16:36 +000099 }
azvyagintsev5173bc52018-09-21 13:24:59 +0300100 }
arazumovskaya36864552019-05-17 13:16:36 +0000101
azvyagintsev5173bc52018-09-21 13:24:59 +0300102 stage("submit review") {
103 if (gerritChange.status == "MERGED") {
104 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
105 } else if (doSubmit) {
Vladimir Khlyunev34dbf982021-02-04 01:24:54 +0400106 sshagent (credentials: [env.CREDENTIALS_ID]) {
107 if (giveVerify) {
108 common.warningMsg("Change ${GERRIT_CHANGE_NUMBER} don't have a Verified, but tests were successful, so adding Verified and submitting")
109 sh "${sshGerritReviewCmd} --verified +1 --submit"
110 } else {
111 sh "${sshGerritReviewCmd} --submit"
112 }
113 common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
azvyagintsev5173bc52018-09-21 13:24:59 +0300114 }
azvyagintsev5173bc52018-09-21 13:24:59 +0300115 }
116 }
117 } catch (Throwable e) {
118 // If there was an error or exception thrown, the build failed
119 currentBuild.result = "FAILURE"
120 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
121 throw e
122 }
123 }
azvyagintsev3f678fd2018-09-28 19:21:45 +0300124}