Bitbucket webhook 和 Jenkins,显示构建结果

Bitbucket webhook and Jenkins, showing build results

是否可以将 Bitbucket webhooks 与 Jenkins 一起使用并在 Bitbucket 上显示构建结果?

有没有人实现这个组合?在 Bitbucket 上使用 webhooks 时,如何显示 Jenkins 的构建结果?

最后我使用 Bitbucket REST API 在 Jenkins 上显示构建状态,是最简单和最灵活的方式:

https://developer.atlassian.com/server/bitbucket/how-tos/updating-build-status-for-commits/

我使用 Jenkins 通用 Webhook 触发器从开放的 PR 中获取有效负载,例如 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'
  }
}

我添加了 try catch,因为有时 API 会失败。