Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 1 | /** |
| 2 | * JS testing pipeline |
| 3 | * CREDENTIALS_ID - gerrit credentials id |
| 4 | * NODE_IMAGE - NodeJS with NPM Docker image name |
| 5 | * COMMANDS - a list of command(s) to run |
| 6 | **/ |
| 7 | |
| 8 | gerrit = new com.mirantis.mk.Gerrit() |
| 9 | common = new com.mirantis.mk.Common() |
| 10 | |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 11 | def executeCmd(containerId, cmd) { |
| 12 | stage(cmd) { |
| 13 | assert containerId != null |
| 14 | common.infoMsg("Starting command: ${cmd}") |
| 15 | def output = sh( |
| 16 | script: "docker exec ${containerId} ${cmd}", |
| 17 | returnStdout: true, |
| 18 | ) |
| 19 | common.infoMsg(output) |
| 20 | common.successMsg("Successfully completed: ${cmd}") |
| 21 | } |
| 22 | } |
| 23 | |
Mikhail Ivanov | 854f21f | 2017-04-14 17:06:31 +0400 | [diff] [blame] | 24 | |
| 25 | def gerritRef |
| 26 | try { |
| 27 | gerritRef = GERRIT_REFSPEC |
| 28 | } catch (MissingPropertyException e) { |
| 29 | gerritRef = null |
| 30 | } |
| 31 | |
| 32 | def defaultGitRef, defaultGitUrl |
| 33 | try { |
| 34 | defaultGitRef = DEFAULT_GIT_REF |
| 35 | defaultGitUrl = DEFAULT_GIT_URL |
| 36 | } catch (MissingPropertyException e) { |
| 37 | defaultGitRef = null |
| 38 | defaultGitUrl = null |
| 39 | } |
| 40 | def checkouted = false |
| 41 | |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 42 | node("docker") { |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 43 | def containerId |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 44 | try { |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 45 | stage('Checkout source code') { |
Mikhail Ivanov | 854f21f | 2017-04-14 17:06:31 +0400 | [diff] [blame] | 46 | if (gerritRef) { |
| 47 | // job is triggered by Gerrit |
Jakub Josef | f59e0bb | 2017-04-17 11:35:50 +0200 | [diff] [blame] | 48 | checkouted = gerrit.gerritPatchsetCheckout ([ |
Mikhail Ivanov | 854f21f | 2017-04-14 17:06:31 +0400 | [diff] [blame] | 49 | credentialsId : CREDENTIALS_ID, |
| 50 | withWipeOut : true, |
| 51 | ]) |
| 52 | } else if(defaultGitRef && defaultGitUrl) { |
| 53 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
| 54 | } |
| 55 | if(!checkouted){ |
| 56 | throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null") |
| 57 | } |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 58 | } |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 59 | stage('Start container') { |
| 60 | def workspace = common.getWorkspace() |
| 61 | containerId = sh( |
| 62 | script: "docker run -d ${NODE_IMAGE}", |
| 63 | returnStdout: true, |
| 64 | ).trim() |
| 65 | common.successMsg("Container with id ${containerId} started.") |
Ilya Kharin | 86a706e | 2017-03-31 18:18:08 +0400 | [diff] [blame] | 66 | sh("docker cp ${workspace}/. ${containerId}:/opt/workspace/") |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 67 | } |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 68 | executeCmd(containerId, "npm install") |
| 69 | def cmds = COMMANDS.tokenize('\n') |
| 70 | for (int i = 0; i < cmds.size(); i++) { |
| 71 | executeCmd(containerId, cmds[i]) |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 72 | } |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 73 | } catch (err) { |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 74 | currentBuild.result = 'FAILURE' |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 75 | common.errorMsg("Build failed due to error: ${err}") |
| 76 | throw err |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 77 | } finally { |
| 78 | common.sendNotification(currentBuild.result, "" ,["slack"]) |
Ilya Kharin | 0921c66 | 2017-03-30 23:08:18 +0400 | [diff] [blame] | 79 | stage('Cleanup') { |
| 80 | if (containerId != null) { |
| 81 | sh("docker stop -t 0 ${containerId}") |
| 82 | sh("docker rm ${containerId}") |
| 83 | common.infoMsg("Container with id ${containerId} was removed.") |
Ilya Kharin | d2043ca | 2017-03-29 16:50:26 +0400 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |