Skip to content

Instantly share code, notes, and snippets.

@anilkrs09
Forked from aliok/jenkinsfile-commons.groovy
Created August 14, 2021 08:12
Show Gist options
  • Save anilkrs09/11a00b843b8449d1a12ec1cd2761b9f2 to your computer and use it in GitHub Desktop.
Save anilkrs09/11a00b843b8449d1a12ec1cd2761b9f2 to your computer and use it in GitHub Desktop.

Revisions

  1. @aliok aliok created this gist Apr 26, 2017.
    78 changes: 78 additions & 0 deletions jenkinsfile-commons.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    /**
    * 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;
    60 changes: 60 additions & 0 deletions jenkinsfile.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #!/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()
    }
    }