Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Run a simple workflow |
| 6 | * |
| 7 | * Function runScenario() executes a sequence of jobs, like |
| 8 | * - Parameters for the jobs are taken from the 'env' object |
| 9 | * - URLs of artifacts from completed jobs may be passed |
| 10 | * as parameters to the next jobs. |
| 11 | * |
| 12 | * No constants, environment specific logic or other conditional dependencies. |
| 13 | * All the logic should be placed in the workflow jobs, and perform necessary |
| 14 | * actions depending on the job parameters. |
| 15 | * The runScenario() function only provides the |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | /** |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 21 | * Get Jenkins parameter names, values and types from jobName |
| 22 | * @param jobName job name |
| 23 | * @return Map with parameter names as keys and the following map as values: |
| 24 | * [ |
| 25 | * <str name1>: [type: <str cls1>, use_variable: <str name1>, defaultValue: <cls value1>], |
| 26 | * <str name2>: [type: <str cls2>, use_variable: <str name2>, defaultValue: <cls value2>], |
| 27 | * ] |
| 28 | */ |
| 29 | def getJobDefaultParameters(jobName) { |
| 30 | def jenkinsUtils = new com.mirantis.mk.JenkinsUtils() |
| 31 | def item = jenkinsUtils.getJobByName(env.JOB_NAME) |
| 32 | def parameters = [:] |
| 33 | def prop = item.getProperty(ParametersDefinitionProperty.class) |
| 34 | if(prop != null) { |
| 35 | for(param in prop.getParameterDefinitions()) { |
| 36 | def defaultParam = param.getDefaultParameterValue() |
| 37 | def cls = defaultParam.getClass().getName() |
| 38 | def value = defaultParam.getValue() |
| 39 | def name = defaultParam.getName() |
| 40 | parameters[name] = [type: cls, use_variable: name, defaultValue: value] |
| 41 | } |
| 42 | } |
| 43 | return parameters |
| 44 | } |
| 45 | |
| 46 | /** |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 47 | * Run a Jenkins job using the collected parameters |
| 48 | * |
| 49 | * @param job_name Name of the running job |
| 50 | * @param job_parameters Map that declares which values from global_variables should be used, in the following format: |
| 51 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'use_variable': <a key from global_variables>}, ...} |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 52 | * or |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 53 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'get_variable_from_url': <a key from global_variables which contains URL with required content>}, ...} |
| 54 | * or |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 55 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'use_template': <a GString multiline template with variables from global_variables>}, ...} |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 56 | * @param global_variables Map that keeps the artifact URLs and used 'env' objects: |
| 57 | * {'PARAM1_NAME': <param1 value>, 'PARAM2_NAME': 'http://.../artifacts/param2_value', ...} |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 58 | * @param propagate Boolean. If false: allows to collect artifacts after job is finished, even with FAILURE status |
| 59 | * If true: immediatelly fails the pipeline. DO NOT USE 'true' if you want to collect artifacts |
| 60 | * for 'finally' steps |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 61 | */ |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 62 | def runJob(job_name, job_parameters, global_variables, Boolean propagate = false) { |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 63 | def parameters = [] |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 64 | def http = new com.mirantis.mk.Http() |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 65 | def engine = new groovy.text.GStringTemplateEngine() |
| 66 | def template |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 67 | def base = [:] |
| 68 | base["url"] = '' |
| 69 | def variable_content |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 70 | |
| 71 | // Collect required parameters from 'global_variables' or 'env' |
| 72 | for (param in job_parameters) { |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 73 | if (param.value.containsKey('use_variable')) { |
| 74 | if (!global_variables[param.value.use_variable]) { |
| 75 | global_variables[param.value.use_variable] = env[param.value.use_variable] ?: '' |
| 76 | } |
| 77 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: global_variables[param.value.use_variable]]) |
| 78 | println "${param.key}: <${param.value.type}> ${global_variables[param.value.use_variable]}" |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 79 | } else if (param.value.containsKey('get_variable_from_url')) { |
| 80 | if (!global_variables[param.value.get_variable_from_url]) { |
| 81 | global_variables[param.value.get_variable_from_url] = env[param.value.get_variable_from_url] ?: '' |
| 82 | } |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 83 | if (global_variables[param.value.get_variable_from_url]) { |
Dennis Dmitriev | 3782836 | 2019-11-11 18:06:49 +0200 | [diff] [blame] | 84 | variable_content = http.restGet(base, global_variables[param.value.get_variable_from_url]).trim() |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 85 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: variable_content]) |
| 86 | println "${param.key}: <${param.value.type}> ${variable_content}" |
| 87 | } else { |
| 88 | println "${param.key} is empty, skipping get_variable_from_url" |
| 89 | } |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 90 | } else if (param.value.containsKey('use_template')) { |
| 91 | template = engine.createTemplate(param.value.use_template).make(global_variables) |
| 92 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: template.toString()]) |
| 93 | println "${param.key}: <${param.value.type}>\n${template.toString()}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 94 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // Build the job |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 98 | def job_info = build job: "${job_name}", parameters: parameters, propagate: propagate |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 99 | return job_info |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Store URLs of the specified artifacts to the global_variables |
| 104 | * |
| 105 | * @param build_url URL of the completed job |
| 106 | * @param step_artifacts Map that contains artifact names in the job, and variable names |
| 107 | * where the URLs to that atrifacts should be stored, for example: |
| 108 | * {'ARTIFACT1': 'logs.tar.gz', 'ARTIFACT2': 'test_report.xml', ...} |
| 109 | * @param global_variables Map that will keep the artifact URLs. Variable 'ARTIFACT1', for example, |
| 110 | * be used in next job parameters: {'ARTIFACT1_URL':{ 'use_variable': 'ARTIFACT1', ...}} |
| 111 | * |
| 112 | * If the artifact with the specified name not found, the parameter ARTIFACT1_URL |
| 113 | * will be empty. |
| 114 | * |
| 115 | */ |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 116 | def storeArtifacts(build_url, step_artifacts, global_variables, job_name, build_num) { |
| 117 | def common = new com.mirantis.mk.Common() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 118 | def http = new com.mirantis.mk.Http() |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 119 | def baseJenkins = [:] |
| 120 | def baseArtifactory = [:] |
| 121 | build_url = build_url.replaceAll(~/\/+$/, "") |
| 122 | artifactory_url = "https://artifactory.mcp.mirantis.net/api/storage/si-local/jenkins-job-artifacts" |
| 123 | baseArtifactory["url"] = artifactory_url + "/${job_name}/${build_num}" |
| 124 | |
| 125 | baseJenkins["url"] = build_url |
| 126 | def job_config = http.restGet(baseJenkins, "/api/json/") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 127 | def job_artifacts = job_config['artifacts'] |
| 128 | for (artifact in step_artifacts) { |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 129 | try { |
| 130 | artifactoryResp = http.restGet(baseArtifactory, "/${artifact.value}") |
| 131 | global_variables[artifact.key] = artifactoryResp.downloadUri |
| 132 | println "Artifact URL ${artifactoryResp.downloadUri} stored to ${artifact.key}" |
| 133 | continue |
| 134 | } catch (Exception e) { |
| 135 | common.warningMsg("Can't find an artifact in ${artifactory_url}/${job_name}/${build_num}/${artifact.value} error code ${e.message}") |
| 136 | } |
| 137 | |
| 138 | job_artifact = job_artifacts.findAll { item -> artifact.value == item['fileName'] || artifact.value == item['relativePath'] } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 139 | if (job_artifact.size() == 1) { |
| 140 | // Store artifact URL |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 141 | def artifact_url = "${build_url}/artifact/${job_artifact[0]['relativePath']}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 142 | global_variables[artifact.key] = artifact_url |
| 143 | println "Artifact URL ${artifact_url} stored to ${artifact.key}" |
| 144 | } else if (job_artifact.size() > 1) { |
| 145 | // Error: too many artifacts with the same name, fail the job |
| 146 | error "Multiple artifacts ${artifact.value} for ${artifact.key} found in the build results ${build_url}, expected one:\n${job_artifact}" |
| 147 | } else { |
| 148 | // Warning: no artifact with expected name |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 149 | println "Artifact ${artifact.value} for ${artifact.key} not found in the build results ${build_url} and in the artifactory ${artifactory_url}/${job_name}/${build_num}/, found the following artifacts in Jenkins:\n${job_artifacts}" |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 150 | global_variables[artifact.key] = '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 155 | /** |
| 156 | * Update workflow job build description |
| 157 | * |
| 158 | * @param jobs_data Map with all job names and result statuses, to showing it in description |
| 159 | */ |
| 160 | def updateDescription(jobs_data) { |
| 161 | table = '' |
| 162 | child_jobs_description = '<strong>Descriptions from jobs:</strong><br>' |
| 163 | table_template_start = "<div><table style='border: solid 1px;'><tr><th>Job:</th><th>Status:</th></tr>" |
| 164 | table_template_end = "</table></div>" |
| 165 | |
| 166 | for (jobdata in jobs_data) { |
| 167 | // Grey background for 'finally' jobs in list |
| 168 | if (jobdata['type'] == 'finally') { |
| 169 | trstyle = "<tr style='background: #DDDDDD;'>" |
| 170 | } else { |
| 171 | trstyle = "<tr>" |
| 172 | } |
| 173 | |
| 174 | // 'description' instead of job name if it exists |
| 175 | if(jobdata['desc'].toString() != "") { |
| 176 | display_name = jobdata['desc'] |
| 177 | } else { |
| 178 | display_name = jobdata['name'] |
| 179 | } |
| 180 | |
| 181 | // Attach url for already builded jobs |
| 182 | if(jobdata['build_url'] != "0") { |
| 183 | build_url = "<a href=${jobdata['build_url']}>$display_name</a>" |
| 184 | } else { |
| 185 | build_url = display_name |
| 186 | } |
| 187 | |
| 188 | // Styling the status of job result |
| 189 | switch(jobdata['status'].toString()) { |
| 190 | case "SUCCESS": |
| 191 | status_style = "<td style='color: green;'><img src='/images/16x16/blue.png' alt='SUCCESS'>" |
| 192 | break |
| 193 | case "UNSTABLE": |
| 194 | status_style = "<td style='color: #FF5733;'><img src='/images/16x16/yellow.png' alt='UNSTABLE'>" |
| 195 | break |
| 196 | case "ABORTED": |
| 197 | status_style = "<td style='color: red;'><img src='/images/16x16/aborted.png' alt='ABORTED'>" |
| 198 | break |
| 199 | case "NOT_BUILT": |
| 200 | status_style = "<td style='color: red;'><img src='/images/16x16/aborted.png' alt='NOT_BUILT'>" |
| 201 | break |
| 202 | case "FAILURE": |
| 203 | status_style = "<td style='color: red;'><img src='/images/16x16/red.png' alt='FAILURE'>" |
| 204 | break |
| 205 | default: |
| 206 | status_style = "<td>-" |
| 207 | } |
| 208 | |
| 209 | // Collect table |
| 210 | table += "$trstyle<td>$build_url</td>$status_style</td></tr>" |
| 211 | |
| 212 | // Collecting descriptions of builded child jobs |
| 213 | if (jobdata['child_desc'] != "") { |
| 214 | child_jobs_description += "<b><small><a href=${jobdata['build_url']}>- ${jobdata['name']} (${jobdata['status']}):</a></small></b><br>" |
| 215 | child_jobs_description += "<small>${jobdata['child_desc']}</small><br>" |
| 216 | } |
| 217 | } |
| 218 | currentBuild.description = table_template_start + table + table_template_end + child_jobs_description |
| 219 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * Run the workflow or final steps one by one |
| 223 | * |
| 224 | * @param steps List of steps (Jenkins jobs) to execute |
| 225 | * @param global_variables Map where the collected artifact URLs and 'env' objects are stored |
| 226 | * @param failed_jobs Map with failed job names and result statuses, to report it later |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 227 | * @param jobs_data Map with all job names and result statuses, to showing it in description |
| 228 | * @param step_id Counter for matching step ID with cell ID in description table |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 229 | * @param propagate Boolean. If false: allows to collect artifacts after job is finished, even with FAILURE status |
| 230 | * If true: immediatelly fails the pipeline. DO NOT USE 'true' with runScenario(). |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 231 | */ |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 232 | def runSteps(steps, global_variables, failed_jobs, jobs_data, step_id, Boolean propagate = false) { |
| 233 | // Show expected jobs list in description |
| 234 | updateDescription(jobs_data) |
| 235 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 236 | for (step in steps) { |
| 237 | stage("Running job ${step['job']}") { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 238 | def engine = new groovy.text.GStringTemplateEngine() |
| 239 | def desc = step['description'] ?: '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 240 | def job_name = step['job'] |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 241 | def job_parameters = [:] |
Dennis Dmitriev | 334eecd | 2020-04-30 14:32:45 +0300 | [diff] [blame] | 242 | def step_parameters = step['parameters'] ?: [:] |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 243 | if (step['inherit_parent_params'] ?: false) { |
| 244 | // add parameters from the current job for the child job |
| 245 | job_parameters << getJobDefaultParameters(env.JOB_NAME) |
| 246 | } |
| 247 | // add parameters from the workflow for the child job |
Dennis Dmitriev | 334eecd | 2020-04-30 14:32:45 +0300 | [diff] [blame] | 248 | job_parameters << step_parameters |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 249 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 250 | // Collect job parameters and run the job |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 251 | def job_info = runJob(job_name, job_parameters, global_variables, propagate) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 252 | def job_result = job_info.getResult() |
| 253 | def build_url = job_info.getAbsoluteUrl() |
| 254 | def build_description = job_info.getDescription() |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 255 | def build_id = job_info.getId() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 256 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 257 | // Update jobs_data for updating description |
| 258 | jobs_data[step_id]['build_url'] = build_url |
| 259 | jobs_data[step_id]['status'] = job_result |
| 260 | jobs_data[step_id]['desc'] = engine.createTemplate(desc).make(global_variables) |
| 261 | if (build_description) { |
| 262 | jobs_data[step_id]['child_desc'] = build_description |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 263 | } |
| 264 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 265 | updateDescription(jobs_data) |
| 266 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 267 | // Store links to the resulting artifacts into 'global_variables' |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 268 | storeArtifacts(build_url, step['artifacts'], global_variables, job_name, build_id) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 269 | |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 270 | // Check job result, in case of SUCCESS, move to next step. |
Mykyta Karpin | 0bd8bc6 | 2020-04-29 12:27:14 +0300 | [diff] [blame] | 271 | // In case job has status NOT_BUILT, fail the build or keep going depending on 'ignore_not_built' flag |
Vasyl Saienko | e72b994 | 2021-03-04 10:54:49 +0200 | [diff] [blame^] | 272 | // In other cases check flag ignore_failed, if true ignore any statuses and keep going additionally |
| 273 | // if skip_results is not set or set to false fail entrie workflow, otherwise succed. |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 274 | if (job_result != 'SUCCESS'){ |
| 275 | def ignoreStepResult = false |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 276 | switch(job_result) { |
| 277 | // In cases when job was waiting too long in queue or internal job logic allows to skip building, |
| 278 | // job may have NOT_BUILT status. In that case ignore_not_built flag can be used not to fail scenario. |
| 279 | case "NOT_BUILT": |
| 280 | ignoreStepResult = step['ignore_not_built'] ?: false |
| 281 | break; |
| 282 | default: |
| 283 | ignoreStepResult = step['ignore_failed'] ?: false |
Vasyl Saienko | e72b994 | 2021-03-04 10:54:49 +0200 | [diff] [blame^] | 284 | if (ignoreStepResult && !step['skip_results'] ?: false) { |
| 285 | failed_jobs[build_url] = job_result |
| 286 | } |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 287 | } |
| 288 | if (!ignoreStepResult) { |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 289 | currentBuild.result = job_result |
| 290 | error "Job ${build_url} finished with result: ${job_result}" |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 291 | } // if (!ignoreStepResult) |
| 292 | } // if (job_result != 'SUCCESS') |
| 293 | println "Job ${build_url} finished with result: ${job_result}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 294 | } // stage ("Running job ${step['job']}") |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 295 | // Jump to next ID for updating next job data in description table |
| 296 | step_id++ |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 297 | } // for (step in scenario['workflow']) |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Run the workflow scenario |
| 302 | * |
| 303 | * @param scenario: Map with scenario steps. |
| 304 | |
| 305 | * There are two keys in the scenario: |
| 306 | * workflow: contains steps to run deploy and test jobs |
| 307 | * finally: contains steps to run report and cleanup jobs |
| 308 | * |
| 309 | * Scenario execution example: |
| 310 | * |
| 311 | * scenario_yaml = """\ |
| 312 | * workflow: |
| 313 | * - job: deploy-kaas |
| 314 | * ignore_failed: false |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 315 | * description: "Management cluster ${KAAS_VERSION}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 316 | * parameters: |
| 317 | * KAAS_VERSION: |
| 318 | * type: StringParameterValue |
| 319 | * use_variable: KAAS_VERSION |
| 320 | * artifacts: |
| 321 | * KUBECONFIG_ARTIFACT: artifacts/management_kubeconfig |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 322 | * DEPLOYED_KAAS_VERSION: artifacts/management_version |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 323 | * |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 324 | * - job: create-child |
| 325 | * inherit_parent_params: true |
| 326 | * ignore_failed: false |
| 327 | * parameters: |
| 328 | * KUBECONFIG_ARTIFACT_URL: |
| 329 | * type: StringParameterValue |
| 330 | * use_variable: KUBECONFIG_ARTIFACT |
| 331 | * KAAS_VERSION: |
| 332 | * type: StringParameterValue |
| 333 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
| 334 | * |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 335 | * - job: test-kaas-ui |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 336 | * ignore_not_built: false |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 337 | * parameters: |
| 338 | * KUBECONFIG_ARTIFACT_URL: |
| 339 | * type: StringParameterValue |
| 340 | * use_variable: KUBECONFIG_ARTIFACT |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 341 | * KAAS_VERSION: |
| 342 | * type: StringParameterValue |
| 343 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 344 | * artifacts: |
| 345 | * REPORT_SI_KAAS_UI: artifacts/test_kaas_ui_result.xml |
| 346 | * |
| 347 | * finally: |
| 348 | * - job: testrail-report |
| 349 | * ignore_failed: true |
| 350 | * parameters: |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 351 | * KAAS_VERSION: |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 352 | * type: StringParameterValue |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 353 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 354 | * REPORTS_LIST: |
| 355 | * type: TextParameterValue |
| 356 | * use_template: | |
| 357 | * REPORT_SI_KAAS_UI: \$REPORT_SI_KAAS_UI |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 358 | * """ |
| 359 | * |
| 360 | * runScenario(scenario) |
| 361 | * |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 362 | * Scenario workflow keys: |
| 363 | * |
| 364 | * job: string. Jenkins job name |
| 365 | * ignore_failed: bool. if true, keep running the workflow jobs if the job is failed, but fail the workflow at finish |
Vasyl Saienko | e72b994 | 2021-03-04 10:54:49 +0200 | [diff] [blame^] | 366 | * skip_results: bool. if true, keep running the workflow jobs if the job is failed, but do not fail the workflow at finish. Makes sense only when ignore_failed is set. |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 367 | * ignore_not_built: bool. if true, keep running the workflow jobs if the job set own status to NOT_BUILT, do not fail the workflow at finish for such jobs |
| 368 | * inherit_parent_params: bool. if true, provide all parameters from the parent job to the child job as defaults |
| 369 | * parameters: dict. parameters name and type to inherit from parent to child job, or from artifact to child job |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 370 | */ |
| 371 | |
vnaumov | 5a6eb8a | 2020-03-31 11:16:54 +0200 | [diff] [blame] | 372 | def runScenario(scenario, slackReportChannel = '') { |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 373 | |
Dennis Dmitriev | 79f3a2d | 2019-08-09 16:06:00 +0300 | [diff] [blame] | 374 | // Clear description before adding new messages |
| 375 | currentBuild.description = '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 376 | // Collect the parameters for the jobs here |
| 377 | global_variables = [:] |
| 378 | // List of failed jobs to show at the end |
| 379 | failed_jobs = [:] |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 380 | // Jobs data to use for wf job build description |
| 381 | def jobs_data = [] |
| 382 | // Counter for matching step ID with cell ID in description table |
| 383 | step_id = 0 |
| 384 | |
| 385 | // Generate expected list jobs for description |
| 386 | list_id = 0 |
| 387 | for (step in scenario['workflow']) { |
| 388 | if(step['description'] != null && step['description'].toString() != "") { |
| 389 | display_name = step['description'] |
| 390 | } else { |
| 391 | display_name = step['job'] |
| 392 | } |
| 393 | jobs_data.add([list_id: "$list_id", type: "workflow", name: "$display_name", build_url: "0", status: "-", desc: "", child_desc: ""]) |
| 394 | list_id += 1 |
| 395 | } |
| 396 | finally_step_id = list_id |
| 397 | for (step in scenario['finally']) { |
| 398 | if(step['description'] != null && step['description'].toString() != "") { |
| 399 | display_name = step['description'] |
| 400 | } else { |
| 401 | display_name = step['job'] |
| 402 | } |
| 403 | jobs_data.add([list_id: "$list_id", type: "finally", name: "$display_name", build_url: "0", status: "-", desc: "", child_desc: ""]) |
| 404 | list_id += 1 |
| 405 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 406 | |
| 407 | try { |
| 408 | // Run the 'workflow' jobs |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 409 | runSteps(scenario['workflow'], global_variables, failed_jobs, jobs_data, step_id) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 410 | |
| 411 | } catch (InterruptedException x) { |
| 412 | error "The job was aborted" |
| 413 | |
| 414 | } catch (e) { |
| 415 | error("Build failed: " + e.toString()) |
| 416 | |
| 417 | } finally { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 418 | // Switching to 'finally' step index |
| 419 | step_id = finally_step_id |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 420 | // Run the 'finally' jobs |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 421 | runSteps(scenario['finally'], global_variables, failed_jobs, jobs_data, step_id) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 422 | |
| 423 | if (failed_jobs) { |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 424 | statuses = [] |
| 425 | failed_jobs.each { |
sgudz | 74c8cdd | 2020-01-23 14:26:32 +0200 | [diff] [blame] | 426 | statuses += it.value |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 427 | } |
| 428 | if (statuses.contains('FAILURE')) { |
| 429 | currentBuild.result = 'FAILURE' |
| 430 | } |
| 431 | else if (statuses.contains('UNSTABLE')) { |
| 432 | currentBuild.result = 'UNSTABLE' |
| 433 | } |
| 434 | else { |
| 435 | currentBuild.result = 'FAILURE' |
| 436 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 437 | println "Failed jobs: ${failed_jobs}" |
vnaumov | 68cba27 | 2020-05-20 11:24:02 +0200 | [diff] [blame] | 438 | } else { |
| 439 | currentBuild.result = 'SUCCESS' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 440 | } |
vnaumov | 5a6eb8a | 2020-03-31 11:16:54 +0200 | [diff] [blame] | 441 | |
| 442 | if (slackReportChannel) { |
| 443 | def slack = new com.mirantis.mcp.SlackNotification() |
| 444 | slack.jobResultNotification(currentBuild.result, slackReportChannel, '', null, '', 'slack_webhook_url') |
| 445 | } |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 446 | } // finally |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 447 | } |