Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame^] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Run a simple workflow |
| 6 | * |
| 7 | * Function runScenario() executes a sequence of jobs, like |
| 8 | * - Parameters for the jobs are taken from the 'env' object |
| 9 | * - URLs of artifacts from completed jobs may be passed |
| 10 | * as parameters to the next jobs. |
| 11 | * |
| 12 | * No constants, environment specific logic or other conditional dependencies. |
| 13 | * All the logic should be placed in the workflow jobs, and perform necessary |
| 14 | * actions depending on the job parameters. |
| 15 | * The runScenario() function only provides the |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * Run a Jenkins job using the collected parameters |
| 22 | * |
| 23 | * @param job_name Name of the running job |
| 24 | * @param job_parameters Map that declares which values from global_variables should be used, in the following format: |
| 25 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'use_variable': <a key from global_variables>}, ...} |
| 26 | * @param global_variables Map that keeps the artifact URLs and used 'env' objects: |
| 27 | * {'PARAM1_NAME': <param1 value>, 'PARAM2_NAME': 'http://.../artifacts/param2_value', ...} |
| 28 | */ |
| 29 | def runJob(job_name, job_parameters, global_variables) { |
| 30 | def parameters = [] |
| 31 | |
| 32 | // Collect required parameters from 'global_variables' or 'env' |
| 33 | for (param in job_parameters) { |
| 34 | if (!global_variables[param.value.use_variable]) { |
| 35 | global_variables[param.value.use_variable] = env[param.value.use_variable] ?: '' |
| 36 | } |
| 37 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: global_variables[param.value.use_variable]]) |
| 38 | println "${param.key}: <${param.value.type}> ${global_variables[param.value.use_variable]}" |
| 39 | } |
| 40 | |
| 41 | // Build the job |
| 42 | def job_info = build job: "${job_name}", parameters: parameters, propagate: false |
| 43 | return job_info |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Store URLs of the specified artifacts to the global_variables |
| 48 | * |
| 49 | * @param build_url URL of the completed job |
| 50 | * @param step_artifacts Map that contains artifact names in the job, and variable names |
| 51 | * where the URLs to that atrifacts should be stored, for example: |
| 52 | * {'ARTIFACT1': 'logs.tar.gz', 'ARTIFACT2': 'test_report.xml', ...} |
| 53 | * @param global_variables Map that will keep the artifact URLs. Variable 'ARTIFACT1', for example, |
| 54 | * be used in next job parameters: {'ARTIFACT1_URL':{ 'use_variable': 'ARTIFACT1', ...}} |
| 55 | * |
| 56 | * If the artifact with the specified name not found, the parameter ARTIFACT1_URL |
| 57 | * will be empty. |
| 58 | * |
| 59 | */ |
| 60 | def storeArtifacts(build_url, step_artifacts, global_variables) { |
| 61 | def http = new com.mirantis.mk.Http() |
| 62 | def base = [:] |
| 63 | base["url"] = build_url |
| 64 | def job_config = http.restGet(base, "/api/json/") |
| 65 | def job_artifacts = job_config['artifacts'] |
| 66 | for (artifact in step_artifacts) { |
| 67 | def job_artifact = job_artifacts.findAll { item -> artifact.value == item['fileName'] || artifact.value == item['relativePath'] } |
| 68 | if (job_artifact.size() == 1) { |
| 69 | // Store artifact URL |
| 70 | def artifact_url = "${build_url}artifact/${job_artifact[0]['relativePath']}" |
| 71 | global_variables[artifact.key] = artifact_url |
| 72 | println "Artifact URL ${artifact_url} stored to ${artifact.key}" |
| 73 | } else if (job_artifact.size() > 1) { |
| 74 | // Error: too many artifacts with the same name, fail the job |
| 75 | error "Multiple artifacts ${artifact.value} for ${artifact.key} found in the build results ${build_url}, expected one:\n${job_artifact}" |
| 76 | } else { |
| 77 | // Warning: no artifact with expected name |
| 78 | println "Artifact ${artifact.value} for ${artifact.key} not found in the build results ${build_url}, found the following artifacts:\n${job_artifacts}" |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * Run the workflow or final steps one by one |
| 86 | * |
| 87 | * @param steps List of steps (Jenkins jobs) to execute |
| 88 | * @param global_variables Map where the collected artifact URLs and 'env' objects are stored |
| 89 | * @param failed_jobs Map with failed job names and result statuses, to report it later |
| 90 | */ |
| 91 | def runSteps(steps, global_variables, failed_jobs) { |
| 92 | currentBuild.description = '' |
| 93 | for (step in steps) { |
| 94 | stage("Running job ${step['job']}") { |
| 95 | |
| 96 | def job_name = step['job'] |
| 97 | def job_parameters = step['parameters'] |
| 98 | // Collect job parameters and run the job |
| 99 | def job_info = runJob(job_name, job_parameters, global_variables) |
| 100 | def job_result = job_info.getResult() |
| 101 | def build_url = job_info.getAbsoluteUrl() |
| 102 | def build_description = job_info.getDescription() |
| 103 | |
| 104 | currentBuild.description += "<a href=${build_url}>${job_name}</a>: ${job_result}<br>" |
| 105 | // Import the remote build description into the current build |
| 106 | if (build_description) { // TODO - add also the job status |
| 107 | currentBuild.description += build_description |
| 108 | } |
| 109 | |
| 110 | // Store links to the resulting artifacts into 'global_variables' |
| 111 | storeArtifacts(build_url, step['artifacts'], global_variables) |
| 112 | |
| 113 | // Job failed, fail the build or keep going depending on 'ignore_failed' flag |
| 114 | if (job_result != "SUCCESS") { |
| 115 | def job_ignore_failed = step['ignore_failed'] ?: false |
| 116 | failed_jobs[build_url] = job_result |
| 117 | if (job_ignore_failed) { |
| 118 | println "Job ${build_url} finished with result: ${job_result}" |
| 119 | } else { |
| 120 | currentBuild.result = job_result |
| 121 | error "Job ${build_url} finished with result: ${job_result}" |
| 122 | } |
| 123 | } // if (job_result == "SUCCESS") |
| 124 | } // stage ("Running job ${step['job']}") |
| 125 | } // for (step in scenario['workflow']) |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Run the workflow scenario |
| 130 | * |
| 131 | * @param scenario: Map with scenario steps. |
| 132 | |
| 133 | * There are two keys in the scenario: |
| 134 | * workflow: contains steps to run deploy and test jobs |
| 135 | * finally: contains steps to run report and cleanup jobs |
| 136 | * |
| 137 | * Scenario execution example: |
| 138 | * |
| 139 | * scenario_yaml = """\ |
| 140 | * workflow: |
| 141 | * - job: deploy-kaas |
| 142 | * ignore_failed: false |
| 143 | * parameters: |
| 144 | * KAAS_VERSION: |
| 145 | * type: StringParameterValue |
| 146 | * use_variable: KAAS_VERSION |
| 147 | * artifacts: |
| 148 | * KUBECONFIG_ARTIFACT: artifacts/management_kubeconfig |
| 149 | * |
| 150 | * - job: test-kaas-ui |
| 151 | * ignore_failed: false |
| 152 | * parameters: |
| 153 | * KUBECONFIG_ARTIFACT_URL: |
| 154 | * type: StringParameterValue |
| 155 | * use_variable: KUBECONFIG_ARTIFACT |
| 156 | * artifacts: |
| 157 | * REPORT_SI_KAAS_UI: artifacts/test_kaas_ui_result.xml |
| 158 | * |
| 159 | * finally: |
| 160 | * - job: testrail-report |
| 161 | * ignore_failed: true |
| 162 | * parameters: |
| 163 | * REPORT_SI_KAAS_UI_URL: |
| 164 | * type: StringParameterValue |
| 165 | * use_variable: REPORT_SI_KAAS_UI |
| 166 | * """ |
| 167 | * |
| 168 | * runScenario(scenario) |
| 169 | * |
| 170 | */ |
| 171 | |
| 172 | def runScenario(scenario) { |
| 173 | |
| 174 | // Collect the parameters for the jobs here |
| 175 | global_variables = [:] |
| 176 | // List of failed jobs to show at the end |
| 177 | failed_jobs = [:] |
| 178 | |
| 179 | try { |
| 180 | // Run the 'workflow' jobs |
| 181 | runSteps(scenario['workflow'], global_variables, failed_jobs) |
| 182 | |
| 183 | } catch (InterruptedException x) { |
| 184 | error "The job was aborted" |
| 185 | |
| 186 | } catch (e) { |
| 187 | error("Build failed: " + e.toString()) |
| 188 | |
| 189 | } finally { |
| 190 | // Run the 'finally' jobs |
| 191 | runSteps(scenario['finally'], global_variables, failed_jobs) |
| 192 | |
| 193 | if (failed_jobs) { |
| 194 | println "Failed jobs: ${failed_jobs}" |
| 195 | currentBuild.result = "FAILED" |
| 196 | } |
| 197 | } // try |
| 198 | } |