Ilya Kharin | 0c9449d | 2017-06-24 23:41:04 +0400 | [diff] [blame] | 1 | /** |
| 2 | * OSS - The DevOps Portal Testing Pipeline |
| 3 | * CREDENTIALS_ID - gerrit credentials id |
| 4 | **/ |
| 5 | |
| 6 | gerrit = new com.mirantis.mk.Gerrit() |
| 7 | common = new com.mirantis.mk.Common() |
| 8 | |
| 9 | def getProjectName(gerritRef, defaultGitRef) { |
| 10 | def refSpec |
| 11 | if (gerritRef) { |
| 12 | refSpec = gerritRef |
| 13 | } else { |
| 14 | refSpec = defaultGitRef |
| 15 | } |
| 16 | def refValue = refSpec.tokenize('/').takeRight(2).join('') |
| 17 | return "oss${BUILD_NUMBER}${refValue}" |
| 18 | } |
| 19 | |
| 20 | def 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}") |
| 29 | } |
| 30 | |
| 31 | def gerritRef |
| 32 | try { |
| 33 | gerritRef = GERRIT_REFSPEC |
| 34 | } catch (MissingPropertyException e) { |
| 35 | gerritRef = null |
| 36 | } |
| 37 | |
| 38 | def defaultGitRef, defaultGitUrl |
| 39 | try { |
| 40 | defaultGitRef = DEFAULT_GIT_REF |
| 41 | defaultGitUrl = DEFAULT_GIT_URL |
| 42 | } catch (MissingPropertyException e) { |
| 43 | defaultGitRef = null |
| 44 | defaultGitUrl = null |
| 45 | } |
| 46 | def checkouted = false |
| 47 | |
| 48 | node("vm") { |
| 49 | def composePath = 'docker/stack/docker-compose.yml' |
| 50 | def projectName |
| 51 | def jenkinsUser |
| 52 | |
| 53 | try { |
| 54 | stage('Checkout Source Code') { |
| 55 | if (gerritRef) { |
| 56 | // job is triggered by Gerrit |
| 57 | checkouted = gerrit.gerritPatchsetCheckout ([ |
| 58 | 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 | } |
| 67 | } |
| 68 | |
| 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.") |
| 75 | } |
| 76 | |
| 77 | def jenkinsUID = common.getJenkinsUid() |
| 78 | def jenkinsGID = common.getJenkinsGid() |
| 79 | |
| 80 | jenkinsUser = "${jenkinsUID}:${jenkinsGID}" |
| 81 | |
| 82 | 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') { |
Ilya Kharin | 25a77f1 | 2017-07-04 17:10:35 +0400 | [diff] [blame] | 102 | timeout(20) { |
Ilya Kharin | 0c9449d | 2017-06-24 23:41:04 +0400 | [diff] [blame] | 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 | } |
| 113 | } |
| 114 | } catch (err) { |
| 115 | currentBuild.result = 'FAILURE' |
| 116 | common.errorMsg("Build failed due to error: ${err}") |
| 117 | throw err |
| 118 | } finally { |
| 119 | common.sendNotification(currentBuild.result, "" ,["slack"]) |
| 120 | stage('Cleanup') { |
| 121 | wrap([$class: 'AnsiColorBuildWrapper']) { |
| 122 | sh("docker-compose -f ${composePath} -p ${projectName} down") |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |