简体   繁体   English

Bitbucket webhook 以触发 Jenkins 项目

[英]Bitbucket webhooks to trigger Jenkins project

I have a simple Jenkins pipeline job that does a bunch of things and calls other jobs.我有一个简单的 Jenkins 管道作业,它可以做很多事情并调用其他作业。 There is no repo associated with the job.没有与该作业相关的存储库。 But this job should be called when a pull request is created in a certain repo in Bitbucket.但是,当在 Bitbucket 的某个存储库中创建拉取请求时,应该调用此作业。 This was easy with Gitlab where I just had to add a webhook with the Jenkins job url on Gitlab.这对于 Gitlab 来说很容易,我只需在 Z97926E23419583666 上的 Jenkins 作业 url 添加一个 webhook。 How do I achieve this with Bitbucket?如何使用 Bitbucket 实现这一目标? It looks like it always needs a repo url in Jenkins for the webhook to be triggered but I have no repo.看起来它总是需要 Jenkins 中的仓库 url 才能触发 webhook,但我没有仓库。 Ex my pipeline job on Jenkins is例如,我在 Jenkins 上的管道工作是

    stage('Stage 1'){
        echo "hello, world!"
    }
}

I want to trigger this build when a PR is created on Bitbucket for repo xyz.我想在 Bitbucket 上为 repo xyz 创建 PR 时触发此构建。

Or in general how to make Jenkins pipeline jobs with pipeline script and Bitbucket Webhooks work?或者一般来说,如何使用管道脚本和 Bitbucket Webhooks 使 Jenkins 管道作业工作? All either speak about freestyle job or multibranch job or pipeline job with Jenkinsfile所有人都使用 Jenkinsfile 谈论自由式工作或多分支工作或管道工作

For this, you can use the Generic Webhook Trigger Plugin .为此,您可以使用Generic Webhook Trigger Plugin The Job will be identified by the token you add to the Job.作业将由您添加到作业的令牌来标识。 Here is a sample Jenkins Pipeline and how you can extract information from the Webhook request to determine it's coming from a PR.这是一个示例 Jenkins 管道以及如何从 Webhook 请求中提取信息以确定它来自 PR。

pipeline {
  agent any
  triggers {
    GenericTrigger(
     genericVariables: [
      [key: 'PR_ID', value: '$.pullrequest.id', defaultValue: 'null'],
      [key: 'PR_TYPE', value: '$.pullrequest.type', defaultValue: 'null'],
      [key: 'PR_TITLE', value: '$.pullrequest.title', defaultValue: 'null'],
      [key: 'PR_STATE', value: '$.pullrequest.state', defaultValue: 'null'],
      [key: 'PUSH_DETAILS', value: '$.push', defaultValue: 'Null']
     ],

     causeString: 'Triggered By Bitbucket',
     token: '12345678',
     tokenCredentialId: '',
     printContributedVariables: true,
     printPostContent: true,
     silentResponse: false
    )
  }
  stages {
    stage('ProcessWebHook') {
      steps {
          script {
            echo "Received a Webhook Request from Bitbucket."
            if(PR_ID != "null") {
                echo "Received a PR with following Details"
                echo "PR_ID: $PR_ID ; PR_TYPE: $PR_TYPE ; PR_TITLE: $PR_TITLE ; PR_STATE: $PR_STATE"
                // If the PR state is either MERGED or DECLINED we have to do some cleanup
                if(PR_STATE == "DECLINED" || PR_STATE == "MERGED") {
                  // Do your cleanup here, You should have all the PR Details to figure out what to clean
                  echo "Cleaning UP!!!!!!"
                }
                
            } else if(PUSH_DETAILS != "null") {
                  echo "This is a commit."
            }
          }
      }
    }
  }
}

Then in Bitbucket, you will have the webhook URL like below.然后在 Bitbucket 中,您将拥有如下所示的 webhook URL。

JENKINS_URL/generic-webhook-trigger/invoke?token=12345678

You can read more about the webhook messages Bitbucket will send from here .您可以阅读有关 Bitbucket 将从此处发送的 webhook 消息的更多信息。

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

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