4

我一直在尝试让 Jenkins 的“Bitbucket Build Status Notifier”插件通知 Bitbucket 提交后的 Maven 构建是否成功。

可用的文档很好地解释了如何创建凭据并将它们添加到 Jenkins(我将它们全局添加到 Jenkins master)

不幸的是,插件页面(https://wiki.jenkins-ci.org/display/JENKINS/Bitbucket+Cloud+Build+Status+Notifier+Plugin)和 GitHub 项目(https://github.com/jenkinsci/ bitbucket-build-status-notifier-plugin)没有描述如何将 Bitbucket 凭证与通知程序的管道版本一起使用(https://issues.jenkins-ci.org/browse/JENKINS-33841

我曾尝试在 Jenkins 文件中使用它,如下所示:

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')
                }
            }
        }
    }
}

不幸的是,我似乎总是以

[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

这些凭据肯定存在,但我不确定如何告诉管道脚本如何使用它们。

4

1 回答 1

5

在阅读了有关 Jenkins 管道语法的更多信息并查看了 Snippet 生成器语法提示(可通过 http://.../pipeline-syntax/ 在您的 Jenkins 主机上获得)后,我了解到错误不是使用 withCredentials 步骤,而是使用“credentialsId”选项可用于此插件。

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-....')

                }
            }
        }
    }
}

当您使用控制面板查看感兴趣的凭据时,它可能会帮助您从 Jenkins 中的 URL 获取凭据。

每次都需要传递凭证ID似乎有点奇怪,也许有人可以发布如何定义默认值?

于 2017-06-11T20:47:53.573 回答