简体   繁体   English

BitBucket WebHook Jenkins

[英]BitBucket WebHook Jenkins

I'd like to configure bitbutcket to trigger a jenkins build.我想配置 bitbutcket 以触发 jenkins 构建。

I've spent some time researching this and all the answers are from a few years ago, and have not found any guides because things seem to have changed since.我花了一些时间对此进行研究,所有答案都是几年前的,并且没有找到任何指南,因为从那以后情况似乎发生了变化。

What I'm trying to do: A bitbucket push to a particular branch triggers a build.我正在尝试做的事情: bitbucket 推送到特定分支会触发构建。

What I've got: Bitbucket web hooks which fires HTTP request to Jenkins on a push to any branch.我得到了什么: Bitbucket web 钩子触发 HTTP 请求到 Z2E54334C0A5CE2E3E3E5A5845DF3 任何分支I've also installed the Bitbucket plugin on Jenkins which adds a check box in the job config Build when a change is pushed to BitBucket .我还在 Jenkins 上安装了 Bitbucket 插件, Build when a change is pushed to BitBucket中添加了一个复选框。 This checkbox doesnt seem to work (maybe I set it up wrong? minimal docs for this), despite me pushing to the configured branch in the SCM section.尽管我推送到 SCM 部分中配置的分支,但此复选框似乎不起作用(也许我设置错了?为此的最少文档)。

Problem 1 : Bitbucket does not fire a GET, but another request which causes a 403. I tested with postman, and it works with a GET, but not a POST.问题 1 :Bitbucket 没有触发 GET,而是另一个导致 403 的请求。我用 postman 进行了测试,它适用于 GET,但不适用于 POST。

Problem 2: This HTTP build request is fired on pushes to any branch.问题 2:这个 HTTP 构建请求在推送到任何分支时被触发。 While the build is still restricted to a particular branch, it seems unnecessary to be rebuilding all the time.虽然构建仍然仅限于特定分支,但似乎没有必要一直在重建。

How do i address these issues?我该如何解决这些问题? Bitbucket does not seem to be very flexible in customizing this. Bitbucket 在定制这个方面似乎不是很灵活。 The Jenkins plugin for bitbucket has a lot of 'bad' reviews. bitbucket 的 Jenkins 插件有很多“差”评论。 How are developers currently doing this?开发人员目前是如何做到这一点的?

SPECIFIC solution for Jenkins CI server--Webhook to Jenkins for Bitbucket plugin has been commercialized in latest version of Bit-Bucket and the current price is around $4800 which was earlier a free offering, because of this, guys who want to save their bucks, can go to the alternative solution by using webhooks feature of bit-bucket:- 针对Jenkins CI服务器的特定解决方案 - 在Jenb for Bitbucket插件的Webhook已经在最新版本的Bit-Bucket中商业化,目前价格约为4800美元,这是早期的免费产品,因此,想要节省他们的钱,可以使用bit-bucket的webhooks功能转到替代解决方案: -

Steps to create a webhook:- 创建webhook的步骤: -

BitBucket Side BitBucket Side

1) Go to your bitbucket repo, click on Repository Setting, under WORKFLOW got for WEBHOOKS option and create a webhook. 1)转到你的bitbucket repo,点击Repository Setting,在WORKFLOW下获取WEBHOOKS选项并创建一个webhook。

a) creation of webhook:- URL https://JenkinsserverURL/git/notifyCommit?url=https://bitbucket.repository-link/repository.git a)创建webhook: - URL https://JenkinsserverURL/git/notifyCommit?url=https://bitbucket.repository-link/repository.git

b) In the name tab, give any name of your choice b)在名称选项卡中,提供您选择的任何名称

c) click on TEST CONNECTION before saving it. c)在保存之前单击TEST CONNECTION。 Make sure you get http status 200 d) View details your logs, check your request and response is correct. 确保您获得http状态200 d)查看日志详细信息,检查您的请求和响应是否正确。

Things to take care of from Jenkins Side:- Jenkins Side要照顾的事情: -

1) Make sure repository mentioned in bitbucket webhook is used in Jenkins job. 1)确保在Jenkins作业中使用bitbucket webhook中提到的存储库。 2) In SCM option, activate/select Poll SCM option, don't mention anything in the schedule, leave it blank. 2)在SCM选项中,激活/选择Poll SCM选项,不要在时间表中提及任何内容,将其留空。 3) configure rest job, 3)配置休息工作,

Whenever your git repo observes any change an automatic build will get triggered in Jenkins. 每当你的git repo观察到任何变化时,都会在Jenkins中触发自动构建。 By default push trigger is activated and if you want to activate other action, please select those events while creating webhook. 默认情况下,激活推送触发器,如果​​要激活其他操作,请在创建webhook时选择这些事件。

***to specify the branch in repository webhook:- ***在存储库webhook中指定分支: -

http://yourserver/git/notifyCommit?url=<URL of the Git repository>[&branches=branch1[,branch2]*][&sha1=<commit ID>]

Cheers, 干杯,

Is your Jenkins URL accessible from your bitbucket server? 您的bitbucket服务器是否可以访问您的Jenkins URL? If yes that it should be fairly simple to do it. 如果是的话,它应该相当简单。 You add the webhook in your repository as http://<url-of-jenkins>/git/notifyCommit?url=<url-of-repository> . 您将存储库中的webhook添加为http://<url-of-jenkins>/git/notifyCommit?url=<url-of-repository> When jenkins receives this POST, it automatically triggers builds on those jobs that use this git repo with that URL you give in webhook. 当jenkins收到此POST时,它会自动触发那些使用此git repo的作业的构建版本与您在webhook中提供的URL。
But you also need to make sure your Build Schedule is set to empty for those jobs. 但是,您还需要确保将这些作业的“ Build Schedule设置为空。 otherwise it wont get triggered. 否则它不会被触发。 You can specify a branch in webhook URL too 您也可以在webhook URL中指定分支
See the Push Notification from repository here 请在此处查看Push Notification from repositoryPush Notification from repository
https://wiki.jenkins.io/display/JENKINS/Git+Plugin https://wiki.jenkins.io/display/JENKINS/Git+Plugin

For anyone here after July 2022, here are the simple steps I followed to make it work.对于 2022 年 7 月之后在这里的任何人,以下是我为使其工作而遵循的简单步骤。

Create a live Jenkins URL创建直播 Jenkins URL

First, create a tunnel from a live URL to your local Jenkins URL using ngrok because using locahost:8080 directly as your webhook URL on bitbucket will simply not work as bitbucket does not recognize your local computer. First, create a tunnel from a live URL to your local Jenkins URL using ngrok because using locahost:8080 directly as your webhook URL on bitbucket will simply not work as bitbucket does not recognize your local computer.

ps: ngrok claims to be the fastest way to put anything on the internet and I agree,
you can use it beyond Jenkins once you know the trick, 
such as quickly handling out your localhost react app for testing by your friends 
out of your local network

To do this is simple.要做到这一点很简单。 For Linux:对于 Linux:

  1. Install ngrok snap install ngrok安装 ngrok snap install ngrok
  2. Add authtoken ngrok config add-authtoken <token>添加 authtoken ngrok config add-authtoken <token>

Don't have an auth token, sign up没有身份验证令牌,请注册

  1. Start a tunnel on your Jenkins port eg ngrok http 8080在您的 Jenkins 端口上启动隧道,例如ngrok http 8080

To know more and for other OS, check ngrok download page要了解更多信息和其他操作系统,请查看ngrok 下载页面

You will then get a response like然后你会得到一个回应

ngrok                                                                                                                                                                                              (Ctrl+C to quit)
                                                                                                                                                                                                                   
Hello World! https://ngrok.com/next-generation                                                                                                                                                                     
                                                                                                                                                                                                                   
Session Status                online                                                                                                                                                                               
Account                       <your email>@<domain>.com (Plan: <plan type>)                                                                                                                                              
Version                       3.0.6                                                                                                                                                                                
Region                        Europe (eu)                                                                                                                                                                          
Latency                       162ms                                                                                                                                                                                
Web Interface                 <web interface url>
Forwarding                    https://<your-assigned-host>.ngrok.io -> http://localhost:8080                                       

Basically, the web interface URL on click gives you a web interface to inspect all the requests being tunnelled from your ngrok live URL to your local host. Basically, the web interface URL on click gives you a web interface to inspect all the requests being tunnelled from your ngrok live URL to your local host.

Forwarding URL is basically a proxy to your localhost, so when you want to configure webhook, instead of using locahost:8080, you replace it with ngrok URL eg https://syue-162-34-12-01.eu.ngrok.io and all requests get tunnelled to localhost:8080 Forwarding URL is basically a proxy to your localhost, so when you want to configure webhook, instead of using locahost:8080, you replace it with ngrok URL eg https://syue-162-34-12-01.eu.ngrok.io并且所有请求都通过隧道传输到 localhost:8080

Hook up the URL on bitbucket cloud在 bitbucket 云上连接 URL

Secondly, configure your Bitbucket repository with a Webhook, using URL JENKINS_URL/bitbucket-hook/ (no need for credentials but do remember the trailing slash) eg https://syue-162-34-12-01.eu.ngrok.io/bitbucket-hook/ Secondly, configure your Bitbucket repository with a Webhook, using URL JENKINS_URL/bitbucket-hook/ (no need for credentials but do remember the trailing slash) eg https://syue-162-34-12-01.eu.ngrok.io/bitbucket-hook/

If you are using bitbucket server and not cloud or you want to know more, the bitbucket plugin documentation for Jenkins is pretty straightforward and easily understandable, see bitbucket plugin If you are using bitbucket server and not cloud or you want to know more, the bitbucket plugin documentation for Jenkins is pretty straightforward and easily understandable, see bitbucket plugin

then you can inspect all your webhook requests on the web interface URL or via your terminal as well as check your build logs on Jenkins via your localhost port or ngrok live url. then you can inspect all your webhook requests on the web interface URL or via your terminal as well as check your build logs on Jenkins via your localhost port or ngrok live url.

Disclaimer : I have not figured out how to enable build only when a specific branch change but you can configure jenkins to only build a specific branch or any branch created as your need may demand, check Source Code Management and Build Triggers免责声明:我还没有弄清楚如何仅在特定分支更改时启用构建,但您可以将 jenkins 配置为仅构建特定分支或根据您的需要创建的任何分支,检查源代码管理构建触发器

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

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