blob: b1024ccc0b2ed07431ff9e6680e4f364c57a6767 [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
Mikhail Ivanovcd50cce2017-06-02 17:57:26 +040012def executeCmd(containerName, cmd) {
Ilya Kharin0921c662017-03-30 23:08:18 +040013 stage(cmd) {
Mikhail Ivanovcd50cce2017-06-02 17:57:26 +040014 assert containerName != null
Ilya Kharin0921c662017-03-30 23:08:18 +040015 common.infoMsg("Starting command: ${cmd}")
16 def output = sh(
Mikhail Ivanovcd50cce2017-06-02 17:57:26 +040017 script: "docker exec ${containerName} ${cmd}",
Ilya Kharin0921c662017-03-30 23:08:18 +040018 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
Mikhail Ivanov98411922017-06-01 16:27:10 +040042node("vm") {
Mikhail Ivanovcd50cce2017-06-02 17:57:26 +040043 def containerName
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') {
Mikhail Ivanovcd0a6f42017-05-17 12:55:42 +040061 writeFile (
62 file: "${workspace}/test_config.json",
Mikhail Ivanovcd50cce2017-06-02 17:57:26 +040063 text: "${JSON_CONFIG}"
Mikhail Ivanovcd0a6f42017-05-17 12:55:42 +040064 )
65 }
66 stage('Start container') {
Ilya Kharin0921c662017-03-30 23:08:18 +040067 def workspace = common.getWorkspace()
Mikhail Ivanovcd0a6f42017-05-17 12:55:42 +040068 def timeStamp = new Date().format("HHmmss", TimeZone.getTimeZone('UTC'))
69 if (gerritRef) {
70 uniqId = gerritRef.tokenize('/').takeRight(2).join('') + timeStamp
71 } else {
72 uniqId = defaultGitRef.tokenize('/').takeRight(2).join('') + timeStamp
73 }
74 sh("docker-compose -f ${COMPOSE_PATH} -p ${uniqId} up -d")
Mikhail Ivanovcd50cce2017-06-02 17:57:26 +040075 containerName = "${uniqId}_devopsportal_1"
76 common.successMsg("Container with id ${containerName} started.")
77 sh("docker cp ${workspace}/. ${containerName}:/opt/workspace/")
Ilya Kharind2043ca2017-03-29 16:50:26 +040078 }
Mikhail Ivanovcd50cce2017-06-02 17:57:26 +040079 executeCmd(containerName, "npm install")
Ilya Kharin0921c662017-03-30 23:08:18 +040080 def cmds = COMMANDS.tokenize('\n')
81 for (int i = 0; i < cmds.size(); i++) {
Ilya Kharinea0b6532017-06-05 12:41:10 +040082 timeout(5) {
83 executeCmd(containerName, cmds[i])
84 }
Ilya Kharind2043ca2017-03-29 16:50:26 +040085 }
Ilya Kharin0921c662017-03-30 23:08:18 +040086 } catch (err) {
Ilya Kharind2043ca2017-03-29 16:50:26 +040087 currentBuild.result = 'FAILURE'
Ilya Kharin0921c662017-03-30 23:08:18 +040088 common.errorMsg("Build failed due to error: ${err}")
89 throw err
Ilya Kharind2043ca2017-03-29 16:50:26 +040090 } finally {
91 common.sendNotification(currentBuild.result, "" ,["slack"])
Ilya Kharin0921c662017-03-30 23:08:18 +040092 stage('Cleanup') {
Mikhail Ivanovcd50cce2017-06-02 17:57:26 +040093 if (containerName != null) {
Mikhail Ivanov02d9c202017-05-17 17:12:50 +040094 dockerCleanupCommands = ['stop', 'rm -f']
Mikhail Ivanovcd0a6f42017-05-17 12:55:42 +040095 for (int i = 0; i < dockerCleanupCommands.size(); i++) {
96 sh("docker-compose -f ${COMPOSE_PATH} -p ${uniqId} ${dockerCleanupCommands[i]} || true")
97 }
98 sh("docker network rm ${uniqId}_default || true")
99 sh("rm -f ${workspace}/test_config.json || true")
Mikhail Ivanovcd50cce2017-06-02 17:57:26 +0400100 common.infoMsg("Container with id ${containerName} was removed.")
Ilya Kharind2043ca2017-03-29 16:50:26 +0400101 }
102 }
103 }
104}