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 | |
| 11 | node("docker") { |
| 12 | def containerID |
| 13 | try { |
| 14 | stage ('Checkout source code') { |
| 15 | gerrit.gerritPatchsetCheckout ([ |
| 16 | credentialsId : CREDENTIALS_ID, |
| 17 | withWipeOut : true, |
| 18 | ]) |
| 19 | } |
| 20 | stage ('Start container') { |
| 21 | def workspace = common.getWorkspace() |
| 22 | containerID = sh( |
| 23 | script: "docker run -d -v ${workspace}:/opt/workspace:rw ${NODE_IMAGE}", |
| 24 | returnStdout: true, |
| 25 | ).trim() |
| 26 | } |
| 27 | stage ('Execute commands') { |
| 28 | assert containerID != null |
| 29 | def cmds = COMMANDS.tokenize('\n') |
| 30 | for (int i = 0; i < cmds.size(), i++) { |
| 31 | def cmd = cmds[i] |
| 32 | def output = sh( |
| 33 | script: "docker exec ${containerID} ${cmd}", |
| 34 | returnStdout: true, |
| 35 | ).trim() |
| 36 | common.infoMsg(output) |
| 37 | } |
| 38 | } |
| 39 | } catch (Throwable e) { |
| 40 | currentBuild.result = 'FAILURE' |
| 41 | common.errorMsg("Build failed due to some commands failed.") |
| 42 | throw e |
| 43 | } finally { |
| 44 | common.sendNotification(currentBuild.result, "" ,["slack"]) |
| 45 | stage ('Remove container') { |
| 46 | if (containerID != null) { |
| 47 | sh "docker stop -t 0 ${containerID}" |
| 48 | sh "docker rm ${containerID}" |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |