繁体   English   中英

Bitbucket webhook 和 Jenkins,显示构建结果

[英]Bitbucket webhook and Jenkins, showing build results

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

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

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

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

我使用 Jenkins 通用 Webhook 触发器从打开的 PR 中获取有效负载,例如 commitId: https://plugins.jenkins.io/generic-webookh


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 会失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM