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 | 38a45cd | 2023-02-27 14:22:13 +0200 | [diff] [blame] | 20 | * Print 'global_variables' accumulated during workflow execution, including |
| 21 | * collected artifacts. |
| 22 | * Output is prepared in format that can be copy-pasted into groovy code |
| 23 | * to replay the workflow using the already created artifacts. |
| 24 | * |
| 25 | * @param global_variables Map that keeps the artifact URLs and used 'env' objects: |
| 26 | * {'PARAM1_NAME': <param1 value>, 'PARAM2_NAME': 'http://.../artifacts/param2_value', ...} |
| 27 | */ |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 28 | def printVariables(global_variables, Boolean yamlStyle = true) { |
| 29 | def common = new com.mirantis.mk.Common() |
| 30 | def mcpcommon = new com.mirantis.mcp.Common() |
| 31 | def global_variables_msg = '' |
| 32 | if (yamlStyle) { |
| 33 | global_variables_msg = mcpcommon.dumpYAML(global_variables) |
| 34 | } else { |
| 35 | for (variable in global_variables) { |
| 36 | global_variables_msg += "env.${variable.key}=\"\"\"${variable.value}\"\"\"\n" |
| 37 | } |
Dennis Dmitriev | 38a45cd | 2023-02-27 14:22:13 +0200 | [diff] [blame] | 38 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 39 | def message = "// Collected global_variables during the workflow:\n${global_variables_msg}" |
Dennis Dmitriev | 38a45cd | 2023-02-27 14:22:13 +0200 | [diff] [blame] | 40 | common.warningMsg(message) |
| 41 | } |
| 42 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Print stack trace to the console |
| 46 | */ |
| 47 | def printStackTrace(e, String prefix = 'at com.mirantis') { |
| 48 | def common = new com.mirantis.mk.Common() |
| 49 | StringWriter writer = new StringWriter() |
| 50 | e.printStackTrace(new PrintWriter(writer)) |
| 51 | String stackTrace = writer |
| 52 | |
| 53 | // Filter the stacktrace to show only the lines related to the specified library |
| 54 | String[] lines = stackTrace.split("\n") |
| 55 | String stackTraceFiltered = '' |
| 56 | Boolean filteredLine = false |
| 57 | for (String line in lines) { |
| 58 | if (line.contains('at ') && line.contains(prefix)) { |
| 59 | if (!filteredLine) { |
| 60 | stackTraceFiltered += "...\n" |
| 61 | filteredLine = true |
| 62 | } |
| 63 | stackTraceFiltered += "${line}\n" |
| 64 | } |
| 65 | else if (!line.contains('at ')) { |
| 66 | if (filteredLine) { |
| 67 | stackTraceFiltered += "...\n" |
| 68 | filteredLine = false |
| 69 | } |
| 70 | stackTraceFiltered += "${line}\n" |
| 71 | } |
| 72 | } |
| 73 | common.errorMsg("Stack trace:\n${stackTraceFiltered}") |
| 74 | } |
| 75 | |
| 76 | |
Dennis Dmitriev | 38a45cd | 2023-02-27 14:22:13 +0200 | [diff] [blame] | 77 | /** |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 78 | * Get Jenkins parameter names, values and types from jobName |
| 79 | * @param jobName job name |
| 80 | * @return Map with parameter names as keys and the following map as values: |
| 81 | * [ |
| 82 | * <str name1>: [type: <str cls1>, use_variable: <str name1>, defaultValue: <cls value1>], |
| 83 | * <str name2>: [type: <str cls2>, use_variable: <str name2>, defaultValue: <cls value2>], |
| 84 | * ] |
| 85 | */ |
| 86 | def getJobDefaultParameters(jobName) { |
| 87 | def jenkinsUtils = new com.mirantis.mk.JenkinsUtils() |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 88 | def item = jenkinsUtils.getJobByName(jobName) |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 89 | def parameters = [:] |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 90 | // def prop = item.getProperty(ParametersDefinitionProperty.class) |
| 91 | def prop = item.getProperty(ParametersDefinitionProperty) |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 92 | if (prop != null) { |
| 93 | for (param in prop.getParameterDefinitions()) { |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 94 | def defaultParam = param.getDefaultParameterValue() |
| 95 | def cls = defaultParam.getClass().getName() |
| 96 | def value = defaultParam.getValue() |
| 97 | def name = defaultParam.getName() |
| 98 | parameters[name] = [type: cls, use_variable: name, defaultValue: value] |
| 99 | } |
| 100 | } |
| 101 | return parameters |
| 102 | } |
| 103 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 104 | |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 105 | /** |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 106 | * Generate parameters for a Jenkins job using different sources |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 107 | * |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 108 | * @param job_parameters Map that declares which values from global_variables should be used, in the following format: |
| 109 | * {'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] | 110 | * or |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 111 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'get_variable_from_url': <a key from global_variables which contains URL with required content>}, ...} |
| 112 | * or |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 113 | * {'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] | 114 | * or |
| 115 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'get_variable_from_yaml': {'yaml_url': <URL with YAML content>, |
| 116 | * 'yaml_key': <a groovy-interpolating path to the key in the YAML, starting from dot '.'> } }, ...} |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 117 | * or |
| 118 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'use_variables_map': <a nested map of job_parameters>}, ...} |
| 119 | * , where job_parameters may contain a special 'type': '_defaultText' for a Yaml with some additional parameters for this map |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 120 | * @param global_variables Map that keeps the artifact URLs and used 'env' objects: |
| 121 | * {'PARAM1_NAME': <param1 value>, 'PARAM2_NAME': 'http://.../artifacts/param2_value', ...} |
| 122 | */ |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 123 | def generateParameters(job_parameters, global_variables) { |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 124 | def parameters = [] |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 125 | def common = new com.mirantis.mk.Common() |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 126 | def mcpcommon = new com.mirantis.mcp.Common() |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 127 | def http = new com.mirantis.mk.Http() |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 128 | def engine = new groovy.text.GStringTemplateEngine() |
| 129 | def template |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 130 | def yamls_from_urls = [:] |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 131 | def base = [:] |
| 132 | base["url"] = '' |
| 133 | def variable_content |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 134 | def env_variables = common.getEnvAsMap() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 135 | |
| 136 | // Collect required parameters from 'global_variables' or 'env' |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 137 | def _msg = '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 138 | for (param in job_parameters) { |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 139 | if (param.value.containsKey('use_variable')) { |
| 140 | if (!global_variables[param.value.use_variable]) { |
| 141 | global_variables[param.value.use_variable] = env[param.value.use_variable] ?: '' |
| 142 | } |
| 143 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: global_variables[param.value.use_variable]]) |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 144 | _msg += "\n${param.key}: <${param.value.type}> From:${param.value.use_variable}, Value:${global_variables[param.value.use_variable]}" |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 145 | } else if (param.value.containsKey('get_variable_from_url')) { |
| 146 | if (!global_variables[param.value.get_variable_from_url]) { |
| 147 | global_variables[param.value.get_variable_from_url] = env[param.value.get_variable_from_url] ?: '' |
| 148 | } |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 149 | if (global_variables[param.value.get_variable_from_url]) { |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 150 | variable_content = http.restGet(base, global_variables[param.value.get_variable_from_url]) |
| 151 | // http.restGet() attempts to read the response as a JSON, and may return an object instead of a string |
| 152 | variable_content = "${variable_content}".trim() |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 153 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: variable_content]) |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 154 | _msg += "\n${param.key}: <${param.value.type}> Content from url: ${variable_content}" |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 155 | } else { |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 156 | _msg += "\n${param.key} is empty, skipping get_variable_from_url" |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 157 | } |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 158 | } else if (param.value.containsKey('get_variable_from_yaml')) { |
| 159 | if (param.value.get_variable_from_yaml.containsKey('yaml_url') && param.value.get_variable_from_yaml.containsKey('yaml_key')) { |
| 160 | // 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] | 161 | def yaml_url_var = param.value.get_variable_from_yaml.yaml_url |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 162 | if (!global_variables[yaml_url_var]) { |
| 163 | global_variables[yaml_url_var] = env[yaml_url_var] ?: '' |
| 164 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 165 | def yaml_url = global_variables[yaml_url_var] // Real YAML URL |
| 166 | def yaml_key = param.value.get_variable_from_yaml.yaml_key |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 167 | // Key to get the data from YAML, to interpolate in the groovy, for example: |
| 168 | // <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] | 169 | if (yaml_url) { |
| 170 | if (!yamls_from_urls[yaml_url]) { |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 171 | _msg += "\nReading YAML from ${yaml_url} for ${param.key}" |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 172 | def yaml_content = http.restGet(base, yaml_url) |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 173 | yamls_from_urls[yaml_url] = readYaml text: yaml_content |
| 174 | } |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 175 | _msg += "\nGetting key ${yaml_key} from YAML ${yaml_url} for ${param.key}" |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 176 | def template_variables = [ |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 177 | 'yaml_data': yamls_from_urls[yaml_url], |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 178 | ] |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 179 | def request = "\${yaml_data${yaml_key}}" |
Dennis Dmitriev | 5e07671 | 2022-02-08 15:05:21 +0200 | [diff] [blame] | 180 | def result |
Dennis Dmitriev | 450cf73 | 2021-11-11 14:59:17 +0200 | [diff] [blame] | 181 | // Catch errors related to wrong key or index in the list or map objects |
| 182 | // For wrong key in map or wrong index in list, groovy returns <null> object, |
| 183 | // but it can be catched only after the string interpolation <template.toString()>, |
| 184 | // so we should catch the string 'null' instead of object <null>. |
| 185 | try { |
| 186 | template = engine.createTemplate(request).make(template_variables) |
Dennis Dmitriev | 5e07671 | 2022-02-08 15:05:21 +0200 | [diff] [blame] | 187 | result = template.toString() |
Dennis Dmitriev | 450cf73 | 2021-11-11 14:59:17 +0200 | [diff] [blame] | 188 | if (result == 'null') { |
| 189 | error "No such key or index, got 'null'" |
| 190 | } |
| 191 | } catch (e) { |
| 192 | error("Failed to get the key ${yaml_key} from YAML ${yaml_url}: " + e.toString()) |
| 193 | } |
| 194 | |
| 195 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: result]) |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 196 | _msg += "\n${param.key}: <${param.value.type}>\n${result}" |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 197 | } else { |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 198 | 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] | 199 | } |
| 200 | } else { |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 201 | 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] | 202 | } |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 203 | } else if (param.value.containsKey('use_template')) { |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 204 | template = engine.createTemplate(param.value.use_template.toString()).make(env_variables + global_variables) |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 205 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: template.toString()]) |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 206 | _msg += "\n${param.key}: <${param.value.type}>\n${template.toString()}" |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 207 | } else if (param.value.containsKey('use_variables_map')) { |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 208 | // Generate multistring YAML with key/value pairs (like job_parameters) from a nested parameters map |
| 209 | def nested_parameters = generateParameters(param.value.use_variables_map, global_variables) |
| 210 | def nested_values = [:] |
| 211 | for (_parameter in nested_parameters) { |
| 212 | if (_parameter.$class == '_defaultText') { |
| 213 | // This is a special type for multiline with default values |
| 214 | def _values = readYaml(text: _parameter.value ?: '---') ?: [:] |
| 215 | _values << nested_values |
| 216 | nested_values = _values |
| 217 | } else { |
| 218 | nested_values[_parameter.name] = _parameter.value |
| 219 | } |
| 220 | } |
| 221 | def multistring_value = mcpcommon.dumpYAML(nested_values) |
| 222 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: multistring_value]) |
| 223 | _msg += "\n${param.key}: <${param.value.type}>\n${multistring_value}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 224 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 225 | } |
azvyagintsev | e2e26bb | 2024-09-24 18:03:59 +0300 | [diff] [blame] | 226 | // Inject hidden random parameter (is not showed in jjb) to be sure we are triggering unique downstream job. |
| 227 | // Most actual case - parallel run for same jobs( but with different params) |
| 228 | parameters.addAll([string(name: 'RANDOM_SEED_STRING', value: "${env.JOB_NAME.toLowerCase()}-${env.BUILD_NUMBER}-${UUID.randomUUID().toString().split('-')[0]}")]) |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 229 | common.infoMsg(_msg) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 230 | return parameters |
| 231 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 232 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 233 | |
| 234 | /** |
| 235 | * Run a Jenkins job using the collected parameters |
| 236 | * |
| 237 | * @param job_name Name of the running job |
| 238 | * @param job_parameters Map that declares which values from global_variables should be used |
| 239 | * @param global_variables Map that keeps the artifact URLs and used 'env' objects |
| 240 | * @param propagate Boolean. If false: allows to collect artifacts after job is finished, even with FAILURE status |
| 241 | * If true: immediatelly fails the pipeline. DO NOT USE 'true' if you want to collect artifacts |
| 242 | * for 'finally' steps |
| 243 | */ |
| 244 | def runJob(job_name, job_parameters, global_variables, Boolean propagate = false) { |
| 245 | |
| 246 | def parameters = generateParameters(job_parameters, global_variables) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 247 | // Build the job |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 248 | def job_info = build job: "${job_name}", parameters: parameters, propagate: propagate |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 249 | return job_info |
| 250 | } |
| 251 | |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 252 | def runOrGetJob(job_name, job_parameters, global_variables, propagate, String fullTaskName = '') { |
| 253 | /** |
| 254 | * Run job directly or try to find already executed build |
| 255 | * Flow, in case CI_JOBS_OVERRIDES passed: |
| 256 | * |
| 257 | * |
| 258 | * CI_JOBS_OVERRIDES = text in yaml|json format |
| 259 | * CI_JOBS_OVERRIDES = 'kaas-testing-core-release-artifact' : 3505 |
| 260 | * 'reindex-testing-core-release-index-with-rc' : 2822 |
| 261 | * 'si-test-release-sanity-check-prepare-configuration': 1877 |
| 262 | */ |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 263 | def common = new com.mirantis.mk.Common() |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 264 | def jobsOverrides = readYaml(text: env.CI_JOBS_OVERRIDES ?: '---') ?: [:] |
| 265 | // get id of overriding job |
| 266 | def jobOverrideID = jobsOverrides.getOrDefault(fullTaskName, '') |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 267 | if (fullTaskName in jobsOverrides.keySet()) { |
| 268 | common.warningMsg("Overriding: ${fullTaskName}/${job_name} <<< ${jobOverrideID}") |
| 269 | common.infoMsg("For debug pin use:\n'${fullTaskName}' : ${jobOverrideID}") |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 270 | return Jenkins.instance.getItemByFullName(job_name, hudson.model.Job).getBuildByNumber(jobOverrideID.toInteger()) |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 271 | } else { |
| 272 | return runJob(job_name, job_parameters, global_variables, propagate) |
| 273 | } |
| 274 | } |
| 275 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 276 | /** |
| 277 | * Store URLs of the specified artifacts to the global_variables |
| 278 | * |
Andrii Baraniuk | cf4c2fa | 2023-04-18 13:53:32 +0300 | [diff] [blame] | 279 | * @param build_url URL of the completed job |
| 280 | * @param step_artifacts Map that contains artifact names in the job, and variable names |
| 281 | * where the URLs to that atrifacts should be stored, for example: |
| 282 | * {'ARTIFACT1': 'logs.tar.gz', 'ARTIFACT2': 'test_report.xml', ...} |
| 283 | * @param global_variables Map that will keep the artifact URLs. Variable 'ARTIFACT1', for example, |
| 284 | * be used in next job parameters: {'ARTIFACT1_URL':{ 'use_variable': 'ARTIFACT1', ...}} |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 285 | * |
Andrii Baraniuk | cf4c2fa | 2023-04-18 13:53:32 +0300 | [diff] [blame] | 286 | * If the artifact with the specified name not found, the parameter ARTIFACT1_URL |
| 287 | * will be empty. |
| 288 | * @param artifactory_server Artifactory server ID defined in Jenkins config |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 289 | * |
| 290 | */ |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 291 | def storeArtifacts(build_url, step_artifacts, global_variables, job_name, build_num, artifactory_url = '', artifactory_server = '', artifacts_msg='local artifacts') { |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 292 | def common = new com.mirantis.mk.Common() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 293 | def http = new com.mirantis.mk.Http() |
Andrii Baraniuk | cf4c2fa | 2023-04-18 13:53:32 +0300 | [diff] [blame] | 294 | def artifactory = new com.mirantis.mcp.MCPArtifactory() |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 295 | if (!artifactory_url && !artifactory_server) { |
Andrii Baraniuk | cf4c2fa | 2023-04-18 13:53:32 +0300 | [diff] [blame] | 296 | artifactory_url = 'https://artifactory.mcp.mirantis.net/artifactory/api/storage/si-local/jenkins-job-artifacts' |
| 297 | } else if (!artifactory_url && artifactory_server) { |
| 298 | artifactory_url = artifactory.getArtifactoryServer(artifactory_server).getUrl() + '/artifactory/api/storage/si-local/jenkins-job-artifacts' |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 299 | } |
Andrii Baraniuk | cf4c2fa | 2023-04-18 13:53:32 +0300 | [diff] [blame] | 300 | |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 301 | def baseJenkins = [:] |
| 302 | def baseArtifactory = [:] |
| 303 | build_url = build_url.replaceAll(~/\/+$/, "") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 304 | baseArtifactory["url"] = artifactory_url + "/${job_name}/${build_num}" |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 305 | baseJenkins["url"] = build_url |
| 306 | def job_config = http.restGet(baseJenkins, "/api/json/") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 307 | def job_artifacts = job_config['artifacts'] |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 308 | common.infoMsg("Attempt to store ${artifacts_msg} for: ${job_name}/${build_num}") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 309 | for (artifact in step_artifacts) { |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 310 | try { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 311 | def artifactoryResp = http.restGet(baseArtifactory, "/${artifact.value}") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 312 | global_variables[artifact.key] = artifactoryResp.downloadUri |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 313 | common.infoMsg("Artifact URL ${artifactoryResp.downloadUri} stored to ${artifact.key}") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 314 | continue |
| 315 | } catch (Exception e) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 316 | common.warningMsg("Can't find an artifact in ${artifactory_url}/${job_name}/${build_num}/${artifact.value} to store in ${artifact.key}\n" + |
| 317 | "error code ${e.message}") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 318 | } |
| 319 | |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 320 | 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] | 321 | if (job_artifact.size() == 1) { |
| 322 | // Store artifact URL |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 323 | def artifact_url = "${build_url}/artifact/${job_artifact[0]['relativePath']}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 324 | global_variables[artifact.key] = artifact_url |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 325 | common.infoMsg("Artifact URL ${artifact_url} stored to ${artifact.key}") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 326 | } else if (job_artifact.size() > 1) { |
| 327 | // Error: too many artifacts with the same name, fail the job |
| 328 | error "Multiple artifacts ${artifact.value} for ${artifact.key} found in the build results ${build_url}, expected one:\n${job_artifact}" |
| 329 | } else { |
| 330 | // Warning: no artifact with expected name |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 331 | 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] | 332 | global_variables[artifact.key] = '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 337 | |
| 338 | def getStatusStyle(status) { |
| 339 | // Styling the status of job result |
| 340 | def status_style = '' |
| 341 | switch (status) { |
| 342 | case "SUCCESS": |
| 343 | status_style = "<td style='color: green;'><img src='/images/16x16/blue.png' alt='SUCCESS'>" |
| 344 | break |
| 345 | case "UNSTABLE": |
| 346 | status_style = "<td style='color: #FF5733;'><img src='/images/16x16/yellow.png' alt='UNSTABLE'>" |
| 347 | break |
| 348 | case "ABORTED": |
| 349 | status_style = "<td style='color: red;'><img src='/images/16x16/aborted.png' alt='ABORTED'>" |
| 350 | break |
| 351 | case "NOT_BUILT": |
| 352 | status_style = "<td style='color: red;'><img src='/images/16x16/aborted.png' alt='NOT_BUILT'>" |
| 353 | break |
| 354 | case "FAILURE": |
| 355 | status_style = "<td style='color: red;'><img src='/images/16x16/red.png' alt='FAILURE'>" |
| 356 | break |
| 357 | default: |
| 358 | status_style = "<td>-" |
| 359 | } |
| 360 | return status_style |
| 361 | } |
| 362 | |
| 363 | |
| 364 | def getTrStyle(jobdata) { |
| 365 | def trstyle = "<tr>" |
| 366 | // Grey background for 'finally' jobs in list |
| 367 | if (jobdata.getOrDefault('type', '') == 'finally') { |
| 368 | trstyle = "<tr style='background: #DDDDDD;'>" |
| 369 | } |
| 370 | return trstyle |
| 371 | } |
| 372 | |
| 373 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 374 | /** |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 375 | * Update a 'job' step description |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 376 | * |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 377 | * @param jobsdata Map with a 'job' step details and status |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 378 | */ |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 379 | def getJobDescription(jobdata) { |
| 380 | def trstyle = getTrStyle(jobdata) |
| 381 | def display_name = jobdata['desc'] ? "${jobdata['desc']}: ${jobdata['build_id']}" : "${jobdata['name']}: ${jobdata['build_id']}" |
| 382 | if ((env.WF_SHOW_FULL_WORKFLOW_DESCRIPTION ?: false).toBoolean()) { |
| 383 | display_name = "[${jobdata['name']}/${jobdata['build_id']}]: ${jobdata['desc']}" |
| 384 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 385 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 386 | // Attach url for already built jobs |
| 387 | def build_url = display_name |
| 388 | if (jobdata['build_url'] != "0") { |
| 389 | build_url = "<a href=${jobdata['build_url']}>$display_name</a>" |
| 390 | } |
| 391 | |
| 392 | def status_style = getStatusStyle(jobdata['status'].toString()) |
| 393 | |
| 394 | return [[trstyle, build_url, jobdata['duration'], status_style,],] |
| 395 | } |
| 396 | |
| 397 | |
| 398 | /** |
| 399 | * Update a 'script' step description |
| 400 | * |
| 401 | * @param jobsdata Map with a 'script' step details and status |
| 402 | */ |
| 403 | def getScriptDescription(jobdata) { |
| 404 | def trstyle = getTrStyle(jobdata) |
| 405 | |
| 406 | def display_name = "${jobdata['desc']}" ?: "${jobdata['name']}" |
| 407 | if ((env.WF_SHOW_FULL_WORKFLOW_DESCRIPTION ?: false).toBoolean()) { |
| 408 | display_name = "[${jobdata['name']}]: ${jobdata['desc']}" |
| 409 | } |
| 410 | |
| 411 | // Attach url for already built jobs |
| 412 | def build_url = display_name |
| 413 | if (jobdata['build_url'] != "0") { |
| 414 | build_url = "<a href=${jobdata['build_url']}>$display_name</a>" |
| 415 | } |
| 416 | |
| 417 | def status_style = getStatusStyle(jobdata['status'].toString()) |
| 418 | |
| 419 | return [[trstyle, build_url, jobdata['duration'], status_style,],] |
| 420 | } |
| 421 | |
| 422 | |
| 423 | /** |
| 424 | * Update a 'parallel' or a 'sequence' step description |
| 425 | * |
| 426 | * @param jobsdata Map with a 'together' step details and statuses |
| 427 | */ |
| 428 | def getNestedDescription(jobdata) { |
| 429 | def tableEntries = [] |
| 430 | def trstyle = getTrStyle(jobdata) |
| 431 | |
| 432 | def display_name = "${jobdata['desc']}" ?: "${jobdata['name']}" |
| 433 | if ((env.WF_SHOW_FULL_WORKFLOW_DESCRIPTION ?: false).toBoolean()) { |
| 434 | display_name = "[${jobdata['name']}]: ${jobdata['desc']}" |
| 435 | } |
| 436 | |
| 437 | // Attach url for already built jobs |
| 438 | def build_url = display_name |
| 439 | if (jobdata['build_url'] != "0") { |
| 440 | build_url = "<a href=${jobdata['build_url']}>$display_name</a>" |
| 441 | } |
| 442 | |
| 443 | def status_style = getStatusStyle(jobdata['status'].toString()) |
| 444 | |
| 445 | tableEntries += [[trstyle, build_url, jobdata['duration'], status_style,],] |
| 446 | |
| 447 | // Collect nested job descriptions |
| 448 | for (nested_jobdata in jobdata['nested_steps_data']) { |
| 449 | (nestedTableEntries, _) = getStepDescription(nested_jobdata.value) |
| 450 | for (nestedTableEntry in nestedTableEntries) { |
| 451 | (nested_trstyle, nested_display_name, nested_duration, nested_status_style) = nestedTableEntry |
| 452 | tableEntries += [[nested_trstyle, " | ${nested_jobdata.key}: ${nested_display_name}", nested_duration, nested_status_style,],] |
| 453 | } |
| 454 | } |
| 455 | return tableEntries |
| 456 | } |
| 457 | |
| 458 | |
| 459 | def getStepDescription(jobs_data) { |
| 460 | def tableEntries = [] |
| 461 | def child_jobs_description = '' |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 462 | for (jobdata in jobs_data) { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 463 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 464 | if (jobdata['step_key'] == 'job') { |
| 465 | tableEntries += getJobDescription(jobdata) |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 466 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 467 | else if (jobdata['step_key'] == 'script') { |
| 468 | tableEntries += getScriptDescription(jobdata) |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 469 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 470 | else if (jobdata['step_key'] == 'parallel' || jobdata['step_key'] == 'sequence') { |
| 471 | tableEntries += getNestedDescription(jobdata) |
| 472 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 473 | |
| 474 | // Collecting descriptions of builded child jobs |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 475 | if (jobdata['child_desc'] != '') { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 476 | 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] | 477 | // remove "null" message-result from description, but leave XXX:JOBRESULT in description |
azvyagintsev | 8b8224d | 2023-10-06 20:52:26 +0300 | [diff] [blame] | 478 | if (jobdata['child_desc'] != 'null') { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 479 | child_jobs_description += "<small>${jobdata['child_desc']}</small><br>" |
| 480 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 481 | } |
| 482 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 483 | return [tableEntries, child_jobs_description] |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Update description for workflow steps |
| 488 | * |
| 489 | * @param jobs_data Map with all step names and result statuses, to showing it in description |
| 490 | */ |
| 491 | def updateDescription(jobs_data) { |
| 492 | def child_jobs_description = '<strong>Descriptions from jobs:</strong><br>' |
| 493 | def table_template_start = "<div><table style='border: solid 1px;'><tr><th>Job:</th><th>Duration:</th><th>Status:</th></tr>" |
| 494 | def table_template_end = "</table></div>" |
| 495 | |
| 496 | (tableEntries, _child_jobs_description) = getStepDescription(jobs_data) |
| 497 | |
| 498 | def table = '' |
| 499 | for (tableEntry in tableEntries) { |
| 500 | // Collect table |
| 501 | (trstyle, display_name, duration, status_style) = tableEntry |
| 502 | table += "${trstyle}<td>${display_name}</td><td>${duration}</td>${status_style}</td></tr>" |
| 503 | } |
| 504 | |
| 505 | child_jobs_description += _child_jobs_description |
| 506 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 507 | currentBuild.description = table_template_start + table + table_template_end + child_jobs_description |
| 508 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 509 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 510 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 511 | def runStep(global_variables, step, Boolean propagate = false, artifactoryBaseUrl = '', artifactoryServer = '', parent_global_variables=null) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 512 | return { |
| 513 | def common = new com.mirantis.mk.Common() |
| 514 | def engine = new groovy.text.GStringTemplateEngine() |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 515 | def env_variables = common.getEnvAsMap() |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 516 | |
| 517 | String jobDescription = step['description'] ?: '' |
| 518 | def jobName = step['job'] |
| 519 | def jobParameters = [:] |
| 520 | def stepParameters = step['parameters'] ?: [:] |
| 521 | if (step['inherit_parent_params'] ?: false) { |
| 522 | // add parameters from the current job for the child job |
| 523 | jobParameters << getJobDefaultParameters(env.JOB_NAME) |
| 524 | } |
| 525 | // add parameters from the workflow for the child job |
| 526 | jobParameters << stepParameters |
| 527 | def wfPauseStepBeforeRun = (step['wf_pause_step_before_run'] ?: false).toBoolean() |
| 528 | def wfPauseStepTimeout = (step['wf_pause_step_timeout'] ?: 10).toInteger() |
| 529 | def wfPauseStepSlackReportChannel = step['wf_pause_step_slack_report_channel'] ?: '' |
| 530 | |
| 531 | if (wfPauseStepBeforeRun) { |
| 532 | // Try-catch construction will allow to continue Steps, if timeout reached |
| 533 | try { |
| 534 | if (wfPauseStepSlackReportChannel) { |
| 535 | def slack = new com.mirantis.mcp.SlackNotification() |
azvyagintsev | da22aa8 | 2022-06-10 15:46:55 +0300 | [diff] [blame] | 536 | wfPauseStepSlackReportChannel.split(',').each { |
| 537 | slack.jobResultNotification('wf_pause_step_before_run', |
| 538 | it.toString(), |
| 539 | env.JOB_NAME, null, |
| 540 | env.BUILD_URL, 'slack_webhook_url') |
| 541 | } |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 542 | } |
| 543 | timeout(time: wfPauseStepTimeout, unit: 'MINUTES') { |
| 544 | input("Workflow pause requested before run: ${jobName}/${jobDescription}\n" + |
| 545 | "Timeout set to ${wfPauseStepTimeout}.\n" + |
| 546 | "Do you want to proceed workflow?") |
| 547 | } |
| 548 | } catch (err) { // timeout reached or input false |
Sergey Lalov | a89e16a | 2024-10-17 20:26:44 +0400 | [diff] [blame] | 549 | def cause = err.getCauses().get(0) |
| 550 | if (cause instanceof org.jenkinsci.plugins.workflow.steps.TimeoutStepExecution.ExceededTimeout) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 551 | common.infoMsg("Timeout finished, continue..") |
Sergey Lalov | a89e16a | 2024-10-17 20:26:44 +0400 | [diff] [blame] | 552 | } else { |
| 553 | def user = causes[0].getUser() |
| 554 | error("Aborted after workflow pause by: [${user}]") |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | } |
| 558 | common.infoMsg("Attempt to run: ${jobName}/${jobDescription}") |
| 559 | // Collect job parameters and run the job |
| 560 | // WARN(alexz): desc must not contain invalid chars for yaml |
| 561 | def jobResult = runOrGetJob(jobName, jobParameters, |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 562 | global_variables, propagate, jobDescription) |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 563 | def buildDuration = jobResult.durationString ?: '-' |
| 564 | if (buildDuration.toString() == null) { |
| 565 | buildDuration = '-' |
| 566 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 567 | def desc = engine.createTemplate(jobDescription.toString()).make(env_variables + global_variables) |
| 568 | if ((desc.toString() == '') || (desc.toString() == 'null')) { |
| 569 | desc = '' |
| 570 | } |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 571 | def jobSummary = [ |
| 572 | job_result : jobResult.getResult().toString(), |
| 573 | build_url : jobResult.getAbsoluteUrl().toString(), |
| 574 | build_id : jobResult.getId().toString(), |
| 575 | buildDuration : buildDuration, |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 576 | desc : desc, |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 577 | ] |
| 578 | def _buildDescription = jobResult.getDescription().toString() |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 579 | if (_buildDescription) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 580 | jobSummary['build_description'] = _buildDescription |
| 581 | } |
| 582 | // Store links to the resulting artifacts into 'global_variables' |
| 583 | storeArtifacts(jobSummary['build_url'], step['artifacts'], |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 584 | global_variables, jobName, jobSummary['build_id'], artifactoryBaseUrl, artifactoryServer, artifacts_msg='artifacts to local variables') |
| 585 | // Store links to the resulting 'global_artifacts' into 'global_variables' |
| 586 | storeArtifacts(jobSummary['build_url'], step['global_artifacts'], |
| 587 | global_variables, jobName, jobSummary['build_id'], artifactoryBaseUrl, artifactoryServer, artifacts_msg='global_artifacts to local variables') |
| 588 | // Store links to the resulting 'global_artifacts' into 'parent_global_variables' |
| 589 | storeArtifacts(jobSummary['build_url'], step['global_artifacts'], |
| 590 | parent_global_variables, jobName, jobSummary['build_id'], artifactoryBaseUrl, artifactoryServer, artifacts_msg='global_artifacts to global_variables') |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 591 | return jobSummary |
| 592 | } |
| 593 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 594 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 595 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 596 | def runScript(global_variables, step, artifactoryBaseUrl = '', artifactoryServer = '', scriptsLibrary = null, parent_global_variables=null) { |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 597 | def common = new com.mirantis.mk.Common() |
| 598 | def env_variables = common.getEnvAsMap() |
| 599 | |
| 600 | if (!scriptsLibrary) { |
| 601 | error "'scriptsLibrary' argument is not provided to load a script object '${step['script']}' from that library" |
| 602 | } |
| 603 | // Evaluate the object from it's name, for example: scriptsLibrary.com.mirantis.si.runtime_steps.ParallelMkeMoskUpgradeSequences |
| 604 | def scriptObj = scriptsLibrary |
| 605 | for (sObj in step['script'].split("\\.")) { |
| 606 | scriptObj = scriptObj."$sObj" |
| 607 | } |
| 608 | |
| 609 | def script = scriptObj.new() |
| 610 | |
| 611 | def scriptSummary = [ |
| 612 | job_result : '', |
| 613 | desc : step['description'] ?: '', |
| 614 | ] |
| 615 | |
| 616 | // prepare 'script_env' from merged 'env' and script step parameters |
| 617 | def script_env = env_variables.clone() |
| 618 | def stepParameters = step['parameters'] ?: [:] |
| 619 | def script_parameters = generateParameters(stepParameters, global_variables) |
| 620 | println "${script_parameters}" |
| 621 | for (script_parameter in script_parameters) { |
| 622 | common.infoMsg("Updating script env['${script_parameter.name}'] with value: ${script_parameter.value}") |
| 623 | script_env[script_parameter.name] = script_parameter.value |
| 624 | } |
| 625 | |
| 626 | try { |
| 627 | script.main(this, script_env) |
| 628 | scriptSummary['script_result'] = 'SUCCESS' |
| 629 | } catch (InterruptedException e) { |
| 630 | scriptSummary['script_result'] = 'ABORTED' |
| 631 | printStackTrace(e) |
| 632 | } catch (e) { |
| 633 | scriptSummary['script_result'] = 'FAILURE' |
| 634 | printStackTrace(e) |
| 635 | } |
| 636 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 637 | // Store links to the resulting 'artifacts' into 'global_variables' |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 638 | storeArtifacts(env.BUILD_URL, step['artifacts'], |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 639 | global_variables, env.JOB_NAME, env.BUILD_NUMBER, artifactoryBaseUrl, artifactoryServer, artifacts_msg='artifacts to local variables') |
| 640 | // Store links to the resulting 'global_artifacts' into 'global_variables' |
| 641 | storeArtifacts(env.BUILD_URL, step['global_artifacts'], |
| 642 | global_variables, env.JOB_NAME, env.BUILD_NUMBER, artifactoryBaseUrl, artifactoryServer, artifacts_msg='global_artifacts to local variables') |
| 643 | // Store links to the resulting 'global_artifacts' into 'parent_global_variables' |
| 644 | storeArtifacts(env.BUILD_URL, step['global_artifacts'], |
| 645 | parent_global_variables, env.JOB_NAME, env.BUILD_NUMBER, artifactoryBaseUrl, artifactoryServer, artifacts_msg='global_artifacts to global_variables') |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 646 | |
| 647 | return scriptSummary |
| 648 | } |
| 649 | |
| 650 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 651 | def runParallel(global_variables, step, failed_jobs, global_jobs_data, nested_steps_data, artifactoryBaseUrl = '', artifactoryServer = '', scriptsLibrary = null, prefixMsg = '', parent_global_variables=null) { |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 652 | // Run the specified steps in parallel |
| 653 | // Repeat the steps for each parameters set from 'repeat_with_parameters_from_yaml' |
| 654 | // If 'repeat_with_parameters_from_yaml' is not provided, then 'parallel' step will perform just one iteration for a default "- _FOO: _BAR" parameter |
| 655 | // If 'repeat_with_parameters_from_yaml' is present, but the specified artifact contains empty list '[]', then 'parallel' step will be skipped |
| 656 | // Example: |
| 657 | // - parallel: |
| 658 | // - job: |
| 659 | // - job: |
| 660 | // - sequence: |
| 661 | // repeat_with_parameters_from_yaml: |
| 662 | // type: TextParameterValue |
| 663 | // get_variable_from_url: SI_PARALLEL_PARAMETERS |
| 664 | // max_concurrent: 2 # how many parallel jobs shold be run at the same time |
| 665 | // max_concurrent_interval: 300 # how many seconds should be passed between checking for an available concurrency |
| 666 | // check_failed_concurrent: false # stop waiting for available concurrent executors if count of failed jobs >= max_concurrent, |
| 667 | // # which means that all available shared resources are occupied by the failed jobs |
| 668 | def common = new com.mirantis.mk.Common() |
| 669 | |
| 670 | def sourceText = "" |
| 671 | def defaultSourceText = "- _FOO: _BAR" |
| 672 | if (step['repeat_with_parameters_from_yaml']) { |
| 673 | def sourceParameter = ["repeat_with_parameters_from_yaml": step['repeat_with_parameters_from_yaml']] |
| 674 | for (parameter in generateParameters(sourceParameter, global_variables)) { |
| 675 | if (parameter.name == "repeat_with_parameters_from_yaml") { |
| 676 | sourceText = parameter.value |
| 677 | common.infoMsg("'repeat_with_parameters_from_yaml' is defined, using it as a yaml text:\n${sourceText}") |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | if (!sourceText) { |
| 682 | sourceText = defaultSourceText |
| 683 | common.warningMsg("'repeat_with_parameters_from_yaml' is not defined. To get one iteration, use default single entry:\n${sourceText}") |
| 684 | } |
| 685 | def iterateParametersList = readYaml text: sourceText |
| 686 | if (!(iterateParametersList instanceof List)) { |
| 687 | // Stop the pipeline if there is wrong parameters data type, to not generate parallel jobs for wrong data |
| 688 | error "Expected a List in 'repeat_with_parameters_from_yaml' for 'parallel' step, but got:\n${sourceText}" |
| 689 | } |
| 690 | |
| 691 | // Limit the maximum steps in parallel at the same time |
| 692 | def max_concurrent = (step['max_concurrent'] ?: 100).toInteger() |
| 693 | // Sleep for the specified amount of time until a free thread will be available |
| 694 | def max_concurrent_interval = (step['max_concurrent_interval'] ?: 600).toInteger() |
| 695 | // Check that failed jobs is not >= free executors. if 'true', then don't wait for free executors, fail the parallel step |
| 696 | def check_failed_concurrent = (step['check_failed_concurrent'] ?: false).toBoolean() |
| 697 | |
| 698 | def jobs = [:] |
| 699 | def nested_step_id = 0 |
| 700 | def free_concurrent = max_concurrent |
| 701 | def failed_concurrent = [] |
| 702 | |
| 703 | common.printMsg("${prefixMsg} Running parallel steps with the following parameters:\n${iterateParametersList}", "purple") |
| 704 | |
| 705 | for (parameters in iterateParametersList) { |
| 706 | for (parallel_step in step['parallel']) { |
| 707 | def step_name = "parallel#${nested_step_id}" |
| 708 | def nested_step = parallel_step |
| 709 | def nested_step_name = step_name |
| 710 | def nested_prefix_name = "${prefixMsg}${nested_step_name} | " |
| 711 | |
| 712 | nested_steps_data[step_name] = [] |
| 713 | prepareJobsData([nested_step,], 'parallel', nested_steps_data[step_name]) |
| 714 | |
| 715 | //Copy global variables and merge "parameters" dict into it for the current particular step |
| 716 | def nested_global_variables = global_variables.clone() |
| 717 | nested_global_variables << parameters |
| 718 | |
| 719 | jobs[step_name] = { |
| 720 | // initialRecurrencePeriod in milliseconds |
| 721 | waitUntil(initialRecurrencePeriod: 1500, quiet: true) { |
| 722 | if (check_failed_concurrent) { |
| 723 | if (failed_concurrent.size() >= max_concurrent){ |
| 724 | common.errorMsg("Failed jobs count is equal max_concurrent value ${max_concurrent}. Will not continue because resources are consumed") |
| 725 | error("max_concurrent == failed_concurrent") |
| 726 | } |
| 727 | } |
| 728 | if (free_concurrent > 0) { |
| 729 | free_concurrent-- |
| 730 | true |
| 731 | } else { |
| 732 | sleep(max_concurrent_interval) |
| 733 | false |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | try { |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 738 | runWorkflowStep(nested_global_variables, nested_step, 0, nested_steps_data[nested_step_name], global_jobs_data, failed_jobs, false, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, nested_prefix_name, parent_global_variables) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 739 | } |
| 740 | catch (e) { |
| 741 | failed_concurrent.add(step_name) |
| 742 | throw(e) |
| 743 | } |
| 744 | |
| 745 | free_concurrent++ |
| 746 | } // 'jobs' closure |
| 747 | |
| 748 | nested_step_id++ |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | def parallelSummary = [ |
| 753 | nested_result : '', |
| 754 | desc : step['description'] ?: '', |
| 755 | nested_steps_data : [:], |
| 756 | ] |
| 757 | |
| 758 | if (iterateParametersList) { |
| 759 | // Run parallel iterations |
| 760 | try { |
| 761 | common.infoMsg("${prefixMsg} Run steps in parallel") |
| 762 | |
| 763 | parallel jobs |
| 764 | |
| 765 | parallelSummary['nested_result'] = 'SUCCESS' |
| 766 | } catch (InterruptedException e) { |
| 767 | parallelSummary['nested_result'] = 'ABORTED' |
| 768 | printStackTrace(e) |
| 769 | } catch (e) { |
| 770 | parallelSummary['nested_result'] = 'FAILURE' |
| 771 | printStackTrace(e) |
| 772 | } |
| 773 | parallelSummary['nested_steps_data'] = nested_steps_data |
| 774 | } |
| 775 | else |
| 776 | { |
| 777 | // No parameters were provided to iterate |
| 778 | common.errorMsg("${prefixMsg} No parameters were provided to iterate, skipping 'parallel' step") |
| 779 | parallelSummary['nested_result'] = 'SUCCESS' |
| 780 | } |
| 781 | return parallelSummary |
| 782 | } |
| 783 | |
| 784 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 785 | def runSequence(global_variables, step, failed_jobs, global_jobs_data, nested_steps_data, artifactoryBaseUrl = '', artifactoryServer = '', scriptsLibrary = null, prefixMsg = '', parent_global_variables=null) { |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 786 | // Run the steps in the specified order, like in main workflow, but repeat the sequence for each parameters set from 'repeat_with_parameters_from_yaml' |
| 787 | // If 'repeat_with_parameters_from_yaml' is not provided, then 'sequence' step will perform just one iteration for a default "- _FOO: _BAR" parameter |
| 788 | // If 'repeat_with_parameters_from_yaml' is present, but the specified artifact contains empty list '[]', then 'sequence' step will be skipped |
| 789 | // - sequence: |
| 790 | // - job: |
| 791 | // - job: |
| 792 | // - script: |
| 793 | // repeat_with_parameters_from_yaml: |
| 794 | // type: TextParameterValue |
| 795 | // get_variable_from_url: SI_PARALLEL_PARAMETERS |
| 796 | def common = new com.mirantis.mk.Common() |
| 797 | |
| 798 | def sourceText = "" |
| 799 | def defaultSourceText = "- _FOO: _BAR" |
| 800 | if (step['repeat_with_parameters_from_yaml']) { |
| 801 | def sourceParameter = ["repeat_with_parameters_from_yaml": step['repeat_with_parameters_from_yaml']] |
| 802 | for (parameter in generateParameters(sourceParameter, global_variables)) { |
| 803 | if (parameter.name == "repeat_with_parameters_from_yaml") { |
| 804 | sourceText = parameter.value |
| 805 | common.infoMsg("'repeat_with_parameters_from_yaml' is defined, using it as a yaml text:\n${sourceText}") |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | if (!sourceText) { |
| 810 | sourceText = defaultSourceText |
| 811 | common.warningMsg("'repeat_with_parameters_from_yaml' is not defined. To get one iteration, use default single entry:\n${sourceText}") |
| 812 | } |
| 813 | def iterateParametersList = readYaml text: sourceText |
| 814 | if (!(iterateParametersList instanceof List)) { |
| 815 | // Stop the pipeline if there is wrong parameters data type, to not generate parallel jobs for wrong data |
| 816 | error "Expected a List in 'repeat_with_parameters_from_yaml' for 'sequence' step, but got:\n${sourceText}" |
| 817 | } |
| 818 | |
| 819 | def jobs = [:] |
| 820 | def nested_step_id = 0 |
| 821 | |
| 822 | common.printMsg("${prefixMsg} Running parallel steps with the following parameters:\n${iterateParametersList}", "purple") |
| 823 | |
| 824 | for (parameters in iterateParametersList) { |
| 825 | def step_name = "sequence#${nested_step_id}" |
| 826 | def nested_steps = step['sequence'] |
| 827 | def nested_step_name = step_name |
| 828 | def nested_prefix_name = "${prefixMsg}${nested_step_name} | " |
| 829 | |
| 830 | nested_steps_data[step_name] = [] |
| 831 | prepareJobsData(nested_steps, 'sequence', nested_steps_data[step_name]) |
| 832 | |
| 833 | //Copy global variables and merge "parameters" dict into it for the current particular step |
| 834 | def nested_global_variables = global_variables.clone() |
| 835 | nested_global_variables << parameters |
| 836 | |
| 837 | jobs[step_name] = { |
| 838 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 839 | runSteps(nested_steps, nested_global_variables, failed_jobs, nested_steps_data[nested_step_name], global_jobs_data, 0, false, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, nested_prefix_name, parent_global_variables) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 840 | |
| 841 | } // 'jobs' closure |
| 842 | |
| 843 | nested_step_id++ |
| 844 | } |
| 845 | |
| 846 | def sequenceSummary = [ |
| 847 | nested_result : '', |
| 848 | desc : step['description'] ?: '', |
| 849 | nested_steps_data : [:], |
| 850 | ] |
| 851 | |
| 852 | if (iterateParametersList) { |
| 853 | // Run sequence iterations |
| 854 | try { |
| 855 | jobs.each { stepName, job -> |
| 856 | common.infoMsg("${prefixMsg} Running sequence ${stepName}") |
| 857 | job() |
azvyagintsev | d5f0512 | 2024-09-21 13:00:16 +0300 | [diff] [blame] | 858 | // just in case sleep. |
| 859 | sleep(5) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 860 | } |
| 861 | sequenceSummary['nested_result'] = 'SUCCESS' |
| 862 | } catch (InterruptedException e) { |
| 863 | sequenceSummary['nested_result'] = 'ABORTED' |
| 864 | printStackTrace(e) |
| 865 | } catch (e) { |
| 866 | sequenceSummary['nested_result'] = 'FAILURE' |
| 867 | printStackTrace(e) |
| 868 | } |
| 869 | sequenceSummary['nested_steps_data'] = nested_steps_data |
| 870 | } |
| 871 | else |
| 872 | { |
| 873 | // No parameters were provided to iterate |
| 874 | common.errorMsg("${prefixMsg} No parameters were provided to iterate, skipping 'sequence' step") |
| 875 | sequenceSummary['nested_result'] = 'SUCCESS' |
| 876 | } |
| 877 | |
| 878 | return sequenceSummary |
| 879 | } |
| 880 | |
| 881 | |
| 882 | def checkResult(job_result, build_url, step, failed_jobs) { |
| 883 | // Check job result, in case of SUCCESS, move to next step. |
| 884 | // In case job has status NOT_BUILT, fail the build or keep going depending on 'ignore_not_built' flag |
| 885 | // In other cases check flag ignore_failed, if true ignore any statuses and keep going additionally |
| 886 | // if skip_results is not set or set to false fail entrie workflow, otherwise succed. |
| 887 | if (job_result != 'SUCCESS') { |
| 888 | def ignoreStepResult = false |
| 889 | switch (job_result) { |
| 890 | // In cases when job was waiting too long in queue or internal job logic allows to skip building, |
| 891 | // job may have NOT_BUILT status. In that case ignore_not_built flag can be used not to fail scenario. |
| 892 | case "NOT_BUILT": |
| 893 | ignoreStepResult = step['ignore_not_built'] ?: false |
| 894 | break |
| 895 | case "UNSTABLE": |
| 896 | ignoreStepResult = step['ignore_unstable'] ?: (step['ignore_failed'] ?: false) |
| 897 | if (ignoreStepResult && !step['skip_results'] ?: false) { |
| 898 | failed_jobs[build_url] = job_result |
| 899 | } |
| 900 | break |
azvyagintsev | e012e41 | 2024-05-22 16:09:23 +0300 | [diff] [blame] | 901 | case "ABORTED": |
| 902 | ignoreStepResult = step['ignore_aborted'] ?: (step['ignore_failed'] ?: false) |
| 903 | if (ignoreStepResult && !step['skip_results'] ?: false) { |
| 904 | failed_jobs[build_url] = job_result |
| 905 | } |
| 906 | break |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 907 | default: |
| 908 | ignoreStepResult = step['ignore_failed'] ?: false |
| 909 | if (ignoreStepResult && !step['skip_results'] ?: false) { |
| 910 | failed_jobs[build_url] = job_result |
| 911 | } |
| 912 | } |
| 913 | if (!ignoreStepResult) { |
| 914 | currentBuild.result = job_result |
| 915 | error "Job ${build_url} finished with result: ${job_result}" |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 920 | def runWorkflowStep(global_variables, step, step_id, jobs_data, global_jobs_data, failed_jobs, propagate, artifactoryBaseUrl, artifactoryServer, scriptsLibrary = null, prefixMsg = '', parent_global_variables=null) { |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 921 | def common = new com.mirantis.mk.Common() |
| 922 | |
| 923 | def _sep = "\n======================\n" |
| 924 | if (step.containsKey('job')) { |
| 925 | |
| 926 | common.printMsg("${_sep}${prefixMsg}Run job ${step['job']} [at ${java.time.LocalDateTime.now()}]${_sep}", "blue") |
| 927 | stage("Run job ${step['job']}") { |
| 928 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 929 | def job_summary = runStep(global_variables, step, propagate, artifactoryBaseUrl, artifactoryServer, parent_global_variables).call() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 930 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 931 | // Update jobs_data for updating description |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 932 | jobs_data[step_id]['build_url'] = job_summary['build_url'] |
| 933 | jobs_data[step_id]['build_id'] = job_summary['build_id'] |
| 934 | jobs_data[step_id]['status'] = job_summary['job_result'] |
| 935 | jobs_data[step_id]['duration'] = job_summary['buildDuration'] |
| 936 | jobs_data[step_id]['desc'] = job_summary['desc'] |
| 937 | if (job_summary['build_description']) { |
| 938 | jobs_data[step_id]['child_desc'] = job_summary['build_description'] |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 939 | } |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 940 | def job_result = job_summary['job_result'] |
| 941 | def build_url = job_summary['build_url'] |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 942 | common.printMsg("${_sep}${prefixMsg}Job ${build_url} finished with result: ${job_result} [at ${java.time.LocalDateTime.now()}]${_sep}", "blue") |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 943 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 944 | } |
| 945 | else if (step.containsKey('script')) { |
| 946 | common.printMsg("${_sep}${prefixMsg}Run script ${step['script']} [at ${java.time.LocalDateTime.now()}]${_sep}", "blue") |
| 947 | stage("Run script ${step['script']}") { |
| 948 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 949 | def scriptResult = runScript(global_variables, step, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, parent_global_variables) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 950 | |
| 951 | // Use build_url just as an unique key for failed_jobs. |
| 952 | // All characters after '#' are 'comment' |
| 953 | def build_url = "${env.BUILD_URL}#${step_id}:${step['script']}" |
| 954 | def job_result = scriptResult['script_result'] |
| 955 | common.printMsg("${_sep}${prefixMsg}Script ${build_url} finished with result: ${job_result} [at ${java.time.LocalDateTime.now()}]${_sep}", "blue") |
| 956 | |
| 957 | jobs_data[step_id]['build_url'] = build_url |
| 958 | jobs_data[step_id]['status'] = scriptResult['script_result'] |
| 959 | jobs_data[step_id]['desc'] = scriptResult['desc'] |
| 960 | if (scriptResult['build_description']) { |
| 961 | jobs_data[step_id]['child_desc'] = scriptResult['build_description'] |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | else if (step.containsKey('parallel')) { |
| 966 | common.printMsg("${_sep}${prefixMsg}Run steps in parallel [at ${java.time.LocalDateTime.now()}]:${_sep}", "blue") |
| 967 | stage("Run steps in parallel:") { |
| 968 | |
| 969 | // Allocate a map to collect nested steps data for updateDescription() |
| 970 | def nested_steps_data = [:] |
| 971 | jobs_data[step_id]['nested_steps_data'] = nested_steps_data |
| 972 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 973 | def parallelResult = runParallel(global_variables, step, failed_jobs, global_jobs_data, nested_steps_data, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, prefixMsg, parent_global_variables) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 974 | |
| 975 | // Use build_url just as an unique key for failed_jobs. |
| 976 | // All characters after '#' are 'comment' |
| 977 | def build_url = "${env.BUILD_URL}#${step_id}" |
| 978 | def job_result = parallelResult['nested_result'] |
| 979 | common.printMsg("${_sep}${prefixMsg}Parallel steps ${build_url} finished with result: ${job_result} [at ${java.time.LocalDateTime.now()}]${_sep}", "blue") |
| 980 | |
| 981 | jobs_data[step_id]['build_url'] = build_url |
| 982 | jobs_data[step_id]['status'] = parallelResult['nested_result'] |
| 983 | jobs_data[step_id]['desc'] = parallelResult['desc'] |
| 984 | if (parallelResult['build_description']) { |
| 985 | jobs_data[step_id]['child_desc'] = parallelResult['build_description'] |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | else if (step.containsKey('sequence')) { |
| 990 | common.printMsg("${_sep}${prefixMsg}Run steps in sequence [at ${java.time.LocalDateTime.now()}]:${_sep}", "blue") |
| 991 | stage("Run steps in sequence:") { |
| 992 | |
| 993 | // Allocate a map to collect nested steps data for updateDescription() |
| 994 | def nested_steps_data = [:] |
| 995 | jobs_data[step_id]['nested_steps_data'] = nested_steps_data |
| 996 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 997 | def sequenceResult = runSequence(global_variables, step, failed_jobs, global_jobs_data, nested_steps_data, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, prefixMsg, parent_global_variables) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 998 | |
| 999 | // Use build_url just as an unique key for failed_jobs. |
| 1000 | // All characters after '#' are 'comment' |
| 1001 | def build_url = "${env.BUILD_URL}#${step_id}" |
| 1002 | def job_result = sequenceResult['nested_result'] |
| 1003 | common.printMsg("${_sep}${prefixMsg}Sequence steps ${build_url} finished with result: ${job_result} [at ${java.time.LocalDateTime.now()}]${_sep}", "blue") |
| 1004 | |
| 1005 | jobs_data[step_id]['build_url'] = build_url |
| 1006 | jobs_data[step_id]['status'] = sequenceResult['nested_result'] |
| 1007 | jobs_data[step_id]['desc'] = sequenceResult['desc'] |
| 1008 | if (sequenceResult['build_description']) { |
| 1009 | jobs_data[step_id]['child_desc'] = sequenceResult['build_description'] |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | updateDescription(global_jobs_data) |
| 1015 | |
| 1016 | job_result = jobs_data[step_id]['status'] |
| 1017 | checkResult(job_result, build_url, step, failed_jobs) |
| 1018 | |
| 1019 | // return build_url |
| 1020 | |
| 1021 | } |
| 1022 | |
| 1023 | /** |
| 1024 | * Run the workflow or final steps one by one |
| 1025 | * |
| 1026 | * @param steps List of steps (Jenkins jobs) to execute |
| 1027 | * @param global_variables Map where the collected artifact URLs and 'env' objects are stored |
| 1028 | * @param failed_jobs Map with failed job names and result statuses, to report it later |
| 1029 | * @param jobs_data Map with all job names and result statuses, to showing it in description |
| 1030 | * @param step_id Counter for matching step ID with cell ID in description table |
| 1031 | * @param propagate Boolean. If false: allows to collect artifacts after job is finished, even with FAILURE status |
| 1032 | * If true: immediatelly fails the pipeline. DO NOT USE 'true' with runScenario(). |
| 1033 | */ |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 1034 | def runSteps(steps, global_variables, failed_jobs, jobs_data, global_jobs_data, step_id, Boolean propagate = false, artifactoryBaseUrl = '', artifactoryServer = '', scriptsLibrary = null, prefixMsg = '', parent_global_variables=null) { |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1035 | // Show expected jobs list in description |
| 1036 | updateDescription(global_jobs_data) |
| 1037 | |
| 1038 | for (step in steps) { |
| 1039 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 1040 | runWorkflowStep(global_variables, step, step_id, jobs_data, global_jobs_data, failed_jobs, propagate, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, prefixMsg, parent_global_variables) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1041 | |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 1042 | // Jump to next ID for updating next job data in description table |
| 1043 | step_id++ |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 1044 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1045 | } |
| 1046 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1047 | |
| 1048 | /** |
| 1049 | * Prepare jobs_data for generating the scenario description |
| 1050 | */ |
| 1051 | def prepareJobsData(scenario_steps, step_type, jobs_data) { |
| 1052 | def list_id = jobs_data.size() |
| 1053 | |
| 1054 | for (step in scenario_steps) { |
| 1055 | def display_name = '' |
| 1056 | def step_key = '' |
| 1057 | def desc = '' |
| 1058 | |
| 1059 | if (step.containsKey('job')) { |
| 1060 | display_name = step['job'] |
| 1061 | step_key = 'job' |
| 1062 | } |
| 1063 | else if (step.containsKey('script')) { |
| 1064 | display_name = step['script'] |
| 1065 | step_key = 'script' |
| 1066 | } |
| 1067 | else if (step.containsKey('parallel')) { |
| 1068 | display_name = 'Parallel steps' |
| 1069 | step_key = 'parallel' |
| 1070 | } |
| 1071 | else if (step.containsKey('sequence')) { |
| 1072 | display_name = 'Sequence steps' |
| 1073 | step_key = 'sequence' |
| 1074 | } |
| 1075 | |
| 1076 | if (step['description'] != null && step['description'] != 'null' && step['description'].toString() != '') { |
| 1077 | desc = (step['description'] ?: '').toString() |
| 1078 | } |
| 1079 | |
| 1080 | jobs_data.add([list_id : "$list_id", |
| 1081 | type : step_type, |
| 1082 | name : "$display_name", |
| 1083 | build_url : "0", |
| 1084 | build_id : "-", |
| 1085 | status : "-", |
| 1086 | desc : desc, |
| 1087 | child_desc : "", |
| 1088 | duration : '-', |
| 1089 | step_key : step_key, |
| 1090 | together_steps: [], |
| 1091 | ]) |
| 1092 | list_id += 1 |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1097 | /** |
| 1098 | * Run the workflow scenario |
| 1099 | * |
| 1100 | * @param scenario: Map with scenario steps. |
| 1101 | |
| 1102 | * There are two keys in the scenario: |
| 1103 | * workflow: contains steps to run deploy and test jobs |
| 1104 | * finally: contains steps to run report and cleanup jobs |
| 1105 | * |
| 1106 | * Scenario execution example: |
| 1107 | * |
| 1108 | * scenario_yaml = """\ |
| 1109 | * workflow: |
| 1110 | * - job: deploy-kaas |
| 1111 | * ignore_failed: false |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 1112 | * description: "Management cluster ${KAAS_VERSION}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1113 | * parameters: |
| 1114 | * KAAS_VERSION: |
| 1115 | * type: StringParameterValue |
| 1116 | * use_variable: KAAS_VERSION |
| 1117 | * artifacts: |
| 1118 | * KUBECONFIG_ARTIFACT: artifacts/management_kubeconfig |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 1119 | * DEPLOYED_KAAS_VERSION: artifacts/management_version |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1120 | * |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 1121 | * - job: create-child |
| 1122 | * inherit_parent_params: true |
| 1123 | * ignore_failed: false |
| 1124 | * parameters: |
| 1125 | * KUBECONFIG_ARTIFACT_URL: |
| 1126 | * type: StringParameterValue |
| 1127 | * use_variable: KUBECONFIG_ARTIFACT |
| 1128 | * KAAS_VERSION: |
| 1129 | * type: StringParameterValue |
| 1130 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 1131 | * RELEASE_NAME: |
| 1132 | * type: StringParameterValue |
| 1133 | * get_variable_from_yaml: |
| 1134 | * yaml_url: SI_CONFIG_ARTIFACT |
| 1135 | * yaml_key: .clusters[0].release_name |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 1136 | * global_artifacts: |
| 1137 | * CHILD_CONFIG_1: artifacts/child_kubeconfig |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 1138 | * |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1139 | * - job: test-kaas-ui |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 1140 | * ignore_not_built: false |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1141 | * parameters: |
| 1142 | * KUBECONFIG_ARTIFACT_URL: |
| 1143 | * type: StringParameterValue |
| 1144 | * use_variable: KUBECONFIG_ARTIFACT |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 1145 | * KAAS_VERSION: |
| 1146 | * type: StringParameterValue |
| 1147 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1148 | * artifacts: |
| 1149 | * REPORT_SI_KAAS_UI: artifacts/test_kaas_ui_result.xml |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1150 | * finally: |
| 1151 | * - job: testrail-report |
| 1152 | * ignore_failed: true |
| 1153 | * parameters: |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 1154 | * KAAS_VERSION: |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1155 | * type: StringParameterValue |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 1156 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 1157 | * REPORTS_LIST: |
| 1158 | * type: TextParameterValue |
| 1159 | * use_template: | |
| 1160 | * REPORT_SI_KAAS_UI: \$REPORT_SI_KAAS_UI |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1161 | * """ |
| 1162 | * |
| 1163 | * runScenario(scenario) |
| 1164 | * |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 1165 | * Scenario workflow keys: |
| 1166 | * |
| 1167 | * job: string. Jenkins job name |
| 1168 | * ignore_failed: bool. if true, keep running the workflow jobs if the job is failed, but fail the workflow at finish |
Sergey Lalov | 3a2e790 | 2023-07-27 01:19:02 +0400 | [diff] [blame] | 1169 | * ignore_unstable: bool. if true, keep running the workflow jobs if the job is unstable, but mark the workflow is unstable at finish |
azvyagintsev | e012e41 | 2024-05-22 16:09:23 +0300 | [diff] [blame] | 1170 | * ignore_aborted: bool. if true, keep running the workflow jobs if the job is aborted, but mark the workflow is unstable at finish |
Vasyl Saienko | e72b994 | 2021-03-04 10:54:49 +0200 | [diff] [blame] | 1171 | * 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] | 1172 | * 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 |
| 1173 | * inherit_parent_params: bool. if true, provide all parameters from the parent job to the child job as defaults |
| 1174 | * 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] | 1175 | * wf_pause_step_before_run: bool. Interactive pause exact step before run. |
| 1176 | * wf_pause_step_slack_report_channel: If step paused, send message about it in slack. |
| 1177 | * wf_pause_step_timeout: timeout im minutes to wait for manual unpause. |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1178 | */ |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1179 | def runScenario(scenario, slackReportChannel = '', artifactoryBaseUrl = '', Boolean logGlobalVariables = false, artifactoryServer = '', scriptsLibrary = null, |
| 1180 | global_variables = null, failed_jobs = null, jobs_data = null) { |
| 1181 | def common = new com.mirantis.mk.Common() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1182 | |
Dennis Dmitriev | 79f3a2d | 2019-08-09 16:06:00 +0300 | [diff] [blame] | 1183 | // Clear description before adding new messages |
| 1184 | currentBuild.description = '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1185 | // Collect the parameters for the jobs here |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1186 | if (global_variables == null) { |
| 1187 | global_variables = [:] |
| 1188 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1189 | // List of failed jobs to show at the end |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1190 | if (failed_jobs == null) { |
| 1191 | failed_jobs = [:] |
| 1192 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 1193 | // Jobs data to use for wf job build description |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1194 | if (jobs_data == null) { |
| 1195 | jobs_data = [] |
| 1196 | } |
| 1197 | def global_jobs_data = jobs_data |
| 1198 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 1199 | // Counter for matching step ID with cell ID in description table |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1200 | def step_id = jobs_data.size() |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 1201 | // Generate expected list jobs for description |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1202 | prepareJobsData(scenario['workflow'], 'workflow', jobs_data) |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 1203 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1204 | def pause_step_id = jobs_data.size() |
| 1205 | // Generate expected list jobs for description |
| 1206 | prepareJobsData(scenario['pause'], 'pause', jobs_data) |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1207 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1208 | def finally_step_id = jobs_data.size() |
| 1209 | // Generate expected list jobs for description |
| 1210 | prepareJobsData(scenario['finally'], 'finally', jobs_data) |
| 1211 | |
| 1212 | |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1213 | def job_failed_flag = false |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1214 | try { |
| 1215 | // Run the 'workflow' jobs |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 1216 | runSteps(scenario['workflow'], global_variables, failed_jobs, jobs_data, global_jobs_data, step_id, false, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, '', global_variables) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1217 | } catch (InterruptedException e) { |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1218 | job_failed_flag = true |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1219 | error "The job was aborted" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1220 | } catch (e) { |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1221 | job_failed_flag = true |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1222 | printStackTrace(e) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1223 | error("Build failed: " + e.toString()) |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1224 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1225 | } finally { |
Dennis Dmitriev | 38a45cd | 2023-02-27 14:22:13 +0200 | [diff] [blame] | 1226 | // Log global_variables |
| 1227 | if (logGlobalVariables) { |
| 1228 | printVariables(global_variables) |
| 1229 | } |
| 1230 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1231 | def flag_pause_variable = (env.PAUSE_FOR_DEBUG) != null |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1232 | // Run the 'finally' or 'pause' jobs |
Sergey Lalov | 6e9400c | 2022-11-17 12:59:31 +0400 | [diff] [blame] | 1233 | common.infoMsg(failed_jobs) |
Sergey Lalov | 2d1cd9c | 2023-08-03 17:08:09 +0400 | [diff] [blame] | 1234 | // Run only if there are failed jobs in the scenario |
| 1235 | if (flag_pause_variable && (PAUSE_FOR_DEBUG && job_failed_flag)) { |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1236 | // Switching to 'pause' step index |
| 1237 | common.infoMsg("FINALLY BLOCK - PAUSE") |
| 1238 | step_id = pause_step_id |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 1239 | runSteps(scenario['pause'], global_variables, failed_jobs, jobs_data, global_jobs_data, step_id, false, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, '', global_variables) |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1240 | |
| 1241 | } |
| 1242 | // Switching to 'finally' step index |
| 1243 | common.infoMsg("FINALLY BLOCK - CLEAR") |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 1244 | step_id = finally_step_id |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 1245 | runSteps(scenario['finally'], global_variables, failed_jobs, jobs_data, global_jobs_data, step_id, false, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, '', global_variables) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1246 | |
| 1247 | if (failed_jobs) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 1248 | def statuses = [] |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 1249 | failed_jobs.each { |
sgudz | 74c8cdd | 2020-01-23 14:26:32 +0200 | [diff] [blame] | 1250 | statuses += it.value |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 1251 | } |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 1252 | if (statuses.contains('FAILURE')) { |
| 1253 | currentBuild.result = 'FAILURE' |
Sergey Lalov | e5e0a84 | 2023-10-02 15:55:59 +0400 | [diff] [blame] | 1254 | } else if (statuses.contains('ABORTED')) { |
| 1255 | currentBuild.result = 'ABORTED' |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1256 | } else if (statuses.contains('UNSTABLE')) { |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 1257 | currentBuild.result = 'UNSTABLE' |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 1258 | } else { |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 1259 | currentBuild.result = 'FAILURE' |
| 1260 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1261 | println "Failed jobs: ${failed_jobs}" |
vnaumov | 68cba27 | 2020-05-20 11:24:02 +0200 | [diff] [blame] | 1262 | } else { |
| 1263 | currentBuild.result = 'SUCCESS' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1264 | } |
vnaumov | 5a6eb8a | 2020-03-31 11:16:54 +0200 | [diff] [blame] | 1265 | |
Sergey Lalov | 3a2e790 | 2023-07-27 01:19:02 +0400 | [diff] [blame] | 1266 | common.infoMsg("Workflow finished with result: ${currentBuild.result}") |
| 1267 | |
vnaumov | 5a6eb8a | 2020-03-31 11:16:54 +0200 | [diff] [blame] | 1268 | if (slackReportChannel) { |
| 1269 | def slack = new com.mirantis.mcp.SlackNotification() |
| 1270 | slack.jobResultNotification(currentBuild.result, slackReportChannel, '', null, '', 'slack_webhook_url') |
| 1271 | } |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 1272 | } // finally |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1273 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1274 | |
| 1275 | |
| 1276 | def manageArtifacts(entrypointDirectory, storeArtsInJenkins = false, artifactoryServerName = 'mcp-ci') { |
| 1277 | def mcpArtifactory = new com.mirantis.mcp.MCPArtifactory() |
| 1278 | def artifactoryRepoPath = "si-local/jenkins-job-artifacts/${JOB_NAME}/${BUILD_NUMBER}" |
| 1279 | def tests_log = "${entrypointDirectory}/tests.log" |
| 1280 | |
| 1281 | if (fileExists(tests_log)) { |
| 1282 | try { |
| 1283 | def size = sh([returnStdout: true, script: "stat --printf='%s' ${tests_log}"]).trim().toInteger() |
| 1284 | // do not archive unless it is more than 50 MB |
| 1285 | def allowed_size = 1048576 * 50 |
| 1286 | if (size >= allowed_size) { |
| 1287 | sh("gzip ${tests_log} || true") |
| 1288 | } |
| 1289 | } catch (e) { |
| 1290 | print("Cannot determine tests.log filesize: ${e}") |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | if (storeArtsInJenkins) { |
| 1295 | archiveArtifacts( |
| 1296 | artifacts: "${entrypointDirectory}/**", |
| 1297 | allowEmptyArchive: true |
| 1298 | ) |
| 1299 | } |
| 1300 | artConfig = [ |
| 1301 | deleteArtifacts: false, |
| 1302 | artifactory : artifactoryServerName, |
| 1303 | artifactPattern: "${entrypointDirectory}/**", |
| 1304 | artifactoryRepo: "artifactory/${artifactoryRepoPath}", |
| 1305 | ] |
| 1306 | def artDescription = mcpArtifactory.uploadArtifactsToArtifactory(artConfig) |
Vasyl Saienko | c0c029e | 2024-10-03 09:24:23 +0300 | [diff] [blame] | 1307 | if (currentBuild.description) { |
| 1308 | currentBuild.description += "${artDescription}<br>" |
| 1309 | } else { |
| 1310 | currentBuild.description = "${artDescription}<br>" |
| 1311 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1312 | |
| 1313 | junit(testResults: "${entrypointDirectory}/**/*.xml", allowEmptyResults: true) |
| 1314 | |
| 1315 | def artifactoryServer = Artifactory.server(artifactoryServerName) |
| 1316 | def artifactsUrl = "${artifactoryServer.getUrl()}/artifactory/${artifactoryRepoPath}" |
| 1317 | return artifactsUrl |
| 1318 | } |
| 1319 | |
| 1320 | |
| 1321 | return this |