blob: 9e033fdfc13741e16d7a17b22f97f8cd102d214c [file] [log] [blame]
Ilya Kharind2043ca2017-03-29 16:50:26 +04001/**
2* JS testing pipeline
3* CREDENTIALS_ID - gerrit credentials id
Mikhail Ivanovcd0a6f42017-05-17 12:55:42 +04004* COMPOSE_PATH - path to compose file in repository
Ilya Kharind2043ca2017-03-29 16:50:26 +04005* NODE_IMAGE - NodeJS with NPM Docker image name
6* COMMANDS - a list of command(s) to run
7**/
8
9gerrit = new com.mirantis.mk.Gerrit()
10common = new com.mirantis.mk.Common()
11
Ilya Kharin0921c662017-03-30 23:08:18 +040012def executeCmd(containerId, cmd) {
13 stage(cmd) {
14 assert containerId != null
15 common.infoMsg("Starting command: ${cmd}")
16 def output = sh(
17 script: "docker exec ${containerId} ${cmd}",
18 returnStdout: true,
19 )
20 common.infoMsg(output)
21 common.successMsg("Successfully completed: ${cmd}")
22 }
23}
24
Mikhail Ivanov854f21f2017-04-14 17:06:31 +040025def 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
Mikhail Ivanovcd0a6f42017-05-17 12:55:42 +040044 def uniqId
Ilya Kharind2043ca2017-03-29 16:50:26 +040045 try {
Ilya Kharin0921c662017-03-30 23:08:18 +040046 stage('Checkout source code') {
Mikhail Ivanov854f21f2017-04-14 17:06:31 +040047 if (gerritRef) {
48 // job is triggered by Gerrit
Jakub Joseff59e0bb2017-04-17 11:35:50 +020049 checkouted = gerrit.gerritPatchsetCheckout ([
Mikhail Ivanov854f21f2017-04-14 17:06:31 +040050 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 Kharind2043ca2017-03-29 16:50:26 +040059 }
Mikhail Ivanovcd0a6f42017-05-17 12:55:42 +040060 stage('Generate config file for devops portal') {
61 def builder = new groovy.json.JsonBuilder()
62 def config = builder.services {
63 elasticsearch {
64 endpoint 'http://elasticsearch:9200'
65 }
66 }
67 writeFile (
68 file: "${workspace}/test_config.json",
69 text: config.toString()
70 )
71 }
72 stage('Start container') {
Ilya Kharin0921c662017-03-30 23:08:18 +040073 def workspace = common.getWorkspace()
Mikhail Ivanovcd0a6f42017-05-17 12:55:42 +040074 def timeStamp = new Date().format("HHmmss", TimeZone.getTimeZone('UTC'))
75 if (gerritRef) {
76 uniqId = gerritRef.tokenize('/').takeRight(2).join('') + timeStamp
77 } else {
78 uniqId = defaultGitRef.tokenize('/').takeRight(2).join('') + timeStamp
79 }
80 sh("docker-compose -f ${COMPOSE_PATH} -p ${uniqId} up -d")
81 containerId = "${uniqId}_devopsportal_1"
Ilya Kharin0921c662017-03-30 23:08:18 +040082 common.successMsg("Container with id ${containerId} started.")
Ilya Kharin86a706e2017-03-31 18:18:08 +040083 sh("docker cp ${workspace}/. ${containerId}:/opt/workspace/")
Ilya Kharind2043ca2017-03-29 16:50:26 +040084 }
Ilya Kharin0921c662017-03-30 23:08:18 +040085 executeCmd(containerId, "npm install")
86 def cmds = COMMANDS.tokenize('\n')
87 for (int i = 0; i < cmds.size(); i++) {
88 executeCmd(containerId, cmds[i])
Ilya Kharind2043ca2017-03-29 16:50:26 +040089 }
Ilya Kharin0921c662017-03-30 23:08:18 +040090 } catch (err) {
Ilya Kharind2043ca2017-03-29 16:50:26 +040091 currentBuild.result = 'FAILURE'
Ilya Kharin0921c662017-03-30 23:08:18 +040092 common.errorMsg("Build failed due to error: ${err}")
93 throw err
Ilya Kharind2043ca2017-03-29 16:50:26 +040094 } finally {
95 common.sendNotification(currentBuild.result, "" ,["slack"])
Ilya Kharin0921c662017-03-30 23:08:18 +040096 stage('Cleanup') {
97 if (containerId != null) {
Mikhail Ivanovcd0a6f42017-05-17 12:55:42 +040098 dockerCleanupCommands = ['stop', 'rm']
99 for (int i = 0; i < dockerCleanupCommands.size(); i++) {
100 sh("docker-compose -f ${COMPOSE_PATH} -p ${uniqId} ${dockerCleanupCommands[i]} || true")
101 }
102 sh("docker network rm ${uniqId}_default || true")
103 sh("rm -f ${workspace}/test_config.json || true")
Ilya Kharin0921c662017-03-30 23:08:18 +0400104 common.infoMsg("Container with id ${containerId} was removed.")
Ilya Kharind2043ca2017-03-29 16:50:26 +0400105 }
106 }
107 }
108}