From c45385af45fcfa663bee6bdb9f219dccef539be0 Mon Sep 17 00:00:00 2001 From: Michael Scharp Date: Fri, 15 Apr 2016 11:27:25 -0600 Subject: [PATCH] Add notifyStash global function --- .../notify-stash/README.md | 1 + .../notify-stash/notifyStash.groovy | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 global-library-examples/notify-stash/README.md create mode 100644 global-library-examples/notify-stash/notifyStash.groovy diff --git a/global-library-examples/notify-stash/README.md b/global-library-examples/notify-stash/README.md new file mode 100644 index 0000000..996f645 --- /dev/null +++ b/global-library-examples/notify-stash/README.md @@ -0,0 +1 @@ +This global function allows jenkins to notify stash of build status for a given commit. It uses the withCredentials and sh steps as well as curl to communicate with stash. diff --git a/global-library-examples/notify-stash/notifyStash.groovy b/global-library-examples/notify-stash/notifyStash.groovy new file mode 100644 index 0000000..f906d7f --- /dev/null +++ b/global-library-examples/notify-stash/notifyStash.groovy @@ -0,0 +1,31 @@ +/** + * Notify stash of build status for commit + * + * @param status{String} INPROGRESS | SUCCESSFUL | FAILED + * @param message{String} Message to record in stash + * @param gitCommit{String} Full hash of git commit + */ +def call( status, message = "", gitCommit ) +{ + env.USERNAME = env.PASSWORD = "" + + def postData = """{ + "state": "$status", + "key": "$env.JOB_NAME #$env.BUILD_NUMBER", + "url": "$env.BUILD_URL", + "description": "Built by Jenkins: $message" + }""" + + // CREDENTIAL_ID_IN_JENKINS should be replaced with your stash user credentials + // that have been stored in jenkins. + + withCredentials([[$class: 'UsernamePasswordMultiBinding', + credentialsId: CREDENTIAL_ID_IN_JENKINS, + usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) + { + def stashUrl = "https://YOUR_STASH_SERVER/rest/build-status/1.0/commits/$gitCommit" + def headers = "-H \"Content-Type: application/json\"" + def credentials = "--user $env.USERNAME:$env.PASSWORD" + sh "curl $credentials $headers -X POST -d '$postData' $stashUrl" + } +}