Skip to content

Instantly share code, notes, and snippets.

@aliok
Created April 26, 2017 12:35
Show Gist options
  • Save aliok/bb3de89cea19d03f8813f209f9df501a to your computer and use it in GitHub Desktop.
Save aliok/bb3de89cea19d03f8813f209f9df501a to your computer and use it in GitHub Desktop.
Sample advanced Jenkins pipeline
/**
* 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;
#!/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