简体   繁体   中英

Bitbucket webhooks to trigger Jenkins project

I have a simple Jenkins pipeline job that does a bunch of things and calls other jobs. 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. This was easy with Gitlab where I just had to add a webhook with the Jenkins job url on Gitlab. How do I achieve this with Bitbucket? It looks like it always needs a repo url in Jenkins for the webhook to be triggered but I have no repo. Ex my pipeline job on Jenkins is

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

I want to trigger this build when a PR is created on Bitbucket for repo xyz.

Or in general how to make Jenkins pipeline jobs with pipeline script and Bitbucket Webhooks work? All either speak about freestyle job or multibranch job or pipeline job with Jenkinsfile

For this, you can use the 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.

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.

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

You can read more about the webhook messages Bitbucket will send from here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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