简体   繁体   English

将代码推送到 bitbucket 时构建 jenkins 作业

[英]Build jenkins job when push code to bitbucket

I have a project in bitbucket and recently installed jenkins .我在 bitbucket 中有一个项目,最近安装了 jenkins 。

In jenkins I created a job in jenkins .在 jenkins 中,我在 jenkins 中创建了一份工作。 from New Item , I ticked Freestyle project and did not add anything to the jenkins project .New Item ,我勾选了Freestyle project并且没有向 jenkins 项目添加任何内容。

in the configure section在配置部分

  • I added Source Code Management -> none我添加了源代码管理 -> 无
  • Build Triggers -> Build when a change is pushed to BitBucket构建触发器 -> 在将更改推送到 BitBucket 时构建

In Bitbucket went to在 Bitbucket 中去了

  • Project settings项目设置
  • Hooks挂钩
  • Select hook -> jenkins选择钩子->詹金斯
  • Add hook添加钩子

and added the following values并添加了以下值

Endpoint :- http://myaddress.com:8080端点:- http://myaddress.com:8080
Module name : my jenkins job name模块名称:我的詹金斯工作名称
project name - empty项目名称 - 空
token - empty令牌 - 空

so所以

I want to bulid my jenkins job automatically when I push code to bitbucket.当我将代码推送到 bitbucket 时,我想自动构建我的 jenkins 工作。

After some research I found a good solution finally:经过一番研究,我终于找到了一个很好的解决方案:

  • Install Bitbucket Plugin at your Jenkins在您的 Jenkins 上安装Bitbucket 插件
  • Add a normal Post as Hook to your Bitbucket repository ( Settings -> Hooks ) and use following url:将一个普通的 Post 作为 Hook 添加到您的 Bitbucket 存储库( Settings -> Hooks )并使用以下 URL:

https:// YOUR.JENKINS.SERVER:PORT /bitbucket-hook https: //YOUR.JENKINS.SERVER:PORT /bitbucket-hook

  • Configure your Jenkins project as follows:按如下方式配置您的 Jenkins 项目:
    • under build trigger enable Build when a change is pushed to BitBucket将更改推送到 BitBucket 时,构建触发器下启用构建
    • under Source Code Management select GIT ;源代码管理下选择GIT enter your credentials and define Branches to build (like **feature/* )输入您的凭据并定义要构建的分支(如**feature/*

By this way I have three build projects, one for all features, one for develop and one for release branch.通过这种方式,我有三个构建项目,一个用于所有功能,一个用于开发,一个用于发布分支。

Best, you don't have to create new hooks for every Jenkins project just define the separate build process or branches to be build.最好,您不必为每个 Jenkins 项目创建新的钩子,只需定义单独的构建过程或要构建的分支。

AFAIK, you need to have Git or Mercurial setup to work with BitBucket. AFAIK,您需要安装 Git 或 Mercurial 才能使用 BitBucket。 After all, you will be making changes to your files.毕竟,您将对文件进行更改。 Please refer these links for further info on how to configure it with Jenkins.请参阅这些链接以获取有关如何使用 Jenkins 配置它的更多信息。

Some detail from the link mentioned below:下面提到的链接中的一些细节:

Preparing the Jenkins project准备 Jenkins 项目

  1. Create a new project or configure your existing project that you want to hook up with your Bitbucket account:创建一个新项目或配置您想要与您的 Bitbucket 帐户挂钩的现有项目:

    a) Under “Source Code Management”, add your Git or Mecurial repository a) 在“源代码管理”下,添加您的 Git 或 Mecurial 存储库

    b) Under “Build Triggers”, check “Triggers builds remotely (eg, from scripts)” and add a really random string in the “Authentication Token” field. b) 在“构建触发器”下,选中“远程触发器构建(例如,从脚本)”并在“身份验证令牌”字段中添加一个真正随机的字符串。 I'm now using KeePassX's random password generator feature to generate the randomized and secure string我现在使用 KeePassX 的随机密码生成器功能来生成随机和安全的字符串

  2. Before we hook it up with Bitbucket, we'll need grab an API token from our Jenkins account:在我们将它与 Bitbucket 连接之前,我们需要从我们的 Jenkins 帐户中获取一个 API 令牌:

    a) Go back to your Jenkins main page, click on “People” link in the sidebar a) 返回您的 Jenkins 主页,单击侧栏中的“人员”链接

    b) In the People page, click on our newly created account's entry b) 在“人员”页面中,单击我们新创建的帐户条目

    c) In our user page, click on the “Configure” link in the sidebar c) 在我们的用户页面,点击侧边栏中的“配置”链接

    d) Under “API Token”, click on the “Show API Token” button and copy that entry d) 在“API 令牌”下,单击“显示 API 令牌”按钮并复制该条目

Bitbucket + Jenkins = Bliss! Bitbucket + Jenkins = 幸福!

  1. Now it's time to hook Jenkins up with Bitbucket!现在是时候将 Jenkins 与 Bitbucket 联系起来了! Visit your desired Bitbucket repository overview page and click on the “Admin” tab访问您想要的 Bitbucket 存储库概览页面,然后单击“管理”选项卡

  2. Under the repository's admin page, click on “Services” at the sidebar:在存储库的管理页面下,单击侧边栏中的“服务”:

    a.一种。 Choose “Jenkins” from the “Select a service…” drop-down and click on “Add Service”从“选择服务...”下拉菜单中选择“Jenkins”,然后单击“添加服务”

    b.You'll be presented four fields:您将看到四个字段:

     b1) Endpoint: Here, you'll need to set your Jenkins URL in the following format — http://username:apitoken@yourjenkinsurl.com/ b2) Project name: The project name that you've set in Jenkins b3) Token: It's the authentication token we created in Step 4 b4) Module name: (Optional) OK, this is one nifty feature that allow you to tell Bitbucket to only trigger a build when the commit contains file names that begins with a certain pattern

Ref: http://felixleong.com/blog/2012/02/hooking-bitbucket-up-with-jenkins参考: http : //felixleong.com/blog/2012/02/hooking-bitbucket-up-with-jenkins

I haven't done used Bitbucket for it.我还没有使用过 Bitbucket。 But it looks like a simple requirement and Jenkins Poll SCM feature should work for you .但它看起来像一个简单的要求,Jenkins Poll SCM 功能应该适合您 You can have a look here for instructions你可以看看这里的说明

Off-course you need to add your ssh public key in jenkins for this to work当然,您需要在 jenkins 中添加您的 ssh 公钥才能使其正常工作

I am not sure in method you describe what authentication mechanism Jenkins will use to checkout your repo from Bitbucket?我不确定您在描述Jenkins 将使用什么身份验证机制从 Bitbucket 结帐您的存储库的方法中?

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

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