blob: 36abbd7afd0e23be1c6544f77c7158a4194ed332 [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 Kharin9edd06f2017-06-21 12:12:41 +040012def executeCmd(user, 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}")
Ilya Kharin817d6d62017-06-20 19:43:54 +040016 wrap([$class: 'AnsiColorBuildWrapper']) {
Ilya Kharin9edd06f2017-06-21 12:12:41 +040017 sh("docker exec --user=${user} ${containerName} ${cmd}")
Ilya Kharin817d6d62017-06-20 19:43:54 +040018 }
Ilya Kharin0921c662017-03-30 23:08:18 +040019 common.successMsg("Successfully completed: ${cmd}")
20 }
21}
22
Mikhail Ivanov854f21f2017-04-14 17:06:31 +040023def gerritRef
24try {
25 gerritRef = GERRIT_REFSPEC
26} catch (MissingPropertyException e) {
27 gerritRef = null
28}
29
30def defaultGitRef, defaultGitUrl
31try {
32 defaultGitRef = DEFAULT_GIT_REF
33 defaultGitUrl = DEFAULT_GIT_URL
34} catch (MissingPropertyException e) {
35 defaultGitRef = null
36 defaultGitUrl = null
37}
38def checkouted = false
39
Mikhail Ivanov98411922017-06-01 16:27:10 +040040node("vm") {
Mikhail Ivanovcd50cce2017-06-02 17:57:26 +040041 def containerName
Mikhail Ivanovcd0a6f42017-05-17 12:55:42 +040042 def uniqId
Ilya Kharind2043ca2017-03-29 16:50:26 +040043 try {
Ilya Kharin0921c662017-03-30 23:08:18 +040044 stage('Checkout source code') {
Mikhail Ivanov854f21f2017-04-14 17:06:31 +040045 if (gerritRef) {
46 // job is triggered by Gerrit
Jakub Joseff59e0bb2017-04-17 11:35:50 +020047 checkouted = gerrit.gerritPatchsetCheckout ([
Mikhail Ivanov854f21f2017-04-14 17:06:31 +040048 credentialsId : CREDENTIALS_ID,
49 withWipeOut : true,
50 ])
51 } else if(defaultGitRef && defaultGitUrl) {
52 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
53 }
54 if(!checkouted){
55 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
56 }
Ilya Kharind2043ca2017-03-29 16:50:26 +040057 }
Mikhail Ivanovcd0a6f42017-05-17 12:55:42 +040058 stage('Start container') {
Ilya Kharin0921c662017-03-30 23:08:18 +040059 def workspace = common.getWorkspace()
Mikhail Ivanovcd0a6f42017-05-17 12:55:42 +040060 def timeStamp = new Date().format("HHmmss", TimeZone.getTimeZone('UTC'))
61 if (gerritRef) {
62 uniqId = gerritRef.tokenize('/').takeRight(2).join('') + timeStamp
63 } else {
64 uniqId = defaultGitRef.tokenize('/').takeRight(2).join('') + timeStamp
65 }
Ilya Kharin1fd1b732017-06-21 18:03:38 +040066 sh("docker-compose -f ${COMPOSE_PATH} -p ${uniqId} up -d")
Mikhail Ivanovcd50cce2017-06-02 17:57:26 +040067 containerName = "${uniqId}_devopsportal_1"
68 common.successMsg("Container with id ${containerName} started.")
Ilya Kharind2043ca2017-03-29 16:50:26 +040069 }
Ilya Kharin9edd06f2017-06-21 12:12:41 +040070
71 def jenkinsUID = common.getJenkinsUid()
72 def jenkinsGID = common.getJenkinsGid()
73 def jenkinsUser = "${jenkinsUID}:${jenkinsGID}"
74
75 executeCmd(jenkinsUser, containerName, "npm install")
76
Ilya Kharin0921c662017-03-30 23:08:18 +040077 def cmds = COMMANDS.tokenize('\n')
78 for (int i = 0; i < cmds.size(); i++) {
Ilya Kharinea0b6532017-06-05 12:41:10 +040079 timeout(5) {
Ilya Kharin9edd06f2017-06-21 12:12:41 +040080 executeCmd(jenkinsUser, containerName, cmds[i])
Ilya Kharinea0b6532017-06-05 12:41:10 +040081 }
Ilya Kharind2043ca2017-03-29 16:50:26 +040082 }
Ilya Kharin0921c662017-03-30 23:08:18 +040083 } catch (err) {
Ilya Kharind2043ca2017-03-29 16:50:26 +040084 currentBuild.result = 'FAILURE'
Ilya Kharin0921c662017-03-30 23:08:18 +040085 common.errorMsg("Build failed due to error: ${err}")
86 throw err
Ilya Kharind2043ca2017-03-29 16:50:26 +040087 } finally {
88 common.sendNotification(currentBuild.result, "" ,["slack"])
Ilya Kharin54987d92017-06-20 18:55:30 +040089 stage('Attach artifacts') {
90 if (containerName != null) {
Ilya Kharin54987d92017-06-20 18:55:30 +040091 archiveArtifacts(
Ilya Kharin9edd06f2017-06-21 12:12:41 +040092 artifacts: "test_output/screenshots/*.png",
Ilya Kharin54987d92017-06-20 18:55:30 +040093 )
94 }
95 }
Ilya Kharin0921c662017-03-30 23:08:18 +040096 stage('Cleanup') {
Mikhail Ivanovcd50cce2017-06-02 17:57:26 +040097 if (containerName != null) {
Mikhail Ivanov02d9c202017-05-17 17:12:50 +040098 dockerCleanupCommands = ['stop', 'rm -f']
Mikhail Ivanovcd0a6f42017-05-17 12:55:42 +040099 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")
Mikhail Ivanovcd50cce2017-06-02 17:57:26 +0400103 common.infoMsg("Container with id ${containerName} was removed.")
Ilya Kharind2043ca2017-03-29 16:50:26 +0400104 }
105 }
106 }
107}