blob: d0365a450b70edf6a62d8b6f25493337d2d47651 [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
Mikhail Ivanov854f21f2017-04-14 17:06:31 +040024
25def gerritRef
26try {
27 gerritRef = GERRIT_REFSPEC
28} catch (MissingPropertyException e) {
29 gerritRef = null
30}
31
32def defaultGitRef, defaultGitUrl
33try {
34 defaultGitRef = DEFAULT_GIT_REF
35 defaultGitUrl = DEFAULT_GIT_URL
36} catch (MissingPropertyException e) {
37 defaultGitRef = null
38 defaultGitUrl = null
39}
40def checkouted = false
41
Ilya Kharind2043ca2017-03-29 16:50:26 +040042node("docker") {
Ilya Kharin0921c662017-03-30 23:08:18 +040043 def containerId
Ilya Kharind2043ca2017-03-29 16:50:26 +040044 try {
Ilya Kharin0921c662017-03-30 23:08:18 +040045 stage('Checkout source code') {
Mikhail Ivanov854f21f2017-04-14 17:06:31 +040046 if (gerritRef) {
47 // job is triggered by Gerrit
Jakub Joseff59e0bb2017-04-17 11:35:50 +020048 checkouted = gerrit.gerritPatchsetCheckout ([
Mikhail Ivanov854f21f2017-04-14 17:06:31 +040049 credentialsId : CREDENTIALS_ID,
50 withWipeOut : true,
51 ])
52 } else if(defaultGitRef && defaultGitUrl) {
53 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
54 }
55 if(!checkouted){
56 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
57 }
Ilya Kharind2043ca2017-03-29 16:50:26 +040058 }
Ilya Kharin0921c662017-03-30 23:08:18 +040059 stage('Start container') {
60 def workspace = common.getWorkspace()
61 containerId = sh(
62 script: "docker run -d ${NODE_IMAGE}",
63 returnStdout: true,
64 ).trim()
65 common.successMsg("Container with id ${containerId} started.")
Ilya Kharin86a706e2017-03-31 18:18:08 +040066 sh("docker cp ${workspace}/. ${containerId}:/opt/workspace/")
Ilya Kharind2043ca2017-03-29 16:50:26 +040067 }
Ilya Kharin0921c662017-03-30 23:08:18 +040068 executeCmd(containerId, "npm install")
69 def cmds = COMMANDS.tokenize('\n')
70 for (int i = 0; i < cmds.size(); i++) {
71 executeCmd(containerId, cmds[i])
Ilya Kharind2043ca2017-03-29 16:50:26 +040072 }
Ilya Kharin0921c662017-03-30 23:08:18 +040073 } catch (err) {
Ilya Kharind2043ca2017-03-29 16:50:26 +040074 currentBuild.result = 'FAILURE'
Ilya Kharin0921c662017-03-30 23:08:18 +040075 common.errorMsg("Build failed due to error: ${err}")
76 throw err
Ilya Kharind2043ca2017-03-29 16:50:26 +040077 } finally {
78 common.sendNotification(currentBuild.result, "" ,["slack"])
Ilya Kharin0921c662017-03-30 23:08:18 +040079 stage('Cleanup') {
80 if (containerId != null) {
81 sh("docker stop -t 0 ${containerId}")
82 sh("docker rm ${containerId}")
83 common.infoMsg("Container with id ${containerId} was removed.")
Ilya Kharind2043ca2017-03-29 16:50:26 +040084 }
85 }
86 }
87}