简体   繁体   中英

Build jenkins job when push code to bitbucket

I have a project in bitbucket and recently installed jenkins .

In jenkins I created a job in jenkins . from New Item , I ticked Freestyle project and did not add anything to the jenkins project .

in the configure section

  • I added Source Code Management -> none
  • Build Triggers -> Build when a change is pushed to BitBucket

In Bitbucket went to

  • Project settings
  • Hooks
  • Select hook -> jenkins
  • Add hook

and added the following values

Endpoint :- 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.

After some research I found a good solution finally:

  • Install Bitbucket Plugin at your Jenkins
  • Add a normal Post as Hook to your Bitbucket repository ( Settings -> Hooks ) and use following url:

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

  • Configure your Jenkins project as follows:
    • under build trigger enable Build when a change is pushed to BitBucket
    • under Source Code Management select GIT ; enter your credentials and define Branches to build (like **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.

AFAIK, you need to have Git or Mercurial setup to work with 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.

Some detail from the link mentioned below:

Preparing the Jenkins project

  1. Create a new project or configure your existing project that you want to hook up with your Bitbucket account:

    a) Under “Source Code Management”, add your Git or Mecurial repository

    b) Under “Build Triggers”, check “Triggers builds remotely (eg, from scripts)” and add a really random string in the “Authentication Token” field. I'm now using KeePassX's random password generator feature to generate the randomized and secure string

  2. Before we hook it up with Bitbucket, we'll need grab an API token from our Jenkins account:

    a) Go back to your Jenkins main page, click on “People” link in the sidebar

    b) In the People page, click on our newly created account's entry

    c) In our user page, click on the “Configure” link in the sidebar

    d) Under “API Token”, click on the “Show API Token” button and copy that entry

Bitbucket + Jenkins = Bliss!

  1. Now it's time to hook Jenkins up with Bitbucket! Visit your desired Bitbucket repository overview page and click on the “Admin” tab

  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”

    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

I haven't done used Bitbucket for it. But it looks like a simple requirement and Jenkins Poll SCM feature should work for you . You can have a look here for instructions

Off-course you need to add your ssh public key in jenkins for this to work

I am not sure in method you describe what authentication mechanism Jenkins will use to checkout your repo from Bitbucket?

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