Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 1 | /** |
| 2 | * JS testing pipeline |
| 3 | * CREDENTIALS_ID - gerrit credentials id |
Mikhail Ivanov | cd0a6f4 | 2017-05-17 12:55:42 +0400 | [diff] [blame] | 4 | * COMPOSE_PATH - path to compose file in repository |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 5 | * NODE_IMAGE - NodeJS with NPM Docker image name |
| 6 | * COMMANDS - a list of command(s) to run |
| 7 | **/ |
| 8 | |
| 9 | gerrit = new com.mirantis.mk.Gerrit() |
| 10 | common = new com.mirantis.mk.Common() |
| 11 | |
Mikhail Ivanov | cd50cce | 2017-06-02 17:57:26 +0400 | [diff] [blame] | 12 | def executeCmd(containerName, cmd) { |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 13 | stage(cmd) { |
Mikhail Ivanov | cd50cce | 2017-06-02 17:57:26 +0400 | [diff] [blame] | 14 | assert containerName != null |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 15 | common.infoMsg("Starting command: ${cmd}") |
Ilya Kharin | 817d6d6 | 2017-06-20 19:43:54 +0400 | [diff] [blame] | 16 | wrap([$class: 'AnsiColorBuildWrapper']) { |
| 17 | sh("docker exec ${containerName} ${cmd}") |
| 18 | } |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 19 | common.successMsg("Successfully completed: ${cmd}") |
| 20 | } |
| 21 | } |
| 22 | |
Mikhail Ivanov | 854f21f | 2017-04-14 17:06:31 +0400 | [diff] [blame] | 23 | def gerritRef |
| 24 | try { |
| 25 | gerritRef = GERRIT_REFSPEC |
| 26 | } catch (MissingPropertyException e) { |
| 27 | gerritRef = null |
| 28 | } |
| 29 | |
| 30 | def defaultGitRef, defaultGitUrl |
| 31 | try { |
| 32 | defaultGitRef = DEFAULT_GIT_REF |
| 33 | defaultGitUrl = DEFAULT_GIT_URL |
| 34 | } catch (MissingPropertyException e) { |
| 35 | defaultGitRef = null |
| 36 | defaultGitUrl = null |
| 37 | } |
| 38 | def checkouted = false |
| 39 | |
Mikhail Ivanov | 9841192 | 2017-06-01 16:27:10 +0400 | [diff] [blame] | 40 | node("vm") { |
Mikhail Ivanov | cd50cce | 2017-06-02 17:57:26 +0400 | [diff] [blame] | 41 | def containerName |
Mikhail Ivanov | cd0a6f4 | 2017-05-17 12:55:42 +0400 | [diff] [blame] | 42 | def uniqId |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 43 | try { |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 44 | stage('Checkout source code') { |
Mikhail Ivanov | 854f21f | 2017-04-14 17:06:31 +0400 | [diff] [blame] | 45 | if (gerritRef) { |
| 46 | // job is triggered by Gerrit |
Jakub Josef | f59e0bb | 2017-04-17 11:35:50 +0200 | [diff] [blame] | 47 | checkouted = gerrit.gerritPatchsetCheckout ([ |
Mikhail Ivanov | 854f21f | 2017-04-14 17:06:31 +0400 | [diff] [blame] | 48 | credentialsId : CREDENTIALS_ID, |
| 49 | withWipeOut : true, |
| 50 | ]) |
| 51 | } else if(defaultGitRef && defaultGitUrl) { |
| 52 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
| 53 | } |
| 54 | if(!checkouted){ |
| 55 | throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null") |
| 56 | } |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 57 | } |
Mikhail Ivanov | cd0a6f4 | 2017-05-17 12:55:42 +0400 | [diff] [blame] | 58 | stage('Start container') { |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 59 | def workspace = common.getWorkspace() |
Mikhail Ivanov | cd0a6f4 | 2017-05-17 12:55:42 +0400 | [diff] [blame] | 60 | def timeStamp = new Date().format("HHmmss", TimeZone.getTimeZone('UTC')) |
| 61 | if (gerritRef) { |
| 62 | uniqId = gerritRef.tokenize('/').takeRight(2).join('') + timeStamp |
| 63 | } else { |
| 64 | uniqId = defaultGitRef.tokenize('/').takeRight(2).join('') + timeStamp |
| 65 | } |
| 66 | sh("docker-compose -f ${COMPOSE_PATH} -p ${uniqId} up -d") |
Mikhail Ivanov | cd50cce | 2017-06-02 17:57:26 +0400 | [diff] [blame] | 67 | containerName = "${uniqId}_devopsportal_1" |
| 68 | common.successMsg("Container with id ${containerName} started.") |
| 69 | sh("docker cp ${workspace}/. ${containerName}:/opt/workspace/") |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 70 | } |
Mikhail Ivanov | cd50cce | 2017-06-02 17:57:26 +0400 | [diff] [blame] | 71 | executeCmd(containerName, "npm install") |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 72 | def cmds = COMMANDS.tokenize('\n') |
| 73 | for (int i = 0; i < cmds.size(); i++) { |
Ilya Kharin | ea0b653 | 2017-06-05 12:41:10 +0400 | [diff] [blame] | 74 | timeout(5) { |
| 75 | executeCmd(containerName, cmds[i]) |
| 76 | } |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 77 | } |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 78 | } catch (err) { |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 79 | currentBuild.result = 'FAILURE' |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 80 | common.errorMsg("Build failed due to error: ${err}") |
| 81 | throw err |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 82 | } finally { |
| 83 | common.sendNotification(currentBuild.result, "" ,["slack"]) |
Ilya Kharin | 54987d9 | 2017-06-20 18:55:30 +0400 | [diff] [blame] | 84 | stage('Attach artifacts') { |
| 85 | if (containerName != null) { |
| 86 | sh("docker cp ${containerName}:/opt/workspace/test_output ${workspace}/test_output") |
| 87 | archiveArtifacts( |
| 88 | artifacts: "${workspace}/test_output/screenshots/*.png", |
| 89 | ) |
| 90 | } |
| 91 | } |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 92 | stage('Cleanup') { |
Mikhail Ivanov | cd50cce | 2017-06-02 17:57:26 +0400 | [diff] [blame] | 93 | if (containerName != null) { |
Mikhail Ivanov | 02d9c20 | 2017-05-17 17:12:50 +0400 | [diff] [blame] | 94 | dockerCleanupCommands = ['stop', 'rm -f'] |
Mikhail Ivanov | cd0a6f4 | 2017-05-17 12:55:42 +0400 | [diff] [blame] | 95 | for (int i = 0; i < dockerCleanupCommands.size(); i++) { |
| 96 | sh("docker-compose -f ${COMPOSE_PATH} -p ${uniqId} ${dockerCleanupCommands[i]} || true") |
| 97 | } |
| 98 | sh("docker network rm ${uniqId}_default || true") |
Mikhail Ivanov | cd50cce | 2017-06-02 17:57:26 +0400 | [diff] [blame] | 99 | common.infoMsg("Container with id ${containerName} was removed.") |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |