github.com/dddengyunjie/fabric-ca@v0.0.0-20190606043049-92df60ae2f0f/Jenkinsfile (about) 1 #!groovy 2 3 // Copyright IBM Corp All Rights Reserved 4 // 5 // SPDX-License-Identifier: Apache-2.0 6 // 7 8 // Jenkinfile will get triggered on verify and merge jobs and run basicChecks, docsBuild as a pre-tests 9 // and call unitTests, fvtTests to run on parallel build nodes. 10 // along with above mentioned tests, merge job also triggers e2e tests (fabcar, e2e_sdk_node & e2e_sdk_java) 11 12 @Library("fabric-ci-lib") _ 13 // global shared library from ci-management repository 14 // https://github.com/hyperledger/ci-management/tree/master/vars (Global Shared scripts) 15 timestamps { // set the timestamps on the jenkins console 16 timeout(120) { // Build timeout set to 120 mins 17 node ('hyp-x') { // trigger jobs on x86_64 builds nodes 18 def DOC_CHANGE 19 def CODE_CHANGE 20 def failure_stage = "none" 21 env.MARCH = sh(returnStdout: true, script: "uname -m | sed 's/x86_64/amd64/g'").trim() 22 buildStages() // call buildStages 23 } // end node block 24 } // end timeout block 25 } // end timestamps block 26 27 def buildStages() { 28 try { 29 def nodeHome = tool 'nodejs-8.14.0' 30 def ROOTDIR = pwd() 31 stage('Clean Environment') { 32 // delete working directory 33 deleteDir() 34 // Clean build environment before start the build 35 fabBuildLibrary.cleanupEnv() 36 // Display jenkins environment details 37 fabBuildLibrary.envOutput() 38 } 39 40 stage('Checkout SCM') { 41 // Clone changes from gerrit 42 fabBuildLibrary.cloneRefSpec('fabric-ca') 43 dir("$ROOTDIR/$BASE_DIR") { 44 DOC_CHANGE = sh(returnStdout: true, script: "git diff-tree --no-commit-id --name-only -r HEAD | egrep '.md\$|.rst\$|.txt\$|conf.py\$|.png\$|.pptx\$|.css\$|.html\$|.ini\$' | wc -l").trim() 45 println DOC_CHANGE 46 CODE_CHANGE = sh(returnStdout: true, script: "git diff-tree --no-commit-id --name-only -r HEAD | egrep -v '.md\$|.rst\$|.txt\$|conf.py\$|.png\$|.pptx\$|.css\$|.html\$|.ini\$' | wc -l").trim() 47 println CODE_CHANGE 48 } 49 // Load properties from ci.properties file 50 props = fabBuildLibrary.loadProperties() 51 // Set PATH 52 env.GOROOT = "/opt/go/go" + props["GO_VER"] + ".linux." + "$MARCH" 53 env.GOPATH = "$WORKSPACE/gopath" 54 env.PATH = "$GOROOT/bin:$GOPATH/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:${nodeHome}/bin:$PATH" 55 } 56 57 if (DOC_CHANGE > '0' && CODE_CHANGE == '0') { 58 sh "echo -e \033[1m ONLY DOC BUILD\033[0m" 59 docsBuild() 60 } else if (DOC_CHANGE > '0' && CODE_CHANGE > '0') { 61 sh "echo -e \033[1m CODE AND DOC BUILD\033[0m" 62 basicChecks() 63 docsBuild() 64 runTests() 65 } else { 66 sh "echo -e \033[1m CODE BUILD\033[0m" 67 basicChecks() // basic checks 68 sh "echo -e \033[1m CODE TESTS\033[0m" 69 runTests() // e2e on merge and unit, fvt tests on parallel 70 } 71 } finally { // post build actions 72 // Don't fail the build if coverage report is not generated 73 step([$class: 'CoberturaPublisher', autoUpdateHealth: false, autoUpdateStability: false, 74 coberturaReportFile: '**/coverage.xml', failUnhealthy: false, failUnstable: false, 75 failNoReports: false, maxNumberOfBuilds: 0, sourceEncoding: 'ASCII', zoomCoverageChart: false]) 76 // Don't fail the build if doc output is missing 77 publishHTML([allowMissing: true, 78 alwaysLinkToLastBuild: true, 79 keepAll: true, 80 reportDir: 'html', 81 reportFiles: 'index.html', 82 reportName: 'Docs Output' 83 ]) 84 // Don't fail the build if there is no log file 85 archiveArtifacts allowEmptyArchive: true, artifacts: '**/*.log' 86 // Send notifications only for merge failures 87 if (env.JOB_TYPE == "merge") { 88 if (currentBuild.result == 'FAILURE') { 89 // Send notification to rocketChat channel 90 // Send merge build failure email notifications to the submitter 91 sendNotifications(currentBuild.result, props["CHANNEL_NAME"]) 92 } 93 } 94 // Delete all containers 95 fabBuildLibrary.deleteContainers() 96 // Delete unused docker images (none,dev,test-vp etc..) 97 fabBuildLibrary.deleteUnusedImages() 98 // Delete workspace when build is done 99 cleanWs notFailBuild: true 100 } // end finally block 101 } // end build stages 102 103 def docsBuild () { 104 def ROOTDIR = pwd() 105 stage("Docs Build") { 106 wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'xterm']) { 107 try { 108 dir("$ROOTDIR/$BASE_DIR") { 109 sh ''' set +x -ue 110 echo "-------> tox VERSION" 111 tox --version 112 pip freeze 113 tox -edocs 114 cp -r docs/_build/html/ $WORKSPACE 115 ''' 116 } 117 } catch (err) { 118 failure_stage = "Docs Build" 119 currentBuild.result = 'FAILURE' 120 throw err 121 } 122 } 123 } 124 } 125 126 def basicChecks() { 127 def ROOTDIR = pwd() 128 stage("Basic Checks") { 129 wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'xterm']) { 130 try { 131 dir("$ROOTDIR/$BASE_DIR") { 132 // runs all check conditions (license, format, imports, lint and vet) 133 sh 'make checks' 134 } 135 } catch (err) { 136 failure_stage = "basicChecks" 137 currentBuild.result = 'FAILURE' 138 throw err 139 } 140 } 141 } 142 } 143 144 def fabCar() { 145 def ROOTDIR = pwd() 146 stage("Fab Car Tests") { 147 withEnv(["BASE_FOLDER=${WORKSPACE}/gopath/src/github.com/hyperledger"]) { 148 wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'xterm']) { 149 try { 150 // Clone fabric-samples repository 151 fabBuildLibrary.cloneScm('fabric-samples', '$GERRIT_BRANCH') 152 sh 'echo npm version \$(npm -v)' 153 sh 'echo node version \$(node -v)' 154 // Delete all containers 155 fabBuildLibrary.deleteContainers() 156 // Delete unused docker images (none,dev,test-vp etc..) 157 fabBuildLibrary.deleteUnusedImages() 158 dir("$ROOTDIR/gopath/src/github.com/hyperledger/fabric-samples/scripts/Jenkins_Scripts") { 159 sh './fabcar.sh' 160 } 161 } catch (err) { 162 failure_stage = "fabCar" 163 currentBuild.result = 'FAILURE' 164 throw err 165 } 166 } 167 } 168 } 169 } 170 171 def e2e_sdk_node() { 172 def ROOTDIR = pwd() 173 stage("e2e_sdk_node") { 174 wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'xterm']) { 175 try { 176 // Clone fabric-sdk-node repository 177 fabBuildLibrary.cloneScm('fabric-sdk-node', '$GERRIT_BRANCH') 178 sh 'echo npm version \$(npm -v)' 179 sh 'echo node version \$(node -v)' 180 // Delete all containers 181 fabBuildLibrary.deleteContainers() 182 // Delete unused docker images (none,dev,test-vp etc..) 183 fabBuildLibrary.deleteUnusedImages() 184 dir("$ROOTDIR/gopath/src/github.com/hyperledger/fabric-sdk-node") { 185 sh '''set +x -ue 186 npm install 187 npm install -g gulp 188 echo " ==== Run gulp test ==== " 189 gulp test 190 ''' 191 } 192 } catch (err) { 193 failure_stage = "e2e_sdk_node" 194 currentBuild.result = 'FAILURE' 195 throw err 196 } 197 } 198 } 199 } 200 201 def e2e_sdk_java() { 202 def ROOTDIR = pwd() 203 stage("e2e_sdk_java") { 204 wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'xterm']) { 205 try { 206 // Clone fabric-sdk-java repository 207 fabBuildLibrary.cloneScm('fabric-sdk-java', '$GERRIT_BRANCH') 208 // Delete all containers 209 fabBuildLibrary.deleteContainers() 210 // Delete unused docker images (none,dev,test-vp etc..) 211 fabBuildLibrary.deleteUnusedImages() 212 dir("$ROOTDIR/gopath/src/github.com/hyperledger/fabric-sdk-java") { 213 sh '''set +x -ue 214 export WD=$WORKSPACE/gopath/src/github.com/hyperledger/fabric-sdk-java 215 export GOPATH=$WD/src/test/fixture 216 cd $WD/src/test 217 chmod +x cirun.sh 218 ./cirun.sh 219 ''' 220 } 221 } catch (err) { 222 failure_stage = "e2e_sdk_java" 223 currentBuild.result = 'FAILURE' 224 throw err 225 } 226 } 227 } 228 } 229 230 def runTests() { 231 def ROOTDIR = pwd() 232 stage ("Tests") { 233 parallel ( 234 "e2e-Tests" : { 235 // Run e2e tests only on Merge job 236 if (env.JOB_TYPE == "merge") { 237 stage("e2e-Tests") { 238 wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'xterm']) { 239 try { 240 println " ==== Build fabric-ca Images ==== " 241 // Build fabri-ca docker images and binaries 242 fabBuildLibrary.fabBuildImages('fabric-ca', 'dist docker') 243 println " ==== Clone fabric repository ==== " 244 // Clone fabric repository 245 fabBuildLibrary.cloneScm('fabric', '$GERRIT_BRANCH') 246 println " ==== Build fabric Images and Binaries ==== " 247 // Build fabric images and binaries 248 fabBuildLibrary.fabBuildImages('fabric', 'dist docker') 249 println " ==== Pull Thirdparty Images ==== " 250 // Pull thirdparty images from DockerHub 251 fabBuildLibrary.pullThirdPartyImages(props["FAB_BASEIMAGE_VERSION"], props["FAB_THIRDPARTY_IMAGES_LIST"]) 252 println " ==== Pull sdk images from Nexus3 ==== " 253 // Pull latest stable images from nexus3 254 fabBuildLibrary.pullDockerImages(props["FAB_BASE_VERSION"], props["FAB_IMAGES_LIST"]) 255 println " ==== Run fabcar Tests ==== " 256 // Test fabcar on fabric-samples 257 fabCar() 258 println " ==== Run sdk-node Tests ==== " 259 // Test e2e tests on sdk-node 260 e2e_sdk_node() 261 println " ==== Run sdk-java Tests ==== " 262 // Test e2e tests on sdk-java 263 e2e_sdk_java() 264 265 } catch (err) { 266 failure_stage = "e2e-Tests" 267 currentBuild.result = 'FAILURE' 268 throw err 269 } 270 } 271 } 272 } 273 }, 274 275 "Unit Tests" : { 276 node('hyp-x') { 277 stage("UnitTests") { 278 wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'xterm']) { 279 try { 280 // delete working directory 281 deleteDir() 282 // Clean build environment before start the build 283 fabBuildLibrary.cleanupEnv() 284 // Clone repository 285 fabBuildLibrary.cloneRefSpec('fabric-ca') 286 dir("$ROOTDIR/$BASE_DIR") { 287 println " ==== RUN UNIT TESTS ====" 288 // Performs checks first and runs the go-test based unit tests 289 sh 'make unit-test int-tests docs' 290 // Stash the coverage report 291 stash name: "coverageReport", includes: "**/coverage.xml" 292 } 293 } 294 catch (err) { 295 failure_stage = "UnitTests" 296 currentBuild.result = 'FAILURE' 297 throw err 298 } 299 } 300 } 301 } 302 }, 303 "FVT Tests" : { 304 node('hyp-x') { 305 stage("FVT Tests") { 306 wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'xterm']) { 307 try { 308 // delete working directory 309 deleteDir() 310 // Clean build environment before start the build 311 fabBuildLibrary.cleanupEnv() 312 // Clone repository 313 fabBuildLibrary.cloneRefSpec('fabric-ca') 314 dir("$ROOTDIR/$BASE_DIR") { 315 if(env.GERRIT_BRANCH == "master") { 316 println " ==== RUN FVT TESTS ==== " 317 sh 'make fvt-tests' 318 } else { 319 println " ==== RUN FVT TESTS ====" 320 sh 'make docker-clean docker-fvt' 321 sh 'docker run -v $PWD:/opt/gopath/src/github.com/hyperledger/fabric-ca hyperledger/fabric-ca-fvt' 322 } 323 } 324 } catch (err) { 325 failure_stage = "FVT Tests" 326 currentBuild.result = 'FAILURE' 327 throw err 328 } 329 } 330 } 331 } 332 }, 333 failFast: true ) // Stop the build flow if one job fails 334 stage("Unstash") { 335 if (DOC_CHANGE > '0' && CODE_CHANGE == '0') { 336 // unstash not required for doc only builds 337 println "Unstash not required" 338 } else { 339 try { 340 dir("$ROOTDIR") { 341 println "Unstash stashed files" 342 // unstash coverageReport on main job 343 unstash 'coverageReport' 344 } 345 } 346 catch (err) { 347 failure_stage = "unstash" 348 currentBuild.result = 'FAILURE' 349 throw err 350 } 351 } 352 } 353 } // stage parallel 354 } // runTests