Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 1 | /** |
| 2 | * JS testing pipeline |
| 3 | * CREDENTIALS_ID - gerrit credentials id |
| 4 | * NODE_IMAGE - NodeJS with NPM Docker image name |
| 5 | * COMMANDS - a list of command(s) to run |
| 6 | **/ |
| 7 | |
| 8 | gerrit = new com.mirantis.mk.Gerrit() |
| 9 | common = new com.mirantis.mk.Common() |
| 10 | |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 11 | def executeCmd(containerId, cmd) { |
| 12 | stage(cmd) { |
| 13 | assert containerId != null |
| 14 | common.infoMsg("Starting command: ${cmd}") |
| 15 | def output = sh( |
| 16 | script: "docker exec ${containerId} ${cmd}", |
| 17 | returnStdout: true, |
| 18 | ) |
| 19 | common.infoMsg(output) |
| 20 | common.successMsg("Successfully completed: ${cmd}") |
| 21 | } |
| 22 | } |
| 23 | |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 24 | node("docker") { |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 25 | def containerId |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 26 | try { |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 27 | stage('Checkout source code') { |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 28 | gerrit.gerritPatchsetCheckout ([ |
| 29 | credentialsId : CREDENTIALS_ID, |
| 30 | withWipeOut : true, |
| 31 | ]) |
| 32 | } |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 33 | stage('Start container') { |
| 34 | def workspace = common.getWorkspace() |
| 35 | containerId = sh( |
| 36 | script: "docker run -d ${NODE_IMAGE}", |
| 37 | returnStdout: true, |
| 38 | ).trim() |
| 39 | common.successMsg("Container with id ${containerId} started.") |
Ilya Kharin | 86a706e | 2017-03-31 18:18:08 +0400 | [diff] [blame] | 40 | sh("docker cp ${workspace}/. ${containerId}:/opt/workspace/") |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 41 | } |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 42 | executeCmd(containerId, "npm install") |
| 43 | def cmds = COMMANDS.tokenize('\n') |
| 44 | for (int i = 0; i < cmds.size(); i++) { |
| 45 | executeCmd(containerId, cmds[i]) |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 46 | } |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 47 | } catch (err) { |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 48 | currentBuild.result = 'FAILURE' |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 49 | common.errorMsg("Build failed due to error: ${err}") |
| 50 | throw err |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 51 | } finally { |
| 52 | common.sendNotification(currentBuild.result, "" ,["slack"]) |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 53 | stage('Cleanup') { |
| 54 | if (containerId != null) { |
| 55 | sh("docker stop -t 0 ${containerId}") |
| 56 | sh("docker rm ${containerId}") |
| 57 | common.infoMsg("Container with id ${containerId} was removed.") |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |