|
| 1 | +#!/usr/bin/env groovy |
| 2 | + |
| 3 | +@Library('apm@current') _ |
| 4 | + |
| 5 | +def NODE_JS_VERSIONS = [8,10,12] |
| 6 | +def nodeJsVersion = NODE_JS_VERSIONS[randomNumber(min: 0, max:2)] |
| 7 | + |
| 8 | +pipeline { |
| 9 | + agent { |
| 10 | + label 'docker && immutable' |
| 11 | + } |
| 12 | + |
| 13 | + environment { |
| 14 | + REPO = 'elasticsearch-js' |
| 15 | + BASE_DIR = "src/github.com/elastic/${env.REPO}" |
| 16 | + NODE_JS_DEFAULT_VERSION = "${nodeJsVersion}" |
| 17 | + NODE_JS_VERSIONS = "${NODE_JS_VERSIONS.join(',')}" |
| 18 | + HOME = "${env.WORKSPACE}" |
| 19 | + npm_config_cache = 'npm-cache' |
| 20 | + } |
| 21 | + |
| 22 | + options { |
| 23 | + timeout(time: 1, unit: 'HOURS') |
| 24 | + buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '20', daysToKeepStr: '30')) |
| 25 | + timestamps() |
| 26 | + ansiColor('xterm') |
| 27 | + disableResume() |
| 28 | + durabilityHint('PERFORMANCE_OPTIMIZED') |
| 29 | + } |
| 30 | + |
| 31 | + triggers { |
| 32 | + issueCommentTrigger('(?i).*(?:jenkins\\W+)?run\\W+(?:the\\W+)?tests(?:\\W+please)?.*') |
| 33 | + // changeRequest() will return true in case of a commit or a pr |
| 34 | + // we will have a daily cron job only ofr branches that don't have an active pr |
| 35 | + cron(changeRequest() ? '' : '@daily') |
| 36 | + } |
| 37 | + |
| 38 | + stages { |
| 39 | + stage('Checkout') { |
| 40 | + options { skipDefaultCheckout() } |
| 41 | + steps { |
| 42 | + deleteDir() |
| 43 | + gitCheckout(basedir: "${BASE_DIR}", githubNotifyFirstTimeContributor: false) |
| 44 | + stash allowEmpty: true, name: 'source', useDefaultExcludes: false |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + stage('Install dependencies') { |
| 49 | + options { skipDefaultCheckout() } |
| 50 | + steps { |
| 51 | + deleteDir() |
| 52 | + unstash 'source' |
| 53 | + script { |
| 54 | + buildDockerImage(image: "node:${env.NODE_JS_DEFAULT_VERSION}-alpine").inside(){ |
| 55 | + dir("${BASE_DIR}"){ |
| 56 | + sh(label: 'System info', script: 'node --version; npm --version') |
| 57 | + sh(label: 'Install dependencies', script: 'npm install') |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + stash allowEmpty: true, name: 'source-dependencies', useDefaultExcludes: false |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + stage('License check') { |
| 66 | + options { skipDefaultCheckout() } |
| 67 | + steps { |
| 68 | + withGithubNotify(context: 'License check') { |
| 69 | + deleteDir() |
| 70 | + unstash 'source-dependencies' |
| 71 | + script { |
| 72 | + buildDockerImage(image: "node:${env.NODE_JS_DEFAULT_VERSION}-alpine").inside(){ |
| 73 | + dir("${BASE_DIR}"){ |
| 74 | + sh(label: 'Check production dependencies licenses', script: 'npm run license-checker') |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + stage('Linter') { |
| 83 | + options { skipDefaultCheckout() } |
| 84 | + steps { |
| 85 | + withGithubNotify(context: 'Linter') { |
| 86 | + deleteDir() |
| 87 | + unstash 'source-dependencies' |
| 88 | + script { |
| 89 | + buildDockerImage(image: "node:${env.NODE_JS_DEFAULT_VERSION}-alpine").inside(){ |
| 90 | + dir("${BASE_DIR}"){ |
| 91 | + sh(label: 'Lint code with standardjs', script: 'npm run lint') |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + stage('Unit test') { |
| 100 | + failFast true |
| 101 | + options { skipDefaultCheckout() } |
| 102 | + steps { |
| 103 | + withGithubNotify(context: 'Unit test') { |
| 104 | + script { |
| 105 | + def versions = env.NODE_JS_VERSIONS.split(',') |
| 106 | + def parallelTasks = [:] |
| 107 | + versions.each{ version -> |
| 108 | + parallelTasks["Node.js v${version}"] = buildUnitTest(version: version) |
| 109 | + } |
| 110 | + parallel(parallelTasks) |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + stage('Integration test') { |
| 117 | + failFast true |
| 118 | + options { skipDefaultCheckout() } |
| 119 | + parallel { |
| 120 | + stage('OSS') { |
| 121 | + agent { label 'docker && immutable' } |
| 122 | + options { skipDefaultCheckout() } |
| 123 | + environment { |
| 124 | + TEST_ES_SERVER = 'http://elasticsearch:9200' |
| 125 | + } |
| 126 | + steps { |
| 127 | + withGithubNotify(context: 'Integration test OSS') { |
| 128 | + deleteDir() |
| 129 | + unstash 'source-dependencies' |
| 130 | + dir("${BASE_DIR}"){ |
| 131 | + // Sometimes the docker registry fails and has random timeouts |
| 132 | + // this block will retry a doker image 3 times before to fail. |
| 133 | + retry(3) { |
| 134 | + sleep randomNumber(min: 5, max: 10) |
| 135 | + sh(label: 'Start Elasticsearch', script: './scripts/es-docker.sh --detach') |
| 136 | + } |
| 137 | + } |
| 138 | + script { |
| 139 | + buildDockerImage(fromDockerfile: true).inside('--network=elastic'){ |
| 140 | + dir("${BASE_DIR}"){ |
| 141 | + sh(label: 'Integration test', script: 'npm run test:integration | tee test-integration.tap') |
| 142 | + sh(label: 'Generating test reporting', script: './node_modules/.bin/tap-mocha-reporter xunit < test-integration.tap > junit-integration.xml') |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + sh(label: 'Stop Elasticsearch', script: 'docker kill $(docker ps -q)') |
| 147 | + junit(allowEmptyResults: true, keepLongStdio: true, testResults: "${BASE_DIR}/**/junit-*.xml") |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + stage('xPack') { |
| 153 | + agent { label 'docker && immutable' } |
| 154 | + options { skipDefaultCheckout() } |
| 155 | + environment { |
| 156 | + TEST_ES_SERVER = 'https://elastic:changeme@elasticsearch:9200' |
| 157 | + } |
| 158 | + steps { |
| 159 | + withGithubNotify(context: 'Integration test xPack') { |
| 160 | + deleteDir() |
| 161 | + unstash 'source-dependencies' |
| 162 | + dir("${BASE_DIR}"){ |
| 163 | + // Sometimes the docker registry fails and has random timeouts |
| 164 | + // this block will retry a doker image 3 times before to fail. |
| 165 | + retry(3) { |
| 166 | + sleep randomNumber(min: 5, max: 10) |
| 167 | + sh(label: 'Start Elasticsearch', script: './scripts/es-docker-platinum.sh --detach') |
| 168 | + } |
| 169 | + } |
| 170 | + script { |
| 171 | + buildDockerImage(fromDockerfile: true).inside('--network=elastic'){ |
| 172 | + dir("${BASE_DIR}"){ |
| 173 | + sh(label: 'Integration test', script: 'npm run test:integration | tee test-integration.tap') |
| 174 | + sh(label: 'Generating test reporting', script: './node_modules/.bin/tap-mocha-reporter xunit < test-integration.tap > junit-integration.xml') |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + sh(label: 'Stop Elasticsearch', script: 'docker kill $(docker ps -q)') |
| 179 | + junit(allowEmptyResults: true, keepLongStdio: true, testResults: "${BASE_DIR}/**/junit-*.xml") |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +// Sometimes the docker registry fails and has random timeouts |
| 189 | +// this function will retry a doker image 3 times before to fail. |
| 190 | +def buildDockerImage(args) { |
| 191 | + def image |
| 192 | + retry(3) { |
| 193 | + sleep randomNumber(min: 5, max: 10) |
| 194 | + if (args.fromDockerfile == true) { |
| 195 | + image = docker.build('nodejs-image', "--build-arg NODE_JS_VERSION=${env.NODE_JS_DEFAULT_VERSION} ${BASE_DIR}/.ci/docker") |
| 196 | + } else { |
| 197 | + image = docker.image(args.image) |
| 198 | + // make sure we have the latest available from Docker Hub |
| 199 | + image.pull() |
| 200 | + } |
| 201 | + } |
| 202 | + return image |
| 203 | +} |
| 204 | + |
| 205 | +def buildUnitTest(args) { |
| 206 | + return { |
| 207 | + node('docker && immutable') { |
| 208 | + deleteDir() |
| 209 | + unstash 'source' |
| 210 | + script { |
| 211 | + buildDockerImage(image: "node:${args.version}-alpine").inside(){ |
| 212 | + dir("${BASE_DIR}"){ |
| 213 | + sh(label: 'Install dependencies', script: 'npm install') |
| 214 | + sh(label: 'Run unit test', script: 'npm run test:unit | tee test-unit.tap') |
| 215 | + sh(label: 'Run behavior test', script: 'npm run test:behavior | tee test-behavior.tap') |
| 216 | + sh(label: 'Run types test', script: 'npm run test:types') |
| 217 | + sh(label: 'Generating test reporting', script: './node_modules/.bin/tap-mocha-reporter xunit < test-unit.tap > junit-unit.xml; ./node_modules/.bin/tap-mocha-reporter xunit < test-behavior.tap > junit-behavior.xml') |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + junit(allowEmptyResults: true, keepLongStdio: true, testResults: "${BASE_DIR}/**/junit-*.xml") |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments