blob: f49b801d4f69e365462778ae22bc62f71f7189a8 [file] [log] [blame]
Alexander Evseev02fe5eb2018-11-15 13:58:36 +01001import groovy.json.JsonOutput
2
Filip Pytlounfcce97c2017-03-07 14:06:07 +01003def gerrit = new com.mirantis.mk.Gerrit()
Filip Pytloun6a057302017-03-07 16:33:30 +01004def common = new com.mirantis.mk.Common()
Filip Pytloun19376a82017-03-07 12:29:00 +01005
Denis Egorenko30618012018-09-28 11:30:28 +04006// extraVarsYaml contains GERRIT_ vars from gate job
7// or will contain GERRIT_ vars from reclass-system patch
8def extraVarsYaml = env.EXTRA_VARIABLES_YAML ?: ''
9if (extraVarsYaml != '') {
10 common.mergeEnv(env, extraVarsYaml)
11} else {
Alexander Evseev02fe5eb2018-11-15 13:58:36 +010012 extraVarsYaml = JsonOutput.toJson(env.getEnvironment().findAll{ it.key.startsWith('GERRIT_') })
Denis Egorenko30618012018-09-28 11:30:28 +040013}
azvyagintsevb266ad22018-09-11 12:11:11 +030014
azvyagintsevaa7b43a2019-02-02 10:17:58 +020015//def slaveNode = env.SLAVE_NODE ?: 'python&&docker'
16def slaveNode = env.SLAVE_NODE ?: 'hardware'
azvyagintsev2f10cf52018-09-20 11:30:47 +030017def gerritCredentials = env.CREDENTIALS_ID ?: 'gerrit'
azvyagintsevb266ad22018-09-11 12:11:11 +030018
azvyagintsev2f10cf52018-09-20 11:30:47 +030019def gerritRef = env.GERRIT_REFSPEC ?: null
Denis Egorenko7feef6f2018-09-27 17:49:55 +040020def defaultGitRef = env.DEFAULT_GIT_REF ?: null
21def defaultGitUrl = env.DEFAULT_GIT_URL ?: null
Filip Pytlounfcce97c2017-03-07 14:06:07 +010022
Jakub Josef4612c5d2017-03-30 16:04:26 +020023def checkouted = false
Jakub Joseffcb615e2017-04-10 14:34:40 +020024def merged = false
chnydad66d6fa2017-06-22 09:34:43 +020025def systemRefspec = "HEAD"
azvyagintsev2f10cf52018-09-20 11:30:47 +030026
Jakub Josefa63f9862018-01-11 17:58:38 +010027timeout(time: 12, unit: 'HOURS') {
azvyagintsevb266ad22018-09-11 12:11:11 +030028 node(slaveNode) {
azvyagintsev0c97ffc2018-09-11 11:55:58 +030029 try {
30 stage("Checkout") {
31 if (gerritRef) {
32 // job is triggered by Gerrit
33 // test if change aren't already merged
34 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, gerritCredentials)
35 merged = gerritChange.status == "MERGED"
36 if (!merged) {
37 checkouted = gerrit.gerritPatchsetCheckout([
38 credentialsId: gerritCredentials
39 ])
40 systemRefspec = GERRIT_REFSPEC
41 }
Denis Egorenko7feef6f2018-09-27 17:49:55 +040042 // change defaultGit variables if job triggered from Gerrit
43 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
44 } else if (defaultGitRef && defaultGitUrl) {
45 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", gerritCredentials)
azvyagintsev0c97ffc2018-09-11 11:55:58 +030046 }
47 }
48
49 stage("Test") {
50 if (merged) {
51 common.successMsg("Gerrit change is already merged, no need to test them")
52 } else {
53 if (checkouted) {
54
55 def documentationOnly = false
56 if (gerritRef) {
57 documentationOnly = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v .releasenotes", returnStatus: true) == 1
58 }
59
60 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'))\" \\;")
61
62 def branches = [:]
63 def testModels = documentationOnly ? [] : TEST_MODELS.split(',')
azvyagintsev2f10cf52018-09-20 11:30:47 +030064 if (['master'].contains(env.GERRIT_BRANCH)) {
65 for (int i = 0; i < testModels.size(); i++) {
66 def cluster = testModels[i]
67 def clusterGitUrl = defaultGitUrl.substring(0, defaultGitUrl.lastIndexOf("/") + 1) + cluster
68 branches["${cluster}"] = {
69 build job: "test-salt-model-${cluster}", parameters: [
70 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl],
71 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"],
72 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: defaultGitUrl],
azvyagintsevb6ca9352018-09-20 13:10:55 +030073 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: systemRefspec]
azvyagintsev2f10cf52018-09-20 11:30:47 +030074 ]
75 }
azvyagintsev0c97ffc2018-09-11 11:55:58 +030076 }
azvyagintsev2f10cf52018-09-20 11:30:47 +030077 } else {
78 common.warningMsg("Tests for ${testModels} skipped!")
azvyagintsev0c97ffc2018-09-11 11:55:58 +030079 }
80 branches["cookiecutter"] = {
81 build job: "test-mk-cookiecutter-templates", parameters: [
azvyagintsevb266ad22018-09-11 12:11:11 +030082 [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_URL', value: defaultGitUrl],
Denis Egorenko30618012018-09-28 11:30:28 +040083 [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_GIT_REF', value: systemRefspec],
84 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: extraVarsYaml ]
azvyagintsev0c97ffc2018-09-11 11:55:58 +030085 ]
86 }
87 parallel branches
88 } else {
azvyagintsev2f10cf52018-09-20 11:30:47 +030089 error("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
azvyagintsev0c97ffc2018-09-11 11:55:58 +030090 }
91 }
92 }
93 } catch (Throwable e) {
azvyagintsev0c97ffc2018-09-11 11:55:58 +030094 currentBuild.result = "FAILURE"
95 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
96 throw e
97 } finally {
98 common.sendNotification(currentBuild.result, "", ["slack"])
Jakub Josefa63f9862018-01-11 17:58:38 +010099 }
Filip Pytlounfcce97c2017-03-07 14:06:07 +0100100 }
Filip Pytloun19376a82017-03-07 12:29:00 +0100101}