Trigger jenkins pipeline job with Bitbucket hook

7,255

If you just need generic post-receive hooks, you can just do a regular web hook and hit the build API endpoint. For instance, if the name of your Jenkins server is jenkins.example.com and the name of the Pipeline job to trigger is my_awesome_job, then you can fire off a POST request to https://jenkins.example.com/job/my_awesome_job/build. Depending on your security model, this request may or may not need to be authenticated and authorized. If you do need authentication and authorization, you can use HTTP Basic auth (unfortunately I haven't found a reasonable way to do keypair auth).

If you need to trigger builds on pull requests, then you'll need a plugin. I've been using the pull-request-notifier plugin for Bitbucket to trigger builds from Bitbucket and it's worked pretty well. It works especially well in combination with the Bitbucket Branch Source plugin for Jenkins, which automatically creates Jenkins jobs for every branch and open PR of every single repository in a Bitbucket project. The combination of these two plugins allows me to automatically trigger builds as soon as a PR is opened and prevent merging the PR until the build passes (this is a pretty common workflow so I figure it's worth mentioning even if you didn't explicitly ask about it).

Share:
7,255

Related videos on Youtube

Alex
Author by

Alex

Updated on September 18, 2022

Comments

  • Alex
    Alex over 1 year

    I'm missing something obvious here.

    Is there no way to trigger a Jenkins scripted pipeline job via Bitbucket hook?

    There's a bug out about it but it's been around for years with apparently no fix. Is the only way to make this work really to make an upstream trigger job that kicks off the pipeline? That seems insanely clunky.

  • 030
    030 about 6 years
    Interesting +1. How do you clean up does branch builds?
  • 030
    030 about 6 years
    And what about the resource management in Jenkins? How do you ensure that there are enough executors? I can imagine that there will be a lot of extra load on Jenkins.
  • 030
    030 about 6 years
    Regarding the Pull Request Notifier for Bitbucket Will it work for BitbucketCloud as well? Does it cost money? If unavailable, will the bb b source plugin for jenkins still work?
  • jayhendren
    jayhendren about 6 years
    It does not cost money. I use Bitbucket Server, not cloud, so I'm not sure about the compatibility there. However, I do know that the Bitbucket Branch Source plugin for Jenkins can automatically set up webhooks for Bitbucket cloud, so I don't think you even need the pull-request-notifier plugin for Bitbucket at all.