Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 1 | /* |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 2 | Global CI wrapper for testing next projects: |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 3 | - salt-models/reclass-system |
| 4 | - mk/cookiecutter-templates |
Denis Egorenko | d71aaa0 | 2018-12-11 16:03:37 +0400 | [diff] [blame] | 5 | |
| 6 | Wrapper allows to test cross-project patches, based on |
| 7 | 'Depends-On: http://<gerrit_address>/<change_number>' key phrase |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | import groovy.json.JsonOutput |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame] | 11 | gerrit = new com.mirantis.mk.Gerrit() |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 12 | |
| 13 | cookiecutterTemplatesRepo='mk/cookiecutter-templates' |
| 14 | reclassSystemRepo='salt-models/reclass-system' |
Denis Egorenko | 6c6b08e | 2018-12-13 13:23:43 +0400 | [diff] [blame] | 15 | slaveNode = env.getProperty('SLAVE_NODE') ?: 'virtual' |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 16 | |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 17 | voteMatrix = [ |
| 18 | 'test-mk-cookiecutter-templates': true, |
| 19 | 'test-drivetrain': true, |
| 20 | 'oscore-test-cookiecutter-models': false, |
| 21 | 'test-salt-model-infra': true, |
| 22 | 'test-salt-model-mcp-virtual-lab': true, |
| 23 | ] |
| 24 | |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame] | 25 | baseGerritConfig = [:] |
Denis Egorenko | d71aaa0 | 2018-12-11 16:03:37 +0400 | [diff] [blame] | 26 | buildTestParams = [:] |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 27 | jobResultComments = [:] |
| 28 | commentLock = false |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame] | 29 | |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 30 | // post Gerrit review comment to patch |
| 31 | def setGerritReviewComment() { |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame] | 32 | if (baseGerritConfig) { |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 33 | while(commentLock) { |
| 34 | sleep 5 |
| 35 | } |
| 36 | commentLock = true |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame] | 37 | LinkedHashMap config = baseGerritConfig.clone() |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 38 | String jobResultComment = '' |
| 39 | jobResultComments.each { job, info -> |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 40 | String skipped = voteMatrix.get(job, 'true') ? '' : '(non-voting)' |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 41 | jobResultComment += "- ${job} ${info.url}console : ${info.status} ${skipped}".trim() + '\n' |
| 42 | } |
| 43 | config['message'] = sh(script: "echo '${jobResultComment}'", returnStdout: true).trim() |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame] | 44 | gerrit.postGerritComment(config) |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 45 | commentLock = false |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame] | 46 | } |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 47 | } |
| 48 | |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 49 | // get job parameters for YAML-based job parametrization |
| 50 | def yamlJobParameters(LinkedHashMap jobParams) { |
| 51 | return [ |
| 52 | [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: JsonOutput.toJson(jobParams) ] |
| 53 | ] |
| 54 | } |
| 55 | |
| 56 | // run needed job with params |
| 57 | def runTests(String jobName, ArrayList jobParams) { |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 58 | def propagateStatus = voteMatrix.get(jobName, true) |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 59 | return { |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 60 | def jobBuild = build job: jobName, propagate: false, parameters: jobParams |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 61 | jobResultComments[jobName] = [ 'url': jobBuild.absoluteUrl, 'status': jobBuild.result ] |
| 62 | setGerritReviewComment() |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 63 | if (propagateStatus && jobBuild.result == 'FAILURE') { |
| 64 | throw new Exception("Build ${jobName} is failed!") |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
Denis Egorenko | d71aaa0 | 2018-12-11 16:03:37 +0400 | [diff] [blame] | 69 | // set params based on depending patches |
| 70 | def setupDependingVars(LinkedHashMap dependingProjects) { |
| 71 | if (dependingProjects) { |
| 72 | if (dependingProjects.containsKey(reclassSystemRepo)) { |
| 73 | buildTestParams['RECLASS_SYSTEM_GIT_REF'] = dependingProjects[reclassSystemRepo].ref |
| 74 | buildTestParams['RECLASS_SYSTEM_BRANCH'] = dependingProjects[reclassSystemRepo].branch |
| 75 | } |
| 76 | if (dependingProjects.containsKey(cookiecutterTemplatesRepo)) { |
| 77 | buildTestParams['COOKIECUTTER_TEMPLATE_REF'] = dependingProjects[cookiecutterTemplatesRepo].ref |
| 78 | buildTestParams['COOKIECUTTER_TEMPLATE_BRANCH'] = dependingProjects[cookiecutterTemplatesRepo].branch |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 83 | timeout(time: 12, unit: 'HOURS') { |
| 84 | node(slaveNode) { |
| 85 | def common = new com.mirantis.mk.Common() |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 86 | |
Denis Egorenko | 28f8c81 | 2018-12-10 16:06:37 +0400 | [diff] [blame] | 87 | // Var EXTRA_VARIABLES_YAML contains any additional parameters for tests, |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 88 | // like manually specified Gerrit Refs/URLs, additional parameters and so on |
Denis Egorenko | 28f8c81 | 2018-12-10 16:06:37 +0400 | [diff] [blame] | 89 | def buildTestParamsYaml = env.getProperty('EXTRA_VARIABLES_YAML') |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 90 | if (buildTestParamsYaml) { |
| 91 | common.mergeEnv(env, buildTestParamsYaml) |
| 92 | buildTestParams = readYaml text: buildTestParamsYaml |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // init required job variables |
| 96 | LinkedHashMap job_env = env.getEnvironment().findAll { k, v -> v } |
| 97 | |
| 98 | // Gerrit parameters |
| 99 | String gerritCredentials = job_env.get('CREDENTIALS_ID', 'gerrit') |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 100 | String gerritRef = job_env.get('GERRIT_REFSPEC') |
| 101 | String gerritProject = job_env.get('GERRIT_PROJECT') |
| 102 | String gerritName = job_env.get('GERRIT_NAME') |
| 103 | String gerritScheme = job_env.get('GERRIT_SCHEME') |
| 104 | String gerritHost = job_env.get('GERRIT_HOST') |
| 105 | String gerritPort = job_env.get('GERRIT_PORT') |
| 106 | String gerritChangeNumber = job_env.get('GERRIT_CHANGE_NUMBER') |
| 107 | String gerritPatchSetNumber = job_env.get('GERRIT_PATCHSET_NUMBER') |
| 108 | String gerritBranch = job_env.get('GERRIT_BRANCH') |
Denis Egorenko | e581f9e | 2018-12-11 18:56:14 +0400 | [diff] [blame] | 109 | Boolean gateMode = job_env.get('GERRIT_CI_MERGE_TRIGGER', false).toBoolean() |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 110 | |
| 111 | // Common and manual build parameters |
| 112 | LinkedHashMap projectsMap = [:] |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 113 | String distribRevision = 'nightly' |
| 114 | //checking if the branch is from release |
| 115 | if (gerritBranch.startsWith('release')) { |
| 116 | def distribRevisionRelease = gerritBranch.tokenize('/')[-1] |
| 117 | if (!common.checkRemoteBinary([apt_mk_version: distribRevisionRelease]).linux_system_repo_url) { |
| 118 | common.infoMsg("Binary release ${distribRevisionRelease} does not exist on http://mirror.mirantis.com. Falling back to 'proposed'.") |
| 119 | distribRevision = 'proposed' |
| 120 | } |
| 121 | distribRevision = distribRevisionRelease |
| 122 | } |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 123 | ArrayList testModels = job_env.get('TEST_MODELS', 'mcp-virtual-lab,infra').split(',') |
| 124 | |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 125 | stage('Gerrit prepare') { |
| 126 | // check if change aren't already merged |
| 127 | def gerritChange = gerrit.getGerritChange(gerritName, gerritHost, gerritChangeNumber, gerritCredentials) |
| 128 | if (gerritChange.status == "MERGED") { |
| 129 | common.successMsg('Patch set is alredy merged, no need to test it') |
| 130 | currentBuild.result = 'SUCCESS' |
| 131 | return |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 132 | } |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 133 | buildTestParams << job_env.findAll { k,v -> k ==~ /GERRIT_.+/ } |
| 134 | baseGerritConfig = [ |
| 135 | 'gerritName': gerritName, |
| 136 | 'gerritHost': gerritHost, |
| 137 | 'gerritChangeNumber': gerritChangeNumber, |
| 138 | 'credentialsId': gerritCredentials, |
| 139 | 'gerritPatchSetNumber': gerritPatchSetNumber, |
| 140 | ] |
Denis Egorenko | d71aaa0 | 2018-12-11 16:03:37 +0400 | [diff] [blame] | 141 | LinkedHashMap gerritDependingProjects = gerrit.getDependentPatches(baseGerritConfig) |
| 142 | setupDependingVars(gerritDependingProjects) |
| 143 | ArrayList descriptionMsgs = [ |
| 144 | "Running with next parameters:", |
| 145 | "Ref for ${gerritProject} => ${gerritRef}", |
| 146 | "Branch for ${gerritProject} => ${gerritBranch}" |
| 147 | ] |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 148 | descriptionMsgs.add("Distrib revision => ${distribRevision}") |
Denis Egorenko | d71aaa0 | 2018-12-11 16:03:37 +0400 | [diff] [blame] | 149 | for(String project in gerritDependingProjects.keySet()) { |
| 150 | descriptionMsgs.add("---") |
| 151 | descriptionMsgs.add("Depending patch to ${project} found:") |
| 152 | descriptionMsgs.add("Ref for ${project} => ${gerritDependingProjects[project]['ref']}") |
| 153 | descriptionMsgs.add("Branch for ${project} => ${gerritDependingProjects[project]['branch']}") |
| 154 | } |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 155 | currentBuild.description = descriptionMsgs.join('<br/>') |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 156 | gerrit.gerritPatchsetCheckout([ |
| 157 | credentialsId: gerritCredentials |
| 158 | ]) |
| 159 | } |
| 160 | |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 161 | stage("Run tests") { |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 162 | def documentationOnly = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v .releasenotes", returnStatus: true) == 1 |
| 163 | if (documentationOnly) { |
| 164 | common.infoMsg("Tests skipped, documenation only changed!") |
| 165 | currentBuild.result = 'SUCCESS' |
| 166 | return |
| 167 | } |
| 168 | |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 169 | def branches = [:] |
Vasyl Saienko | b121eb8 | 2018-12-27 17:34:28 +0200 | [diff] [blame] | 170 | branches.failFast = false |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 171 | String branchJobName = '' |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 172 | |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 173 | if (gerritProject == reclassSystemRepo && gerritBranch == 'master') { |
Denis Egorenko | bbc1268 | 2018-12-11 18:38:43 +0400 | [diff] [blame] | 174 | 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'))\" \\;") |
Denis Egorenko | d71aaa0 | 2018-12-11 16:03:37 +0400 | [diff] [blame] | 175 | def defaultSystemURL = "${gerritScheme}://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}" |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 176 | for (int i = 0; i < testModels.size(); i++) { |
| 177 | def cluster = testModels[i] |
Denis Egorenko | d71aaa0 | 2018-12-11 16:03:37 +0400 | [diff] [blame] | 178 | def clusterGitUrl = defaultSystemURL.substring(0, defaultSystemURL.lastIndexOf("/") + 1) + cluster |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 179 | branchJobName = "test-salt-model-${cluster}" |
| 180 | def jobParams = [ |
| 181 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl], |
| 182 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"], |
Denis Egorenko | d71aaa0 | 2018-12-11 16:03:37 +0400 | [diff] [blame] | 183 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: defaultSystemURL ], |
| 184 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: gerritRef ], |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 185 | ] |
| 186 | branches[branchJobName] = runTests(branchJobName, jobParams) |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 187 | } |
| 188 | } |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 189 | if (gerritProject == reclassSystemRepo || gerritProject == cookiecutterTemplatesRepo) { |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 190 | branchJobName = 'test-mk-cookiecutter-templates' |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 191 | branches[branchJobName] = runTests(branchJobName, yamlJobParameters(buildTestParams)) |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 192 | } |
Denis Egorenko | 28f8c81 | 2018-12-10 16:06:37 +0400 | [diff] [blame] | 193 | |
| 194 | if (!gateMode) { |
| 195 | if (gerritProject == cookiecutterTemplatesRepo) { |
| 196 | branchJobName = 'test-drivetrain' |
| 197 | branches[branchJobName] = runTests(branchJobName, yamlJobParameters(buildTestParams)) |
| 198 | branchJobName = 'oscore-test-cookiecutter-models' |
| 199 | branches[branchJobName] = runTests(branchJobName, yamlJobParameters(buildTestParams)) |
| 200 | } |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 201 | } |
| 202 | |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 203 | branches.keySet().each { key -> |
| 204 | if (branches[key] instanceof Closure) { |
| 205 | jobResultComments[key] = [ 'url': job_env.get('BUILD_URL'), 'status': 'WAITING' ] |
| 206 | } |
| 207 | } |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 208 | setGerritReviewComment() |
Denis Egorenko | d8d13e8 | 2018-12-07 16:16:31 +0400 | [diff] [blame] | 209 | parallel branches |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |