blob: 30ea6420798c0e37553683c7a552f4ec3580f2f1 [file] [log] [blame]
Ilya Kharind2043ca2017-03-29 16:50:26 +04001/**
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
8gerrit = new com.mirantis.mk.Gerrit()
9common = new com.mirantis.mk.Common()
10
Ilya Kharin0921c662017-03-30 23:08:18 +040011def 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 Kharind2043ca2017-03-29 16:50:26 +040024node("docker") {
Ilya Kharin0921c662017-03-30 23:08:18 +040025 def containerId
Ilya Kharind2043ca2017-03-29 16:50:26 +040026 try {
Ilya Kharin0921c662017-03-30 23:08:18 +040027 stage('Checkout source code') {
Ilya Kharind2043ca2017-03-29 16:50:26 +040028 gerrit.gerritPatchsetCheckout ([
29 credentialsId : CREDENTIALS_ID,
30 withWipeOut : true,
31 ])
32 }
Ilya Kharin0921c662017-03-30 23:08:18 +040033 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 Kharin86a706e2017-03-31 18:18:08 +040040 sh("docker cp ${workspace}/. ${containerId}:/opt/workspace/")
Ilya Kharind2043ca2017-03-29 16:50:26 +040041 }
Ilya Kharin0921c662017-03-30 23:08:18 +040042 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 Kharind2043ca2017-03-29 16:50:26 +040046 }
Ilya Kharin0921c662017-03-30 23:08:18 +040047 } catch (err) {
Ilya Kharind2043ca2017-03-29 16:50:26 +040048 currentBuild.result = 'FAILURE'
Ilya Kharin0921c662017-03-30 23:08:18 +040049 common.errorMsg("Build failed due to error: ${err}")
50 throw err
Ilya Kharind2043ca2017-03-29 16:50:26 +040051 } finally {
52 common.sendNotification(currentBuild.result, "" ,["slack"])
Ilya Kharin0921c662017-03-30 23:08:18 +040053 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 Kharind2043ca2017-03-29 16:50:26 +040058 }
59 }
60 }
61}