blob: a7207451324ddbba3ba625dd7340ff7957b77aa4 [file] [log] [blame]
Ilya Kharind2043ca2017-03-29 16:50:26 +04001/**
Ilya Kharin0c9449d2017-06-24 23:41:04 +04002* OSS - The DevOps Portal Testing Pipeline
Ilya Kharind2043ca2017-03-29 16:50:26 +04003* CREDENTIALS_ID - gerrit credentials id
Ilya Kharind2043ca2017-03-29 16:50:26 +04004**/
5
6gerrit = new com.mirantis.mk.Gerrit()
7common = new com.mirantis.mk.Common()
8
Ilya Kharin0c9449d2017-06-24 23:41:04 +04009def getProjectName(gerritRef, defaultGitRef) {
10 def refSpec
11 if (gerritRef) {
12 refSpec = gerritRef
13 } else {
14 refSpec = defaultGitRef
Ilya Kharin0921c662017-03-30 23:08:18 +040015 }
Ilya Kharin0c9449d2017-06-24 23:41:04 +040016 def refValue = refSpec.tokenize('/').takeRight(2).join('')
17 return "oss${BUILD_NUMBER}${refValue}"
18}
19
20def executeCmd(user, project, cmd) {
21 common.infoMsg("Starting command: ${cmd}")
22 wrap([$class: 'AnsiColorBuildWrapper']) {
23 // Docker sets HOME=/ ignoring that it have to be HOME=/opt/workspace,
24 // as `docker-compose exec` does not support to pass environment
25 // variables, then `docker exec` is used.
26 sh("docker exec --user=${user} --env=HOME=/opt/workspace ${project}_devopsportal_1 ${cmd}")
27 }
28 common.successMsg("Successfully completed: ${cmd}")
Ilya Kharin0921c662017-03-30 23:08:18 +040029}
30
Mikhail Ivanov854f21f2017-04-14 17:06:31 +040031def gerritRef
32try {
33 gerritRef = GERRIT_REFSPEC
34} catch (MissingPropertyException e) {
35 gerritRef = null
36}
37
38def defaultGitRef, defaultGitUrl
39try {
40 defaultGitRef = DEFAULT_GIT_REF
41 defaultGitUrl = DEFAULT_GIT_URL
42} catch (MissingPropertyException e) {
43 defaultGitRef = null
44 defaultGitUrl = null
45}
46def checkouted = false
47
Mikhail Ivanov98411922017-06-01 16:27:10 +040048node("vm") {
Ilya Kharin0c9449d2017-06-24 23:41:04 +040049 def composePath = 'docker/stack/docker-compose.yml'
50 def projectName
51 def jenkinsUser
52
Ilya Kharind2043ca2017-03-29 16:50:26 +040053 try {
Ilya Kharin0c9449d2017-06-24 23:41:04 +040054 stage('Checkout Source Code') {
Mikhail Ivanov854f21f2017-04-14 17:06:31 +040055 if (gerritRef) {
56 // job is triggered by Gerrit
Jakub Joseff59e0bb2017-04-17 11:35:50 +020057 checkouted = gerrit.gerritPatchsetCheckout ([
Mikhail Ivanov854f21f2017-04-14 17:06:31 +040058 credentialsId : CREDENTIALS_ID,
59 withWipeOut : true,
60 ])
61 } else if(defaultGitRef && defaultGitUrl) {
62 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
63 }
64 if(!checkouted){
65 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
66 }
Ilya Kharind2043ca2017-03-29 16:50:26 +040067 }
Ilya Kharin0c9449d2017-06-24 23:41:04 +040068
69 projectName = getProjectName(gerritRef, defaultGitRef)
70
71 stage('Setup Up Stack') {
72 sh("docker-compose --file ${composePath} --project-name=${projectName} pull")
73 sh("docker-compose --file ${composePath} --project-name=${projectName} up -d --force-recreate")
74 common.successMsg("Stack with the ${projectName} is started.")
Ilya Kharind2043ca2017-03-29 16:50:26 +040075 }
Ilya Kharin9edd06f2017-06-21 12:12:41 +040076
77 def jenkinsUID = common.getJenkinsUid()
78 def jenkinsGID = common.getJenkinsGid()
Ilya Kharin9edd06f2017-06-21 12:12:41 +040079
Ilya Kharin0c9449d2017-06-24 23:41:04 +040080 jenkinsUser = "${jenkinsUID}:${jenkinsGID}"
Ilya Kharin9edd06f2017-06-21 12:12:41 +040081
Ilya Kharin0c9449d2017-06-24 23:41:04 +040082 stage('Print Environment Information') {
83 sh("docker-compose version")
84 sh("docker version")
85 executeCmd(jenkinsUser, projectName, "npm config get")
86 executeCmd(jenkinsUser, projectName, "env")
87 executeCmd(jenkinsUser, projectName, "ls -lan")
88 }
89
90 stage('Install Dependencies') {
91 executeCmd(jenkinsUser, projectName, "npm install")
92 }
93 stage('Run Linter Tests') {
94 executeCmd(jenkinsUser, projectName, "npm run lint")
95 }
96 stage('Run Unit Tests') {
97 timeout(4) {
98 executeCmd(jenkinsUser, projectName, "npm run test:unit")
99 }
100 }
101 stage('Run Function Tests') {
102 timeout(8) {
103 try {
104 executeCmd(jenkinsUser, projectName, "npm run test:functional")
105 } catch (err) {
106 archiveArtifacts(
107 artifacts: "test_output/**/*.png",
108 allowEmptyArchive: true,
109 )
110 throw err
111 }
112 }
Ilya Kharind2043ca2017-03-29 16:50:26 +0400113 }
Ilya Kharin0921c662017-03-30 23:08:18 +0400114 } catch (err) {
Ilya Kharind2043ca2017-03-29 16:50:26 +0400115 currentBuild.result = 'FAILURE'
Ilya Kharin0921c662017-03-30 23:08:18 +0400116 common.errorMsg("Build failed due to error: ${err}")
117 throw err
Ilya Kharind2043ca2017-03-29 16:50:26 +0400118 } finally {
119 common.sendNotification(currentBuild.result, "" ,["slack"])
Ilya Kharin0921c662017-03-30 23:08:18 +0400120 stage('Cleanup') {
Ilya Kharin0c9449d2017-06-24 23:41:04 +0400121 wrap([$class: 'AnsiColorBuildWrapper']) {
122 sh("docker-compose -f ${composePath} -p ${projectName} down")
Ilya Kharind2043ca2017-03-29 16:50:26 +0400123 }
124 }
125 }
126}
Ilya Kharin0c9449d2017-06-24 23:41:04 +0400127