2
votes

I have been trying to get Jenkins' "Bitbucket Build Status Notifier" plugin to notify Bitbucket whether the Maven build after a commit was successful or not.

The documentation available very nicely explains how to create credentials and add them to Jenkins (I added them globally to Jenkins master)

Unfortunately neither the plugin page (https://wiki.jenkins-ci.org/display/JENKINS/Bitbucket+Cloud+Build+Status+Notifier+Plugin), nor the GitHub project (https://github.com/jenkinsci/bitbucket-build-status-notifier-plugin) does not describe how to use Bitbucket credentials with pipeline version of notifier (https://issues.jenkins-ci.org/browse/JENKINS-33841)

I have tried using it in Jenkins file like this:

pipeline {

agent {
    label 'jenkins-slave'
}

stages {
    stage ('Build') {
        steps {
            script {
                withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '050a0876-fb6b-....',
                usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
                    bitbucketStatusNotify(buildState: 'INPROGRESS')

                    sh '''#!/bin/bash
                        mvn clean package
                        ...
                      '''
                    bitbucketStatusNotify(buildState: 'SUCCESSFUL')
                }
            }
        }
    }
}

Unfortunately I always seem to end up with

[Pipeline] // node
[Pipeline] End of Pipeline
java.lang.Exception: Credentials could not be found!
at org.jenkinsci.plugins.bitbucket.BitbucketBuildStatusHelper.sendBuildStatusNotification(BitbucketBuildStatusHelper.java:262)
at org.jenkinsci.plugins.bitbucket.BitbucketBuildStatusHelper.notifyBuildStatus(BitbucketBuildStatusHelper.java:252)
at org.jenkinsci.plugins.bitbucket.BitbucketBuildStatusNotifierStep$Execution.run(BitbucketBuildStatusNotifierStep.java:189)
at org.jenkinsci.plugins.bitbucket.BitbucketBuildStatusNotifierStep$Execution.run(BitbucketBuildStatusNotifierStep.java:140)
at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:47)
at hudson.security.ACL.impersonate(ACL.java:221)
at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:44)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Finished: FAILURE

Those credentials definitely exist, but I am not sure how to tell the pipeline script how to use them.

1

1 Answers

5
votes

After reading more about Jenkins pipeline syntax and reviewing Snippet generator syntax tips (available on your Jenkins host via http://…/pipeline-syntax/) I understood the mistake was not to use withCredentials step, but to use the "credentialsId" option available for this plugin.

pipeline {

agent {
    label 'jenkins-slave'
}

stages {
    stage ('Build') {
        steps {
            script {
                bitbucketStatusNotify(buildState: 'INPROGRESS', credentialsId: '050a0876-fb6b-....')
                sh '''#!/bin/bash
                    mvn clean package
                    ...
                  '''
                bitbucketStatusNotify(buildState: 'SUCCESSFUL', credentialsId: '050a0876-fb6b-....')

                }
            }
        }
    }
}

It might help someone you can get credentials from the URL in Jenkins when viewing the credential of interest using the control panel.

It seems a bit strange you need to pass the credential id every time, maybe somebody can post how to define default ones?