Issue
Is it possible to use Bitbucket webhooks with Jenkins and show the build results on Bitbucket?
Has anyone achieved this combination? When using webhooks on Bitbucket, how do you show the build results from Jenkins?
Solution
In the end I used the Bitbucket REST API to show the build status on Jenkins, was the easiest and most flexible way:
https://developer.atlassian.com/server/bitbucket/how-tos/updating-build-status-for-commits/
I used the Jenkins Generic Webhook trigger to get the payload from the open PR such as commitId: https://plugins.jenkins.io/generic-webhook-trigger/
def notifyBitBucket(commitId, state) {
withCredentials([string(credentialsId: ‘[your-bitbucket-user-token], variable: 'TOKEN')]) {
try {
sh """
curl --location --request POST “[your-bitbucket-repo]/rest/build-status/1.0/commits/${commitId}" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer ${TOKEN}" \
--data-raw '{
"state": "${state}",
"key": "pipeline-webhook",
"name": "pipeline-webhook",
"url": "${BUILD_URL}/console#footer",
"description": “[something describing your build]”
}'
"""
} catch(e) {
echo 'Notify failed, trying again... error: ' + ex.toString()
notifyBitBucket(commitId, state)
}
}
}
node {
try {
stage('Build') {
notifyBitBucket(commitId, "INPROGRESS");
... your logic
notifyBitBucket(commitId, "SUCCESSFUL");
} catch(ex) {
echo 'Build failed with error: ' + ex.toString()
notifyBitBucket(commitId, "FAILED");
currentBuild.result = 'FAILED'
}
}
I added try catch cause sometimes the API would fail.
Answered By - Bernard Wiesner