Bohdan Nesteruk
Bohdan Nesteruk

Reputation: 914

Jenkins build: notify Bitbucket cloud

I'm using Jenkins 2.346.2

The repository is located on bitbucket.org (cloud, not local server).

I want the build status to be sent to bitbucket and to be displayed as the PR build status.

I'm trying the plugin: https://plugins.jenkins.io/bitbucket-build-status-notifier/

The configuration is (multibranch pipeline project):


def notifyBitbucket(String state) {
    notifyBitbucket(
            commitSha1: 'a0e5012be0e8e89d122cc773a964c0en3a1a656b2',
            credentialsId: 'jenkins_bitbucket_ssh',
            disableInprogressNotification: false,
            considerUnstableAsSuccess: true,
            ignoreUnverifiedSSLPeer: true,
            buildStatus: state,
            buildName: 'Performance Testing',
            buildUrl: 'https://bitbucket.org',
            includeBuildNumberInKey: false,
            prependParentProjectKey: false,
            projectKey: '',
            stashServerBaseUrl: 'https://bitbucket.org')

}

But what I get is a returned bitbucket page saying 'Resource not found'. Currently, the only credentials I can use to connect to bitbucket is SSH key pair. And they work okay for pulling the code. I'm trying to use this key for the notification plugin as well. Is this wrong?

Could anyone let me know how to specify the path to the project in this case, please?

Upvotes: 0

Views: 1761

Answers (1)

M B
M B

Reputation: 3430

One option you can consider is using the Bitbucket API, which would remove the need for an external plugin. The endpoint you need to call is:

${BITBUCKET_API_HEAD}/commit/${env.COMMIT_HASH}/statuses/build

More on this in the documentation. Here is how I have done it:

httpRequest([
        acceptType        : 'APPLICATION_JSON',
        authentication    : '<credentials>',
        contentType       : 'APPLICATION_JSON',
        httpMode          : 'POST',
        requestBody       : '''{
            "key":"<unique-key>",
            "name":"PR-Branch-Build",
            "url":"<path-to-jenkins-build>/''' + env.BUILD_NUMBER + '''/pipeline",
            "description":"Build status: '''+ BUILD_STATUS +'''",
            "state":"'''+ BUILD_STATUS +'''"
        }''',
        responseHandle    : 'NONE',
        url               : "${BITBUCKET_API_HEAD}/commit/${env.COMMIT_HASH}/statuses/build",
        validResponseCodes: '200,201'
    ])

Upvotes: 1

Related Questions