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 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 19 | /** |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 20 | * Get Jenkins parameter names, values and types from jobName |
| 21 | * @param jobName job name |
| 22 | * @return Map with parameter names as keys and the following map as values: |
| 23 | * [ |
| 24 | * <str name1>: [type: <str cls1>, use_variable: <str name1>, defaultValue: <cls value1>], |
| 25 | * <str name2>: [type: <str cls2>, use_variable: <str name2>, defaultValue: <cls value2>], |
| 26 | * ] |
| 27 | */ |
| 28 | def getJobDefaultParameters(jobName) { |
| 29 | def jenkinsUtils = new com.mirantis.mk.JenkinsUtils() |
| 30 | def item = jenkinsUtils.getJobByName(env.JOB_NAME) |
| 31 | def parameters = [:] |
| 32 | def prop = item.getProperty(ParametersDefinitionProperty.class) |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 33 | if (prop != null) { |
| 34 | for (param in prop.getParameterDefinitions()) { |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 35 | def defaultParam = param.getDefaultParameterValue() |
| 36 | def cls = defaultParam.getClass().getName() |
| 37 | def value = defaultParam.getValue() |
| 38 | def name = defaultParam.getName() |
| 39 | parameters[name] = [type: cls, use_variable: name, defaultValue: value] |
| 40 | } |
| 41 | } |
| 42 | return parameters |
| 43 | } |
| 44 | |
| 45 | /** |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 46 | * Run a Jenkins job using the collected parameters |
| 47 | * |
| 48 | * @param job_name Name of the running job |
| 49 | * @param job_parameters Map that declares which values from global_variables should be used, in the following format: |
| 50 | * {'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] | 51 | * or |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 52 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'get_variable_from_url': <a key from global_variables which contains URL with required content>}, ...} |
| 53 | * or |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 54 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'use_template': <a GString multiline template with variables from global_variables>}, ...} |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 55 | * or |
| 56 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'get_variable_from_yaml': {'yaml_url': <URL with YAML content>, |
| 57 | * 'yaml_key': <a groovy-interpolating path to the key in the YAML, starting from dot '.'> } }, ...} |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 58 | * @param global_variables Map that keeps the artifact URLs and used 'env' objects: |
| 59 | * {'PARAM1_NAME': <param1 value>, 'PARAM2_NAME': 'http://.../artifacts/param2_value', ...} |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 60 | * @param propagate Boolean. If false: allows to collect artifacts after job is finished, even with FAILURE status |
| 61 | * If true: immediatelly fails the pipeline. DO NOT USE 'true' if you want to collect artifacts |
| 62 | * for 'finally' steps |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 63 | */ |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 64 | def runJob(job_name, job_parameters, global_variables, Boolean propagate = false) { |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 65 | def parameters = [] |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 66 | def common = new com.mirantis.mk.Common() |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 67 | def http = new com.mirantis.mk.Http() |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 68 | def engine = new groovy.text.GStringTemplateEngine() |
| 69 | def template |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 70 | def yamls_from_urls = [:] |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 71 | def base = [:] |
| 72 | base["url"] = '' |
| 73 | def variable_content |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 74 | |
| 75 | // Collect required parameters from 'global_variables' or 'env' |
| 76 | for (param in job_parameters) { |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 77 | if (param.value.containsKey('use_variable')) { |
| 78 | if (!global_variables[param.value.use_variable]) { |
| 79 | global_variables[param.value.use_variable] = env[param.value.use_variable] ?: '' |
| 80 | } |
| 81 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: global_variables[param.value.use_variable]]) |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 82 | common.infoMsg("${param.key}: <${param.value.type}> ${global_variables[param.value.use_variable]}") |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 83 | } else if (param.value.containsKey('get_variable_from_url')) { |
| 84 | if (!global_variables[param.value.get_variable_from_url]) { |
| 85 | global_variables[param.value.get_variable_from_url] = env[param.value.get_variable_from_url] ?: '' |
| 86 | } |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 87 | if (global_variables[param.value.get_variable_from_url]) { |
Dennis Dmitriev | 3782836 | 2019-11-11 18:06:49 +0200 | [diff] [blame] | 88 | 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] | 89 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: variable_content]) |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 90 | common.infoMsg("${param.key}: <${param.value.type}> ${variable_content}") |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 91 | } else { |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 92 | common.warningMsg("${param.key} is empty, skipping get_variable_from_url") |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 93 | } |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 94 | } else if (param.value.containsKey('get_variable_from_yaml')) { |
| 95 | if (param.value.get_variable_from_yaml.containsKey('yaml_url') && param.value.get_variable_from_yaml.containsKey('yaml_key')) { |
| 96 | // YAML url is stored in an environment or a global variable (like 'SI_CONFIG_ARTIFACT') |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 97 | def yaml_url_var = param.value.get_variable_from_yaml.yaml_url |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 98 | if (!global_variables[yaml_url_var]) { |
| 99 | global_variables[yaml_url_var] = env[yaml_url_var] ?: '' |
| 100 | } |
| 101 | yaml_url = global_variables[yaml_url_var] // Real YAML URL |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 102 | yaml_key = param.value.get_variable_from_yaml.yaml_key |
| 103 | // Key to get the data from YAML, to interpolate in the groovy, for example: |
| 104 | // <yaml_map_variable>.key.to.the[0].required.data , where yaml_key = '.key.to.the[0].required.data' |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 105 | if (yaml_url) { |
| 106 | if (!yamls_from_urls[yaml_url]) { |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 107 | common.infoMsg("Reading YAML from ${yaml_url} for ${param.key}") |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 108 | yaml_content = http.restGet(base, yaml_url) |
| 109 | yamls_from_urls[yaml_url] = readYaml text: yaml_content |
| 110 | } |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 111 | common.infoMsg("Getting key ${yaml_key} from YAML ${yaml_url} for ${param.key}") |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 112 | template_variables = [ |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 113 | 'yaml_data': yamls_from_urls[yaml_url] |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 114 | ] |
| 115 | request = "\${yaml_data${yaml_key}}" |
Dennis Dmitriev | 5e07671 | 2022-02-08 15:05:21 +0200 | [diff] [blame] | 116 | def result |
Dennis Dmitriev | 450cf73 | 2021-11-11 14:59:17 +0200 | [diff] [blame] | 117 | // Catch errors related to wrong key or index in the list or map objects |
| 118 | // For wrong key in map or wrong index in list, groovy returns <null> object, |
| 119 | // but it can be catched only after the string interpolation <template.toString()>, |
| 120 | // so we should catch the string 'null' instead of object <null>. |
| 121 | try { |
| 122 | template = engine.createTemplate(request).make(template_variables) |
Dennis Dmitriev | 5e07671 | 2022-02-08 15:05:21 +0200 | [diff] [blame] | 123 | result = template.toString() |
Dennis Dmitriev | 450cf73 | 2021-11-11 14:59:17 +0200 | [diff] [blame] | 124 | if (result == 'null') { |
| 125 | error "No such key or index, got 'null'" |
| 126 | } |
| 127 | } catch (e) { |
| 128 | error("Failed to get the key ${yaml_key} from YAML ${yaml_url}: " + e.toString()) |
| 129 | } |
| 130 | |
| 131 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: result]) |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 132 | common.infoMsg("${param.key}: <${param.value.type}>\n${result}") |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 133 | } else { |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 134 | common.warningMsg("'yaml_url' in ${param.key} is empty, skipping get_variable_from_yaml") |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 135 | } |
| 136 | } else { |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 137 | common.warningMsg("${param.key} missing 'yaml_url'/'yaml_key' parameters, skipping get_variable_from_yaml") |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 138 | } |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 139 | } else if (param.value.containsKey('use_template')) { |
| 140 | template = engine.createTemplate(param.value.use_template).make(global_variables) |
| 141 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: template.toString()]) |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 142 | common.infoMsg("${param.key}: <${param.value.type}>\n${template.toString()}") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 143 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Build the job |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 147 | def job_info = build job: "${job_name}", parameters: parameters, propagate: propagate |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 148 | return job_info |
| 149 | } |
| 150 | |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 151 | def runOrGetJob(job_name, job_parameters, global_variables, propagate, String fullTaskName = '') { |
| 152 | /** |
| 153 | * Run job directly or try to find already executed build |
| 154 | * Flow, in case CI_JOBS_OVERRIDES passed: |
| 155 | * |
| 156 | * |
| 157 | * CI_JOBS_OVERRIDES = text in yaml|json format |
| 158 | * CI_JOBS_OVERRIDES = 'kaas-testing-core-release-artifact' : 3505 |
| 159 | * 'reindex-testing-core-release-index-with-rc' : 2822 |
| 160 | * 'si-test-release-sanity-check-prepare-configuration': 1877 |
| 161 | */ |
| 162 | common = new com.mirantis.mk.Common() |
| 163 | def jobsOverrides = readYaml(text: env.CI_JOBS_OVERRIDES ?: '---') ?: [:] |
| 164 | // get id of overriding job |
| 165 | def jobOverrideID = jobsOverrides.getOrDefault(fullTaskName, '') |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 166 | if (fullTaskName in jobsOverrides.keySet()) { |
| 167 | common.warningMsg("Overriding: ${fullTaskName}/${job_name} <<< ${jobOverrideID}") |
| 168 | common.infoMsg("For debug pin use:\n'${fullTaskName}' : ${jobOverrideID}") |
| 169 | return Jenkins.instance.getItemByFullName(job_name, |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 170 | hudson.model.Job.class).getBuildByNumber(jobOverrideID.toInteger()) |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 171 | } else { |
| 172 | return runJob(job_name, job_parameters, global_variables, propagate) |
| 173 | } |
| 174 | } |
| 175 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 176 | /** |
| 177 | * Store URLs of the specified artifacts to the global_variables |
| 178 | * |
| 179 | * @param build_url URL of the completed job |
| 180 | * @param step_artifacts Map that contains artifact names in the job, and variable names |
| 181 | * where the URLs to that atrifacts should be stored, for example: |
| 182 | * {'ARTIFACT1': 'logs.tar.gz', 'ARTIFACT2': 'test_report.xml', ...} |
| 183 | * @param global_variables Map that will keep the artifact URLs. Variable 'ARTIFACT1', for example, |
| 184 | * be used in next job parameters: {'ARTIFACT1_URL':{ 'use_variable': 'ARTIFACT1', ...}} |
| 185 | * |
| 186 | * If the artifact with the specified name not found, the parameter ARTIFACT1_URL |
| 187 | * will be empty. |
| 188 | * |
| 189 | */ |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 190 | def storeArtifacts(build_url, step_artifacts, global_variables, job_name, build_num, artifactory_url = '') { |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 191 | def common = new com.mirantis.mk.Common() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 192 | def http = new com.mirantis.mk.Http() |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 193 | if (!artifactory_url) { |
| 194 | artifactory_url = 'https://artifactory.mcp.mirantis.net/api/storage/si-local/jenkins-job-artifacts' |
| 195 | } |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 196 | def baseJenkins = [:] |
| 197 | def baseArtifactory = [:] |
| 198 | build_url = build_url.replaceAll(~/\/+$/, "") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 199 | baseArtifactory["url"] = artifactory_url + "/${job_name}/${build_num}" |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 200 | baseJenkins["url"] = build_url |
| 201 | def job_config = http.restGet(baseJenkins, "/api/json/") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 202 | def job_artifacts = job_config['artifacts'] |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 203 | common.infoMsg("Attempt to storeArtifacts for: ${job_name}/${build_num}") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 204 | for (artifact in step_artifacts) { |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 205 | try { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 206 | def artifactoryResp = http.restGet(baseArtifactory, "/${artifact.value}") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 207 | global_variables[artifact.key] = artifactoryResp.downloadUri |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 208 | common.infoMsg("Artifact URL ${artifactoryResp.downloadUri} stored to ${artifact.key}") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 209 | continue |
| 210 | } catch (Exception e) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 211 | common.warningMsg("Can't find an artifact in ${artifactory_url}/${job_name}/${build_num}/${artifact.value} to store in ${artifact.key}\n" + |
| 212 | "error code ${e.message}") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 213 | } |
| 214 | |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 215 | def 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] | 216 | if (job_artifact.size() == 1) { |
| 217 | // Store artifact URL |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 218 | def artifact_url = "${build_url}/artifact/${job_artifact[0]['relativePath']}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 219 | global_variables[artifact.key] = artifact_url |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 220 | common.infoMsg("Artifact URL ${artifact_url} stored to ${artifact.key}") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 221 | } else if (job_artifact.size() > 1) { |
| 222 | // Error: too many artifacts with the same name, fail the job |
| 223 | error "Multiple artifacts ${artifact.value} for ${artifact.key} found in the build results ${build_url}, expected one:\n${job_artifact}" |
| 224 | } else { |
| 225 | // Warning: no artifact with expected name |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 226 | common.warningMsg("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] | 227 | global_variables[artifact.key] = '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 232 | /** |
| 233 | * Update workflow job build description |
| 234 | * |
| 235 | * @param jobs_data Map with all job names and result statuses, to showing it in description |
| 236 | */ |
| 237 | def updateDescription(jobs_data) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 238 | def common = new com.mirantis.mk.Common() |
| 239 | def table = '' |
| 240 | def child_jobs_description = '<strong>Descriptions from jobs:</strong><br>' |
| 241 | def table_template_start = "<div><table style='border: solid 1px;'><tr><th>Job:</th><th>Duration:</th><th>Status:</th></tr>" |
| 242 | def table_template_end = "</table></div>" |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 243 | |
| 244 | for (jobdata in jobs_data) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 245 | def trstyle = "<tr>" |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 246 | // Grey background for 'finally' jobs in list |
| 247 | if (jobdata['type'] == 'finally') { |
| 248 | trstyle = "<tr style='background: #DDDDDD;'>" |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 249 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 250 | // 'description' instead of job name if it exists |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 251 | def display_name = "'${jobdata['name']}': ${jobdata['build_id']}" |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 252 | if (jobdata['desc'].toString() != "") { |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 253 | display_name = "'${jobdata['desc']}': ${jobdata['build_id']}" |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 254 | } |
| 255 | |
azvyagintsev | 2eeaa56 | 2022-01-27 12:03:40 +0200 | [diff] [blame] | 256 | // Attach url for already built jobs |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 257 | def build_url = display_name |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 258 | if (jobdata['build_url'] != "0") { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 259 | build_url = "<a href=${jobdata['build_url']}>$display_name</a>" |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // Styling the status of job result |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 263 | switch (jobdata['status'].toString()) { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 264 | case "SUCCESS": |
| 265 | status_style = "<td style='color: green;'><img src='/images/16x16/blue.png' alt='SUCCESS'>" |
| 266 | break |
| 267 | case "UNSTABLE": |
| 268 | status_style = "<td style='color: #FF5733;'><img src='/images/16x16/yellow.png' alt='UNSTABLE'>" |
| 269 | break |
| 270 | case "ABORTED": |
| 271 | status_style = "<td style='color: red;'><img src='/images/16x16/aborted.png' alt='ABORTED'>" |
| 272 | break |
| 273 | case "NOT_BUILT": |
| 274 | status_style = "<td style='color: red;'><img src='/images/16x16/aborted.png' alt='NOT_BUILT'>" |
| 275 | break |
| 276 | case "FAILURE": |
| 277 | status_style = "<td style='color: red;'><img src='/images/16x16/red.png' alt='FAILURE'>" |
| 278 | break |
| 279 | default: |
| 280 | status_style = "<td>-" |
| 281 | } |
| 282 | |
| 283 | // Collect table |
azvyagintsev | 2eeaa56 | 2022-01-27 12:03:40 +0200 | [diff] [blame] | 284 | table += "$trstyle<td>$build_url</td><td>${jobdata['duration']}</td>$status_style</td></tr>" |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 285 | |
| 286 | // Collecting descriptions of builded child jobs |
| 287 | if (jobdata['child_desc'] != "") { |
| 288 | child_jobs_description += "<b><small><a href=${jobdata['build_url']}>- ${jobdata['name']} (${jobdata['status']}):</a></small></b><br>" |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 289 | // remove "null" message-result from description, but leave XXX:JOBRESULT in description |
| 290 | if (jobdata['child_desc'] != "null") { |
| 291 | child_jobs_description += "<small>${jobdata['child_desc']}</small><br>" |
| 292 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | currentBuild.description = table_template_start + table + table_template_end + child_jobs_description |
| 296 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 297 | |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 298 | def runStep(global_variables, step, Boolean propagate = false, artifactoryBaseUrl = '') { |
| 299 | return { |
| 300 | def common = new com.mirantis.mk.Common() |
| 301 | def engine = new groovy.text.GStringTemplateEngine() |
| 302 | |
| 303 | String jobDescription = step['description'] ?: '' |
| 304 | def jobName = step['job'] |
| 305 | def jobParameters = [:] |
| 306 | def stepParameters = step['parameters'] ?: [:] |
| 307 | if (step['inherit_parent_params'] ?: false) { |
| 308 | // add parameters from the current job for the child job |
| 309 | jobParameters << getJobDefaultParameters(env.JOB_NAME) |
| 310 | } |
| 311 | // add parameters from the workflow for the child job |
| 312 | jobParameters << stepParameters |
| 313 | def wfPauseStepBeforeRun = (step['wf_pause_step_before_run'] ?: false).toBoolean() |
| 314 | def wfPauseStepTimeout = (step['wf_pause_step_timeout'] ?: 10).toInteger() |
| 315 | def wfPauseStepSlackReportChannel = step['wf_pause_step_slack_report_channel'] ?: '' |
| 316 | |
| 317 | if (wfPauseStepBeforeRun) { |
| 318 | // Try-catch construction will allow to continue Steps, if timeout reached |
| 319 | try { |
| 320 | if (wfPauseStepSlackReportChannel) { |
| 321 | def slack = new com.mirantis.mcp.SlackNotification() |
| 322 | slack.jobResultNotification('wf_pause_step_before_run', |
| 323 | wfPauseStepSlackReportChannel, |
| 324 | env.JOB_NAME, null, |
| 325 | env.BUILD_URL, 'slack_webhook_url') |
| 326 | } |
| 327 | timeout(time: wfPauseStepTimeout, unit: 'MINUTES') { |
| 328 | input("Workflow pause requested before run: ${jobName}/${jobDescription}\n" + |
| 329 | "Timeout set to ${wfPauseStepTimeout}.\n" + |
| 330 | "Do you want to proceed workflow?") |
| 331 | } |
| 332 | } catch (err) { // timeout reached or input false |
| 333 | def user = err.getCauses()[0].getUser() |
| 334 | if (user.toString() != 'SYSTEM') { // SYSTEM means timeout. |
| 335 | error("Aborted after workFlow pause by: [${user}]") |
| 336 | } else { |
| 337 | common.infoMsg("Timeout finished, continue..") |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | common.infoMsg("Attempt to run: ${jobName}/${jobDescription}") |
| 342 | // Collect job parameters and run the job |
| 343 | // WARN(alexz): desc must not contain invalid chars for yaml |
| 344 | def jobResult = runOrGetJob(jobName, jobParameters, |
| 345 | global_variables, propagate, jobDescription) |
| 346 | def buildDuration = jobResult.durationString ?: '-' |
| 347 | if (buildDuration.toString() == null) { |
| 348 | buildDuration = '-' |
| 349 | } |
| 350 | def jobSummary = [ |
| 351 | job_result : jobResult.getResult().toString(), |
| 352 | build_url : jobResult.getAbsoluteUrl().toString(), |
| 353 | build_id : jobResult.getId().toString(), |
| 354 | buildDuration : buildDuration, |
| 355 | desc : engine.createTemplate(jobDescription).make(global_variables), |
| 356 | ] |
| 357 | def _buildDescription = jobResult.getDescription().toString() |
| 358 | if(_buildDescription){ |
| 359 | jobSummary['build_description'] = _buildDescription |
| 360 | } |
| 361 | // Store links to the resulting artifacts into 'global_variables' |
| 362 | storeArtifacts(jobSummary['build_url'], step['artifacts'], |
| 363 | global_variables, jobName, jobSummary['build_id'], artifactoryBaseUrl) |
| 364 | return jobSummary |
| 365 | } |
| 366 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 367 | /** |
| 368 | * Run the workflow or final steps one by one |
| 369 | * |
| 370 | * @param steps List of steps (Jenkins jobs) to execute |
| 371 | * @param global_variables Map where the collected artifact URLs and 'env' objects are stored |
| 372 | * @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] | 373 | * @param jobs_data Map with all job names and result statuses, to showing it in description |
| 374 | * @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] | 375 | * @param propagate Boolean. If false: allows to collect artifacts after job is finished, even with FAILURE status |
| 376 | * If true: immediatelly fails the pipeline. DO NOT USE 'true' with runScenario(). |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 377 | */ |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 378 | def runSteps(steps, global_variables, failed_jobs, jobs_data, step_id, Boolean propagate = false, artifactoryBaseUrl = '') { |
azvyagintsev | b673f39 | 2021-05-19 15:31:48 +0300 | [diff] [blame] | 379 | common = new com.mirantis.mk.Common() |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 380 | // Show expected jobs list in description |
| 381 | updateDescription(jobs_data) |
| 382 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 383 | for (step in steps) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 384 | stage("Preparing for run job ${step['job']}") { |
| 385 | def job_summary = runStep(global_variables, step, propagate, artifactoryBaseUrl).call() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 386 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 387 | // Update jobs_data for updating description |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 388 | jobs_data[step_id]['build_url'] = job_summary['build_url'] |
| 389 | jobs_data[step_id]['build_id'] = job_summary['build_id'] |
| 390 | jobs_data[step_id]['status'] = job_summary['job_result'] |
| 391 | jobs_data[step_id]['duration'] = job_summary['buildDuration'] |
| 392 | jobs_data[step_id]['desc'] = job_summary['desc'] |
| 393 | if (job_summary['build_description']) { |
| 394 | jobs_data[step_id]['child_desc'] = job_summary['build_description'] |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 395 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 396 | updateDescription(jobs_data) |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 397 | def job_result = job_summary['job_result'] |
| 398 | def build_url = job_summary['build_url'] |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 399 | |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 400 | // Check job result, in case of SUCCESS, move to next step. |
Mykyta Karpin | 0bd8bc6 | 2020-04-29 12:27:14 +0300 | [diff] [blame] | 401 | // 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] | 402 | // In other cases check flag ignore_failed, if true ignore any statuses and keep going additionally |
| 403 | // if skip_results is not set or set to false fail entrie workflow, otherwise succed. |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 404 | if (job_result != 'SUCCESS') { |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 405 | def ignoreStepResult = false |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 406 | switch (job_result) { |
| 407 | // In cases when job was waiting too long in queue or internal job logic allows to skip building, |
| 408 | // job may have NOT_BUILT status. In that case ignore_not_built flag can be used not to fail scenario. |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 409 | case "NOT_BUILT": |
| 410 | ignoreStepResult = step['ignore_not_built'] ?: false |
| 411 | break; |
Dmitry Tyzhnenko | a141270 | 2022-02-14 21:23:09 +0200 | [diff] [blame^] | 412 | case "UNSTABLE": |
| 413 | ignoreStepResult = step['ignore_unstable'] ?: false |
| 414 | break; |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 415 | default: |
| 416 | ignoreStepResult = step['ignore_failed'] ?: false |
Vasyl Saienko | e72b994 | 2021-03-04 10:54:49 +0200 | [diff] [blame] | 417 | if (ignoreStepResult && !step['skip_results'] ?: false) { |
| 418 | failed_jobs[build_url] = job_result |
| 419 | } |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 420 | } |
| 421 | if (!ignoreStepResult) { |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 422 | currentBuild.result = job_result |
| 423 | error "Job ${build_url} finished with result: ${job_result}" |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 424 | } |
| 425 | } |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 426 | common.infoMsg("Job ${build_url} finished with result: ${job_result}") |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 427 | } |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 428 | // Jump to next ID for updating next job data in description table |
| 429 | step_id++ |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 430 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Run the workflow scenario |
| 435 | * |
| 436 | * @param scenario: Map with scenario steps. |
| 437 | |
| 438 | * There are two keys in the scenario: |
| 439 | * workflow: contains steps to run deploy and test jobs |
| 440 | * finally: contains steps to run report and cleanup jobs |
| 441 | * |
| 442 | * Scenario execution example: |
| 443 | * |
| 444 | * scenario_yaml = """\ |
| 445 | * workflow: |
| 446 | * - job: deploy-kaas |
| 447 | * ignore_failed: false |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 448 | * description: "Management cluster ${KAAS_VERSION}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 449 | * parameters: |
| 450 | * KAAS_VERSION: |
| 451 | * type: StringParameterValue |
| 452 | * use_variable: KAAS_VERSION |
| 453 | * artifacts: |
| 454 | * KUBECONFIG_ARTIFACT: artifacts/management_kubeconfig |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 455 | * DEPLOYED_KAAS_VERSION: artifacts/management_version |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 456 | * |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 457 | * - job: create-child |
| 458 | * inherit_parent_params: true |
| 459 | * ignore_failed: false |
| 460 | * parameters: |
| 461 | * KUBECONFIG_ARTIFACT_URL: |
| 462 | * type: StringParameterValue |
| 463 | * use_variable: KUBECONFIG_ARTIFACT |
| 464 | * KAAS_VERSION: |
| 465 | * type: StringParameterValue |
| 466 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 467 | * RELEASE_NAME: |
| 468 | * type: StringParameterValue |
| 469 | * get_variable_from_yaml: |
| 470 | * yaml_url: SI_CONFIG_ARTIFACT |
| 471 | * yaml_key: .clusters[0].release_name |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 472 | * |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 473 | * - job: test-kaas-ui |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 474 | * ignore_not_built: false |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 475 | * parameters: |
| 476 | * KUBECONFIG_ARTIFACT_URL: |
| 477 | * type: StringParameterValue |
| 478 | * use_variable: KUBECONFIG_ARTIFACT |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 479 | * KAAS_VERSION: |
| 480 | * type: StringParameterValue |
| 481 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 482 | * artifacts: |
| 483 | * REPORT_SI_KAAS_UI: artifacts/test_kaas_ui_result.xml |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 484 | * finally: |
| 485 | * - job: testrail-report |
| 486 | * ignore_failed: true |
| 487 | * parameters: |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 488 | * KAAS_VERSION: |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 489 | * type: StringParameterValue |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 490 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 491 | * REPORTS_LIST: |
| 492 | * type: TextParameterValue |
| 493 | * use_template: | |
| 494 | * REPORT_SI_KAAS_UI: \$REPORT_SI_KAAS_UI |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 495 | * """ |
| 496 | * |
| 497 | * runScenario(scenario) |
| 498 | * |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 499 | * Scenario workflow keys: |
| 500 | * |
| 501 | * job: string. Jenkins job name |
| 502 | * 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] | 503 | * 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] | 504 | * 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 |
| 505 | * inherit_parent_params: bool. if true, provide all parameters from the parent job to the child job as defaults |
| 506 | * parameters: dict. parameters name and type to inherit from parent to child job, or from artifact to child job |
azvyagintsev | b3cd2a7 | 2022-01-17 23:41:34 +0200 | [diff] [blame] | 507 | * wf_pause_step_before_run: bool. Interactive pause exact step before run. |
| 508 | * wf_pause_step_slack_report_channel: If step paused, send message about it in slack. |
| 509 | * wf_pause_step_timeout: timeout im minutes to wait for manual unpause. |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 510 | */ |
| 511 | |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 512 | def runScenario(scenario, slackReportChannel = '', artifactoryBaseUrl = '') { |
Dennis Dmitriev | 79f3a2d | 2019-08-09 16:06:00 +0300 | [diff] [blame] | 513 | // Clear description before adding new messages |
| 514 | currentBuild.description = '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 515 | // Collect the parameters for the jobs here |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 516 | def global_variables = [:] |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 517 | // List of failed jobs to show at the end |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 518 | def failed_jobs = [:] |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 519 | // Jobs data to use for wf job build description |
| 520 | def jobs_data = [] |
| 521 | // Counter for matching step ID with cell ID in description table |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 522 | def step_id = 0 |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 523 | |
| 524 | // Generate expected list jobs for description |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 525 | def list_id = 0 |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 526 | for (step in scenario['workflow']) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 527 | def display_name = step['job'] |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 528 | if (step['description'] != null && step['description'].toString() != "") { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 529 | display_name = step['description'] |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 530 | } |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 531 | jobs_data.add([list_id : "$list_id", |
| 532 | type : "workflow", |
| 533 | name : "$display_name", |
| 534 | build_url : "0", |
| 535 | build_id : "-", |
| 536 | status : "-", |
| 537 | desc : "", |
azvyagintsev | 2eeaa56 | 2022-01-27 12:03:40 +0200 | [diff] [blame] | 538 | child_desc: "", |
| 539 | duration : '-']) |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 540 | list_id += 1 |
| 541 | } |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 542 | |
| 543 | def finally_step_id = list_id |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 544 | for (step in scenario['finally']) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 545 | def display_name = step['job'] |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 546 | if (step['description'] != null && step['description'].toString() != "") { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 547 | display_name = step['description'] |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 548 | } |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 549 | jobs_data.add([list_id : "$list_id", |
| 550 | type : "finally", |
| 551 | name : "$display_name", |
| 552 | build_url : "0", |
| 553 | build_id : "-", |
| 554 | status : "-", |
| 555 | desc : "", |
azvyagintsev | 2eeaa56 | 2022-01-27 12:03:40 +0200 | [diff] [blame] | 556 | child_desc: "", |
| 557 | duration : '-']) |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 558 | list_id += 1 |
| 559 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 560 | |
| 561 | try { |
| 562 | // Run the 'workflow' jobs |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 563 | runSteps(scenario['workflow'], global_variables, failed_jobs, jobs_data, step_id, false, artifactoryBaseUrl) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 564 | } catch (InterruptedException x) { |
| 565 | error "The job was aborted" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 566 | } catch (e) { |
| 567 | error("Build failed: " + e.toString()) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 568 | } finally { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 569 | // Switching to 'finally' step index |
| 570 | step_id = finally_step_id |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 571 | // Run the 'finally' jobs |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 572 | runSteps(scenario['finally'], global_variables, failed_jobs, jobs_data, step_id, false, artifactoryBaseUrl) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 573 | |
| 574 | if (failed_jobs) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 575 | def statuses = [] |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 576 | failed_jobs.each { |
sgudz | 74c8cdd | 2020-01-23 14:26:32 +0200 | [diff] [blame] | 577 | statuses += it.value |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 578 | } |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 579 | if (statuses.contains('FAILURE')) { |
| 580 | currentBuild.result = 'FAILURE' |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 581 | } else if (statuses.contains('UNSTABLE')) { |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 582 | currentBuild.result = 'UNSTABLE' |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 583 | } else { |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 584 | currentBuild.result = 'FAILURE' |
| 585 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 586 | println "Failed jobs: ${failed_jobs}" |
vnaumov | 68cba27 | 2020-05-20 11:24:02 +0200 | [diff] [blame] | 587 | } else { |
| 588 | currentBuild.result = 'SUCCESS' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 589 | } |
vnaumov | 5a6eb8a | 2020-03-31 11:16:54 +0200 | [diff] [blame] | 590 | |
| 591 | if (slackReportChannel) { |
| 592 | def slack = new com.mirantis.mcp.SlackNotification() |
| 593 | slack.jobResultNotification(currentBuild.result, slackReportChannel, '', null, '', 'slack_webhook_url') |
| 594 | } |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 595 | } // finally |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 596 | } |