Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notifyStash global function #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions global-library-examples/notify-stash/README.md
Original file line number Diff line number Diff line change
@@ -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.
31 changes: 31 additions & 0 deletions global-library-examples/notify-stash/notifyStash.groovy
Original file line number Diff line number Diff line change
@@ -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"
}
}