我如何让 Jenkins 建立在推送到 BitBucket git 存储库的基础上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11231064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get Jenkins to build on push to a BitBucket git repository?
提问by Allen T.
I have a git repository hosted on BitBucket, and have set up SSH authentication between the repository and my Jenkins server. I can build on Jenkins manually, but cannot get the Jenkins service on BitBucket to trigger builds.
我有一个托管在 BitBucket 上的 git 存储库,并在存储库和我的 Jenkins 服务器之间设置了 SSH 身份验证。我可以手动在 Jenkins 上构建,但无法让 BitBucket 上的 Jenkins 服务触发构建。
Jenkins configuration:
- Project Name: [my_jenkins_job]
- Build Triggers:
--Trigger Builds Remotely:
---Token: [token]
BitBucket configuration:
- Endpoint: http://[my_jenkins_address]/job/[my_jenkins_job]/build (I've also tried build?token=[token])
- Project Name: [my_jenkins_job]
- Module Name: [blank]
- Token: [token]
Visiting http://{my_jenkins_address}
/job/{my_jenkins_job}
/build?token={token}
kicks off a build properly.
访问http:// {my_jenkins_address}
/job/ {my_jenkins_job}
/build?token= 可以{token}
正确启动构建。
Why doesn't pushing a change to BitBucket cause Jenkins to initiate a build?
为什么推动对 BitBucket 的更改不会导致 Jenkins 启动构建?
采纳答案by Joseph Sturtevant
I have our Jenkins instance set up to poll the repository every minute. Is that not frequent enough for your needs?
我将 Jenkins 实例设置为每分钟轮询存储库。这对您的需求来说还不够频繁吗?
Our Jenkins configuration:
我们的詹金斯配置:
Build Triggers > Poll SCM = Checked
构建触发器 > 轮询 SCM = 检查
Build Triggers > Poll SCM > Schedule =
构建触发器 > 轮询 SCM > 计划 =
# every 1 minute
*/1 * * * *
回答by salcosand
Due to the Jenkins Hook of Bitbucket is not working at all for me and I have different Jenkins projects for different branches I had come to this solution:
由于 Bitbucket 的 Jenkins Hook 对我来说根本不起作用,而且我为不同的分支有不同的 Jenkins 项目,所以我来到了这个解决方案:
- Install Bitbucket Pluginat your Jenkins
- Add a normal Post as Hook to your Bitbucket repository (Settings -> Hooks) and use following url:
- 在您的 Jenkins 上安装Bitbucket 插件
- 将一个普通的 Post 作为 Hook 添加到您的 Bitbucket 存储库(Settings -> Hooks)并使用以下 URL:
https://YOUR.JENKINS.SERVER:PORT/bitbucket-hook/
https: //YOUR.JENKINS.SERVER:PORT/bitbucket-hook/
and if you have setup authentication on jenkins then URL must be like
如果您在 jenkins 上设置了身份验证,则 URL 必须类似于
https://USERNAME:[email protected]:PORT/bitbucket-hook/
https:// USERNAME:[email protected]:PORT/bitbucket-hook/
- Configure your Jenkins project as follows:
- under build triggerenable Build when a change is pushed to BitBucket
- under Source Code Managementselect GIT; enter your credentials and define Branches to build(like **feature/*)
- 按如下方式配置您的 Jenkins 项目:
- 在构建触发器下,在将更改推送到 BitBucket 时启用构建
- 在源代码管理下选择GIT;输入您的凭据并定义要构建的分支(如**feature/*)
By this way I have three build projects, one for all features, one for develop and one for release branch. Make sure to include the slash ('/') on the end of the URL or the hook won't work.
通过这种方式,我有三个构建项目,一个用于所有功能,一个用于开发,一个用于发布分支。确保在 URL 末尾包含斜杠 ('/'),否则钩子将不起作用。
And best of it, you don't have to ad new hooks for new Jenkins projects.
最棒的是,您不必为新的 Jenkins 项目添加新的钩子。
回答by Ian Hunter
You can actually get this to work in Jenkins with the Bitbucket service, but it took some playing around to get it working.
实际上,您可以使用 Bitbucket 服务在 Jenkins 中使用它,但需要花费一些时间才能使其工作。
If you use authentication, you can grab an API token from one of the users that can create builds. I created a separate account just for Bitbucket. You'll need at leastJenkins 1.426 to use the API token. You can then use HTTP authentication with the API token as your password for the Bitbucket service.
如果您使用身份验证,则可以从可以创建构建的用户之一获取 API 令牌。我为 Bitbucket 创建了一个单独的帐户。您至少需要Jenkins 1.426 才能使用 API 令牌。然后,您可以使用带有 API 令牌的 HTTP 身份验证作为 Bitbucket 服务的密码。
This is how mine is set up:
这是我的设置方式:
Endpoint:http://[bitbucket]:[APITOKEN]@[www.example.com/jenkins/]
端点:http://[bitbucket]:[APITOKEN]@[www.example.com/jenkins/]
Project Name:[NameOfMyJenkinsProject]
项目名称:[NameOfMyJenkinsProject]
Module Name:[empty]
模块名称:[空]
Token:[Token found in Jenkins project settings]
Token:【在Jenkins项目设置中找到的Token】
After I set it up like this, I was able to use the token as well as authentication to enable push-triggered builds from Bitbucket.
像这样设置后,我能够使用令牌和身份验证来启用来自 Bitbucket 的推送触发构建。
回答by floatingLomas
The token stuff is useless if you use authentication in Jenkins.
如果您在 Jenkins 中使用身份验证,那么令牌的东西就没用了。
use the Git plugin, and use a POST hook with http[s]://your.site.com[/jenkins]/git/[email protected]:your-username/your-repo.git
in it. Be sure that polling is on and schedule at some value, or this won't work.
使用 Git 插件,并在其中使用 POST 钩子http[s]://your.site.com[/jenkins]/git/[email protected]:your-username/your-repo.git
。确保轮询处于开启状态并按某个值进行安排,否则这将不起作用。
回答by Abhijeet Kamble
Using Poll SCM is good, however it has few disadvantages also, it will fetch metadata from you GIT Repository, which anyway is using some percentage of bandwidth from your bitbucket server and Jenkins server.
使用 Poll SCM 是好的,但它也有一些缺点,它会从你的 GIT 存储库中获取元数据,无论如何它会使用来自你的 bitbucket 服务器和 Jenkins 服务器的一定比例的带宽。
It's better that Bitbucket knows when to trigger Jenkins if any new update arrives.If you are using bitbucket latest versions, there are a plugin name "Stash webhooks for Jenkins" which is now compatible with Bitbucket.
如果有任何新更新,Bitbucket 知道何时触发 Jenkins 会更好。如果您使用的是 bitbucket 最新版本,则有一个插件名称“Stash webhooks for Jenkins”现在与 Bitbucket 兼容。
Within this plugin, you just have to specify the Jenkins URL and the JOB name, this also provides an extra layer of security between Jenkins and Bitbucket.
在这个插件中,你只需要指定 Jenkins URL 和 JOB 名称,这也为 Jenkins 和 Bitbucket 之间提供了额外的安全层。
This is the plugin (Stash webhooks for Jenkins which is not called Bitbucket webhooks for Jenkins) which can be downloaded from Atlassian Marketplace.
这是可以从 Atlassian Marketplace 下载的插件(Stash webhooks for Jenkins,不称为 Bitbucket webhooks for Jenkins)。
Above is the Add-on settings where you have to specify Jenkins URL and Repo Clone URL. With the trigger, you can also check the connection etc.
以上是附加设置,您必须在其中指定 Jenkins URL 和 Repo Clone URL。使用触发器,您还可以检查连接等。
Link for the plugin : Bitbucket webhooks for Jenkins
插件链接:适用于 Jenkins 的 Bitbucket webhooks
How this Plugin works:
这个插件的工作原理:
- Install the Git Plugin in Jenkins. Configure your project to use Git for your Source Code Management. You will need to keep the Repository URL you use for configuration in Stash. Enable the Poll SCM option in the Build Triggers. This is required to remotely trigger a build. Since we don't really need to poll, you can set the poll frequency to poll very infrequently. Save your project configuration.
- Enter the URL for your Jenkins instance and the Repository URL that you configured Jenkins to use. You can use the dropdown to get the clone URL for each supported the protocol. Afterwards, feel free to change it to match your Jenkins instance.
- That's it!
- 在 Jenkins 中安装 Git 插件。将您的项目配置为使用 Git 进行源代码管理。您需要在 Stash 中保留用于配置的 Repository URL。在 Build Triggers 中启用 Poll SCM 选项。这是远程触发构建所必需的。由于我们并不真正需要轮询,您可以将轮询频率设置为非常不频繁地轮询。保存您的项目配置。
- 输入 Jenkins 实例的 URL 和配置 Jenkins 使用的存储库 URL。您可以使用下拉菜单获取每个支持的协议的克隆 URL。之后,您可以随意更改它以匹配您的 Jenkins 实例。
- 就是这样!
With both Jenkins and Stash now configured, if you commit code, a build trigger will automatically occur. What happens is the post-receive hook fires a GET request to Jenkins, which then tells it to poll the repository (why you need to have polling turned on). The poll checks to see if there are actually any changes. Since there are, it triggers the build!
现在配置了 Jenkins 和 Stash,如果你提交代码,构建触发器将自动发生。发生的事情是 post-receive 钩子向 Jenkins 发出 GET 请求,然后告诉它轮询存储库(为什么需要打开轮询)。投票会检查是否真的有任何变化。既然有,它就会触发构建!
回答by joeystdio
Just install the bitbucket plugin and follow the instructions as can be found on the plugins page:
只需安装 bitbucket 插件并按照插件页面上的说明进行操作:
Configure your BitBucket repository with a POST hook, using URL JENKINS_URL/bitbucket-hook (no need for credentials). On each commit, all jobs with "Build when a change is pushed to BitBucket" option enabled will be triggered.
使用 URL JENKINS_URL/bitbucket-hook(不需要凭据)使用 POST 挂钩配置您的 BitBucket 存储库。在每次提交时,将触发所有启用了“将更改推送到 BitBucket 时构建”选项的作业。
See here: https://wiki.jenkins-ci.org/display/JENKINS/BitBucket+Plugin
请参阅此处:https: //wiki.jenkins-ci.org/display/JENKINS/BitBucket+Plugin
回答by abdelhadi danba
I was struggling with this issue for long, and couldn't find a solution in Jenkins documentation, I found herea solution in Cloudbees support site.
我在这个问题上苦苦挣扎了很长时间,在 Jenkins 文档中找不到解决方案,我在这里找到了 Cloudbees 支持站点的解决方案。
You just need to add these tow webhooks to your bitbucket repo and it works very fine :
您只需要将这些两个 webhooks 添加到您的 bitbucket 存储库中,它就可以很好地工作:
https://JENKIN_URL/bitbucket-scmsource-hook/notify
https://JENKIN_URL/bitbucket-hook
And i work for all pipelines even MultiBranch ones.
我为所有管道工作,甚至是 MultiBranch 管道。
Hope this helps someone ;)
希望这有助于某人;)
ADA
ADA