Filip Pytloun | fcce97c | 2017-03-07 14:06:07 +0100 | [diff] [blame] | 1 | def gerrit = new com.mirantis.mk.Gerrit() |
Filip Pytloun | 6a05730 | 2017-03-07 16:33:30 +0100 | [diff] [blame] | 2 | def common = new com.mirantis.mk.Common() |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 3 | |
Filip Pytloun | fcce97c | 2017-03-07 14:06:07 +0100 | [diff] [blame] | 4 | def gerritCredentials |
| 5 | try { |
| 6 | gerritCredentials = CREDENTIALS_ID |
| 7 | } catch (MissingPropertyException e) { |
| 8 | gerritCredentials = "gerrit" |
| 9 | } |
| 10 | |
Jakub Josef | 4612c5d | 2017-03-30 16:04:26 +0200 | [diff] [blame] | 11 | def gerritRef |
| 12 | try { |
| 13 | gerritRef = GERRIT_REFSPEC |
| 14 | } catch (MissingPropertyException e) { |
| 15 | gerritRef = null |
| 16 | } |
| 17 | |
| 18 | def defaultGitRef, defaultGitUrl |
| 19 | try { |
| 20 | defaultGitRef = DEFAULT_GIT_REF |
| 21 | defaultGitUrl = DEFAULT_GIT_URL |
| 22 | } catch (MissingPropertyException e) { |
| 23 | defaultGitRef = null |
| 24 | defaultGitUrl = null |
| 25 | } |
| 26 | def checkouted = false |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 27 | def merged = false |
chnyda | d66d6fa | 2017-06-22 09:34:43 +0200 | [diff] [blame] | 28 | def systemRefspec = "HEAD" |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 29 | timeout(time: 12, unit: 'HOURS') { |
| 30 | node() { |
| 31 | try { |
| 32 | stage("Checkout") { |
| 33 | if (gerritRef) { |
| 34 | // job is triggered by Gerrit |
| 35 | // test if change aren't already merged |
| 36 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, gerritCredentials) |
| 37 | merged = gerritChange.status == "MERGED" |
| 38 | if(!merged){ |
| 39 | checkouted = gerrit.gerritPatchsetCheckout ([ |
| 40 | credentialsId : gerritCredentials |
| 41 | ]) |
| 42 | systemRefspec = GERRIT_REFSPEC |
chnyda | 708781e | 2017-10-02 15:40:07 +0200 | [diff] [blame] | 43 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 44 | // change defaultGit variables if job triggered from Gerrit |
| 45 | defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}" |
| 46 | } else if(defaultGitRef && defaultGitUrl) { |
| 47 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", gerritCredentials) |
| 48 | } |
| 49 | } |
chnyda | 708781e | 2017-10-02 15:40:07 +0200 | [diff] [blame] | 50 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 51 | stage("Test") { |
| 52 | if(merged){ |
| 53 | common.successMsg("Gerrit change is already merged, no need to test them") |
| 54 | }else{ |
| 55 | if(checkouted){ |
chnyda | 9bb38b2 | 2017-11-13 13:55:57 +0100 | [diff] [blame] | 56 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 57 | def documentationOnly = false |
| 58 | if (gerritRef) { |
| 59 | documentationOnly = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v .releasenotes", returnStatus: true) == 1 |
| 60 | } |
| 61 | |
| 62 | sh("git diff-tree --no-commit-id --diff-filter=d --name-only -r HEAD | grep .yml | xargs -I {} python -c \"import yaml; yaml.load(open('{}', 'r'))\" \\;") |
| 63 | |
| 64 | def branches = [:] |
| 65 | def testModels = documentationOnly ? [] : TEST_MODELS.split(',') |
| 66 | for (int i = 0; i < testModels.size(); i++) { |
| 67 | def cluster = testModels[i] |
| 68 | def clusterGitUrl = defaultGitUrl.substring(0, defaultGitUrl.lastIndexOf("/") + 1) + cluster |
| 69 | branches["${cluster}"] = { |
| 70 | build job: "test-salt-model-${cluster}", parameters: [ |
| 71 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl], |
| 72 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"], |
| 73 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: defaultGitUrl], |
| 74 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: systemRefspec] |
| 75 | ] |
| 76 | } |
| 77 | } |
| 78 | branches["cookiecutter"] = { |
| 79 | build job: "test-mk-cookiecutter-templates", parameters: [ |
chnyda | 6f78a9d | 2017-12-19 11:34:26 +0100 | [diff] [blame] | 80 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: defaultGitUrl], |
| 81 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: systemRefspec] |
| 82 | ] |
chnyda | 708781e | 2017-10-02 15:40:07 +0200 | [diff] [blame] | 83 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 84 | parallel branches |
| 85 | }else{ |
| 86 | throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null") |
chnyda | 6f78a9d | 2017-12-19 11:34:26 +0100 | [diff] [blame] | 87 | } |
chnyda | 708781e | 2017-10-02 15:40:07 +0200 | [diff] [blame] | 88 | } |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 89 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 90 | } catch (Throwable e) { |
| 91 | // If there was an error or exception thrown, the build failed |
| 92 | currentBuild.result = "FAILURE" |
| 93 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 94 | throw e |
| 95 | } finally { |
| 96 | common.sendNotification(currentBuild.result,"",["slack"]) |
Filip Pytloun | fcce97c | 2017-03-07 14:06:07 +0100 | [diff] [blame] | 97 | } |
Filip Pytloun | fcce97c | 2017-03-07 14:06:07 +0100 | [diff] [blame] | 98 | } |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 99 | } |