Hooking Bitbucket Up With Jenkins
Previously I’ve blogged about automating Hyde deployment with Jenkins and I’m prety much hooked with the idea of doing automated deployment and continuous integration in the future.
Now as much as I loved Github, Bitbucket is where I store all my private repository at — after all, you can’t beat the notion that it allows you to have private repositories even with free accounts :). So, with my blog stuff hosted at Github and my other stuff hosted with Bitbucket, some configuration changes are needed.
After reading the documentation and some trial and error, here’s how you would hook up with an secured instance of Jenkins:
Setting up a secured Jenkins instance
- To setup up a secured instance, click on “Manage Jenkins” and then “Configure System”
- Under “Access Control > Security Realm”, select the “Jenkins’s own user database” option and check the “Allow user to sign up” checkbox below it
- Under “Access Control > Authorization”, select the “Logged-in user can do anything”
- Once you save it, click on the “Sign In” link to create a Jenkins user account
- (Optional) Once you have created a user and logged in, you’d want to lock down your Jenkins instance and prevent new sign-ins. To do so, go to the “Configure System” page, uncheck the “Allow user to sign up” checkbox below it under “Access Control > Security Realm” section
Preparing the Jenkins project
- Now you’ve created a secured Jenkins instance, create a new project or configure your existing project that you want to hook up with your Bitbucket account:
- Under “Source Code Management”, add your Git or Mecurial repository
- Under “Build Triggers”, check “Triggers builds remotely (e.g., 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
- Before we hook it up with Bitbucket, we’ll need grab an API token from our Jenkins account:
- Go back to your Jenkins main page, click on “People” link in the sidebar
- In the People page, click on our newly created account’s entry
- In our user page, click on the “Configure” link in the sidebar
- Under “API Token”, click on the “Show API Token” button and copy that entry
Bitbucket + Jenkins = Bliss!
- Now it’s time to hook Jenkins up with Bitbucket! Visit your desired Bitbucket repository overview page and click on the “Admin” tab
- Under the repository’s admin page, click on “Services” at the sidebar:
- Choose “Jenkins” from the “Select a service…” drop-down and click on “Add Service”
- You’ll be presented four fields:
- Endpoint: Here, you’ll need to set your Jenkins URL in the following format — http://username:email@example.com/
- Project name: The project name that you’ve set in Jenkins
- Token: It’s the authentication token we created in Step 4
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.
For example, assume that you organize your source code the open source way where you have several subfolders (e.g. src, tests, doc) and you only want to trigger a build when actual code stored in the “src” folder is commited, you can set this field to “src/”. Once this is set, Bitbucket will only trigger a Jenkins build when changes in the “src” folder is made. 3. Try commit some code and see what happens 😄
If you want more details on how the Bitbucket’s service broker for Jenkins is implemented, Jesper Noehr have posted the the actual service broker code in Bitbucket’s Issue Tracker.
… and dang, this is a long article — I’d really love to redo this as a webcast next time. 😊 Anyway, hope you’d find it useful!