-
-
Save peschee/65b1b5e8f1c2b4a52bab5849a010b169 to your computer and use it in GitHub Desktop.
Sample advanced Jenkins pipeline
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
/** | |
* This file provides common stuff to be used in the pipelines. | |
* It is important to load it after repo checkout is done: see https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md#triggering-manual-loading | |
* | |
*/ | |
/** | |
* Dumps some info about the environment. | |
* @return | |
*/ | |
def dumpEnv() { | |
// see http://stackoverflow.com/questions/35873902/accessing-scm-git-variables-on-a-jenkins-pipeline-job | |
gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim() | |
echo "Path is ${env.PATH}" | |
echo "Current dir is ${pwd()}" | |
echo "Build tag is ${env.BUILD_TAG}, Git commit is ${gitCommit}" | |
sh "node --version" | |
sh "npm --version" | |
sh "gulp --version" | |
sh "bower --version" | |
sh "java -version" | |
sh "mvn --version" | |
sh "ansible --version" | |
} | |
def success() { | |
currentBuild.result = "SUCCESS" | |
} | |
def handleError(err) { | |
echo "Caught: ${err}" | |
currentBuild.result = "FAILURE" | |
throw any //rethrow exception to prevent the build from proceeding | |
} | |
/** | |
* Sends notifications if necessary. | |
* @return | |
*/ | |
def sendNotifications() { | |
step([$class: "Mailer", notifyEveryUnstableBuild: true, recipients: emailextrecipients([[$class: "CulpritsRecipientProvider"], [$class: "RequesterRecipientProvider"]])]) | |
} | |
def dockerCleanup(){ | |
// do || true so that Jenkins won't notice if there is an error in any of those commands | |
// | |
// delete images w/o tags | |
sh 'docker rmi $(docker images | grep "^<none>" | awk \'{print $3}\') || true' | |
// remove dangling images | |
sh 'docker images -q -f dangling=true | xargs --no-run-if-empty docker rmi || true' | |
// remove dangling volumes | |
sh 'docker volume ls -qf dangling=true | xargs -r docker volume rm || true' | |
// remove all images! the ones that are running won't be removed. | |
// others are pushed to registry anyway | |
sh 'docker images -q | xargs --no-run-if-empty docker rmi || true' | |
} | |
def prompt(msg) { | |
try{ | |
timeout(time: 120, unit: 'SECONDS') { | |
try { | |
input msg | |
echo "User gave approval" | |
return true | |
} catch (ignored) { | |
echo "User pressed NO" | |
return false | |
} | |
} | |
} catch (ignored){ | |
echo "Nothing is pressed and prompt timed out" | |
return false | |
} | |
} | |
return this; |
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
#!/usr/bin/env groovy | |
def deployPlaybook = "playbooks/staging-redeploy.yml" | |
node { | |
def commons | |
stage("log") { | |
checkout scm | |
commons = load("jenkinsfile-commons.groovy") | |
commons.dumpEnv() | |
} | |
try { | |
stage("build-deps") { | |
dir("project-commons") { | |
sh "mvn clean install" | |
} | |
} | |
stage("test") { | |
dir("project-bots") { | |
sh "mvn clean package" | |
} | |
} | |
stage("deploy to staging") { | |
def deploy = commons.prompt('Deploy to staging system?') | |
if (!deploy) { | |
echo "Skipping deployment to staging system" | |
} else { | |
echo "Gonna deploy now to staging system" | |
sh "echo \"${env.VAULT_PASS}\" > /tmp/${env.BUILD_TAG}_vault_pass" | |
dir("project-ops") { | |
sh "ansible-playbook -i inventory -f 5 --vault-password-file /tmp/${env.BUILD_TAG}_vault_pass \"${deployPlaybook}\"" | |
} | |
} | |
} | |
commons.success() | |
} catch (err) { | |
commons.handleError(err) | |
} finally { | |
catchError { | |
stage("clean up") { | |
// delete the temporary vault passfile. don't fail if the file doesn't exist | |
sh "rm /tmp/${env.BUILD_TAG}_vault_pass || true" | |
} | |
} | |
commons.sendNotifications() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment