blob: 04eafebe63ba636d1b23b2433b8862e29bf216e7 [file] [log] [blame]
Filip Pytlounfcce97c2017-03-07 14:06:07 +01001def gerrit = new com.mirantis.mk.Gerrit()
Filip Pytloun6a057302017-03-07 16:33:30 +01002def common = new com.mirantis.mk.Common()
Filip Pytloun19376a82017-03-07 12:29:00 +01003
Denis Egorenko30618012018-09-28 11:30:28 +04004// extraVarsYaml contains GERRIT_ vars from gate job
5// or will contain GERRIT_ vars from reclass-system patch
6def extraVarsYaml = env.EXTRA_VARIABLES_YAML ?: ''
7if (extraVarsYaml != '') {
8 common.mergeEnv(env, extraVarsYaml)
9} else {
10 extraVarsYaml = '\n---'
11 for (envVar in env.getEnvironment()) {
12 if (envVar.key.startsWith("GERRIT_")) {
13 extraVarsYaml += "\n${envVar.key}: '${envVar.value}'"
14 }
15 }
16}
azvyagintsevb266ad22018-09-11 12:11:11 +030017
azvyagintsev2f10cf52018-09-20 11:30:47 +030018def slaveNode = env.SLAVE_NODE ?: 'python&&docker'
19def gerritCredentials = env.CREDENTIALS_ID ?: 'gerrit'
azvyagintsevb266ad22018-09-11 12:11:11 +030020
azvyagintsev2f10cf52018-09-20 11:30:47 +030021def gerritRef = env.GERRIT_REFSPEC ?: null
Denis Egorenko7feef6f2018-09-27 17:49:55 +040022def defaultGitRef = env.DEFAULT_GIT_REF ?: null
23def defaultGitUrl = env.DEFAULT_GIT_URL ?: null
Filip Pytlounfcce97c2017-03-07 14:06:07 +010024
Jakub Josef4612c5d2017-03-30 16:04:26 +020025def checkouted = false
Jakub Joseffcb615e2017-04-10 14:34:40 +020026def merged = false
chnydad66d6fa2017-06-22 09:34:43 +020027def systemRefspec = "HEAD"
azvyagintsev2f10cf52018-09-20 11:30:47 +030028
Jakub Josefa63f9862018-01-11 17:58:38 +010029timeout(time: 12, unit: 'HOURS') {
azvyagintsevb266ad22018-09-11 12:11:11 +030030 node(slaveNode) {
azvyagintsev0c97ffc2018-09-11 11:55:58 +030031 try {
32 stage("Checkout") {
33 if (gerritRef) {
34 // job is triggered by Gerrit
35 // test if change aren't already merged
36 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, gerritCredentials)
37 merged = gerritChange.status == "MERGED"
38 if (!merged) {
39 checkouted = gerrit.gerritPatchsetCheckout([
40 credentialsId: gerritCredentials
41 ])
42 systemRefspec = GERRIT_REFSPEC
43 }
Denis Egorenko7feef6f2018-09-27 17:49:55 +040044 // change defaultGit variables if job triggered from Gerrit
45 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
46 } else if (defaultGitRef && defaultGitUrl) {
47 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", gerritCredentials)
azvyagintsev0c97ffc2018-09-11 11:55:58 +030048 }
49 }
50
51 stage("Test") {
52 if (merged) {
53 common.successMsg("Gerrit change is already merged, no need to test them")
54 } else {
55 if (checkouted) {
56
57 def documentationOnly = false
58 if (gerritRef) {
59 documentationOnly = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v .releasenotes", returnStatus: true) == 1
60 }
61
62 sh("git diff-tree --no-commit-id --diff-filter=d --name-only -r HEAD | grep .yml | xargs -I {} python -c \"import yaml; yaml.load(open('{}', 'r'))\" \\;")
63
64 def branches = [:]
65 def testModels = documentationOnly ? [] : TEST_MODELS.split(',')
azvyagintsev2f10cf52018-09-20 11:30:47 +030066 if (['master'].contains(env.GERRIT_BRANCH)) {
67 for (int i = 0; i < testModels.size(); i++) {
68 def cluster = testModels[i]
69 def clusterGitUrl = defaultGitUrl.substring(0, defaultGitUrl.lastIndexOf("/") + 1) + cluster
70 branches["${cluster}"] = {
71 build job: "test-salt-model-${cluster}", parameters: [
72 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl],
73 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"],
74 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: defaultGitUrl],
azvyagintsevb6ca9352018-09-20 13:10:55 +030075 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: systemRefspec]
azvyagintsev2f10cf52018-09-20 11:30:47 +030076 ]
77 }
azvyagintsev0c97ffc2018-09-11 11:55:58 +030078 }
azvyagintsev2f10cf52018-09-20 11:30:47 +030079 } else {
80 common.warningMsg("Tests for ${testModels} skipped!")
azvyagintsev0c97ffc2018-09-11 11:55:58 +030081 }
82 branches["cookiecutter"] = {
83 build job: "test-mk-cookiecutter-templates", parameters: [
azvyagintsevb266ad22018-09-11 12:11:11 +030084 [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_URL', value: defaultGitUrl],
Denis Egorenko30618012018-09-28 11:30:28 +040085 [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_GIT_REF', value: systemRefspec],
86 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: extraVarsYaml ]
azvyagintsev0c97ffc2018-09-11 11:55:58 +030087 ]
88 }
89 parallel branches
90 } else {
azvyagintsev2f10cf52018-09-20 11:30:47 +030091 error("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
azvyagintsev0c97ffc2018-09-11 11:55:58 +030092 }
93 }
94 }
95 } catch (Throwable e) {
azvyagintsev0c97ffc2018-09-11 11:55:58 +030096 currentBuild.result = "FAILURE"
97 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
98 throw e
99 } finally {
100 common.sendNotification(currentBuild.result, "", ["slack"])
Jakub Josefa63f9862018-01-11 17:58:38 +0100101 }
Filip Pytlounfcce97c2017-03-07 14:06:07 +0100102 }
Filip Pytloun19376a82017-03-07 12:29:00 +0100103}