Created
April 26, 2017 14:36
-
-
Save jmaitrehenry/157a8a6efb971f2d6aebd913cad73c6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def buildId = env.BUILD_NUMBER | |
def maxRunner = 15 | |
def envName = "${env.JOB_NAME}-${buildId}".toLowerCase().replaceAll(~/[^-a-z0-9]/, '-') | |
def gitCommit = "" | |
def prepareDbAndRun = [:] | |
for (int i = 1; i <= maxRunner; i++) { | |
def runner = i | |
prepareDbAndRun["runner${runner}"] = { | |
node(envName) { | |
// do some job | |
} | |
} | |
} | |
timestamps { | |
properties([disableConcurrentBuilds(), pipelineTriggers([])]) | |
node('master') { | |
env.CI_ID = buildId | |
dir('tmp') { | |
deleteDir() | |
} | |
stage('Git clone and merge master') { | |
retry(3) { | |
checkout scm | |
gitCommit = sh returnStdout: true, script: "echo \$(git rev-parse HEAD)" | |
sh ''' | |
git checkout origin/master | |
git merge --ff -q origin/$BRANCH_NAME | |
''' | |
} | |
} | |
stage('Provision new instances') { | |
sh "docker run --rm -i --volumes-from gcloud-config -v `pwd`/config:/config google/cloud-sdk gcloud deployment-manager deployments create ${envName} --config /config/gce.jinja --properties targetSize:${maxRunner},buildId:${buildId},buildName:${envName}" | |
} | |
try { | |
stage('Prepare test environment') { | |
// do some stuff | |
stash name: "code", includes: 'code.tgz', useDefaultExcludes: false | |
} | |
stage('Run tests') { | |
parallel prepareDbAndRun | |
} | |
} finally { | |
stage('Deallocate jenkins agent') { | |
sh "docker run --rm -i --volumes-from gcloud-config -v `pwd`/config:/config google/cloud-sdk gcloud deployment-manager deployments delete -q --async ${envName}" | |
script { | |
for (aSlave in hudson.model.Hudson.getInstance().getSlaves()) { | |
if (aSlave.getLabelString() == envName) { | |
aSlave.toComputer().doDoDelete(); | |
println('Node ' + aSlave.getNodeName() + ' deleted'); | |
} | |
} | |
} | |
} | |
} | |
stage('Archive artefact and publish build status') { | |
for (int i = 1; i <= maxRunner; i++) { | |
unstash "runner${i}" | |
} | |
// Touch all rpsec file because xUnit is dumb: [xUnit] [ERROR] - Test reports were found but not all of them are new. Did all the tests run? | |
sh 'touch tmp/rspec*.xml' | |
step([ | |
$class: 'XUnitPublisher', | |
testTimeMargin: '3000', | |
thresholdMode: 1, | |
thresholds: [ | |
[$class: 'FailedThreshold', failureNewThreshold: '0', failureThreshold: '0', unstableNewThreshold: '0', unstableThreshold: '0'], | |
[$class: 'SkippedThreshold', failureNewThreshold: '', failureThreshold: '', unstableNewThreshold: '', unstableThreshold: ''] | |
], | |
tools: [[$class: 'JUnitType', deleteOutputFiles: true, failIfNotNew: true, pattern: 'tmp/*.xml', skipNoTestFiles: false, stopProcessingIfError: true]] | |
]) | |
// create a unique parallel runtime log | |
sh 'cat tmp/parallel_runtime_rspec[0-9]* > tmp/parallel_runtime_rspec.log' | |
archiveArtifacts 'tmp/*' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment