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