How to Integrate TeamCity and Bitbucket Server

April 20, 2016
#How To#Bitbucket
10 min

Bitbucket is a centralized development management solution for controlling your repositories behind the firewall. It allows you to simplify the procedures of hosting an internal source code control server and provide easy access to your repositories for all your team members.

In the IT world, it is not a secret that Bitbucket can be integrated with other software products and platforms into a single ecosystem that makes the entire development process comprehensive and convenient. Most times, people integrate Bitbucket with Jira. But localization and resolution of the issue is not the only problem, which a generic development encounters every day, several times a day. But it is more important to preserve the project integrity while adding modifications or corrections into the program code. For these purposes, you can use the continuous integration server that can also create project builds and run a series of tests for automatic verification of functionality.

With the introduction of the Integrated CI/CD, Atlassian extended the list of platforms supported out-of-the-box, so you can now link Bitbucket Data Center and Server with Bamboo or Jenkins. But it doesn’t mean that TeamCity users have to miss out on the integration benefits, and in this blog post, we will show how to link your Bitbucket and TeamCity and get the most of it.

View build statuses with Commit Status Publisher

First of all, we would like to present the native add-on from the developers of TeamCity because native integration tools are usually convenient, well-thought, and rich in features. 

Commit Status Publisher is an open-source plugin that comes bundled with TeamCity (since version 10.0) and allows it to automatically send build statuses of the commits to an external system.

To enable the transfer of the statuses to Bitbucket, you need to add a Build Feature with Commit Status Publisher type in the Build Configuration Settings 

And then set it up for Bitbucket.

Once the configuration is complete, run the application build and ensure that Bitbucket received data from TeamCity. If everything worked correctly, from now you’ll be able to see the build statuses on the Builds page.

Apart from the Builds page, builds statuses are indicated on the Commits and Branches pages. If you select any build status icon, it will take you to the Builds page with the filter set to that commit or branch.

While viewing the pull request details, you’ll see build status on the Overview tab or go straight to the Builds page to see builds information for each commit.

Run builds, access artifacts and logs without leaving Bitbucket

If you don’t want to spend time switching tools and would like to operate on your builds without losing the context, you can get a more advanced integration with TeamCity using the TeamCity Integration for Bitbucket app. It provides a seamless way to connect your Bitbucket to the TeamCity server, so you can monitor and configure the pipeline without leaving Bitbucket. Using it, you can:

  • Run builds
  • View build results
  • Access logs and artifacts via direct links.

After you installed the app to your instance, you need to authorize TeamCity in Bitbucket using access tokens.

You can create an access token in your TeamCity profile.

Now add the token to your profile in Bitbucket. Click on your avatar in the header and then on Manage account, choose TeamCity tab in the navigation bar and add the token.

You can enhance the integration by adding the Commit Status Publisher to your build configuration. 

Go to the Configuration Settings on a build page in TeamCity, click on the Build Features, Add Commit Status Publisher and set it up for Bitbucket as described a few paragraphs above.

Now you’re all set and can get to work with the app. On the Builds page, you can view build statuses, links to logs and artifacts, and perform build actions. At the moment, the list of actions include:

  • Build the latest commit — running a build for the latest commit in the current branch.
  • Re-build this commit — re-running a build for a particular commit.

You can also find build information on the Pull Requests page. The latest build statuses are shown in the Overview tab, while the Builds tab allows to act on builds and view builds’ logs and artifacts, similarly to the main Builds page.

TeamCity Integration for Bitbucket is available for a free 30-day trial on the Marketplace both for Bitbucket Server and Bitbucket Data Center. We have step-by-step guides to get you started faster, but if you have any questions about the app or feedback you’d like to share, create a ticket in our Help Center.

Integrate to improve

You can enhance your DevOps toolchain with the help of integrations described in the article to improve developers’ velocity as well as time to production. Which one to choose depends on your requirements and personal preferences. Write in the comments, which one you use or would use, and share your opinion about these integrations.

Related posts

    How to mirror Bitbucket Cloud and GitHub repositories to Bitbucket Server / Data Center

    October 8, 2015
    #How To#Bitbucket
    13 min

    Visualize, track and analyze repositories hosted in Bitbucket with Awesome Graphs. Benefit from what graphs, charts and reports can tell you. Learn more

    When working on our add-ons, we at Stiltsoft use Bitbucket Data Center for distributed version control management and code collaboration. Some of you have also picked Bitbucket Server / Data, and others went with another solution, e.g., Bitbucket Cloud, GitHub, or are using several platforms at once. Whatever tool you work in, mirroring repositories can come in useful in some instances. That is why we posted the article that covered the benefits and how-tos about mirroring remote repositories from Bitbucket Data Center and Server. Meanwhile, creating mirrors in Bitbucket Bitbucket Server / Data Center rather than mirroring from it might be more relevant for some of our blog readers, so today’s post is about that.

    Benefits

    There’s a number of goals that can be attained by mirroring repositories from Bitbucket Cloud or GitHub to Bitbucket Server / Data Center. Among them:

    • protecting yourself from downtime when Bitbucket Cloud or GitHub are unavailable
    • consolidating repositories in one place
    • facilitating the process of migration from Bitbucket Cloud or GitHub to Bitbucket Server / Data Center

    How to mirror Bitbucket Cloud repositories

    To arrange automatic update of mirrored repositories you can:

    • Use an add-on for Bitbucket Server
    • Use a Continuous Integration tool
    • Use an OS job scheduler

    In our examples, we’ll be mirroring a Bitbucket Cloud repository to Bitbucket Server / Data Center.

    Use an add-on for Bitbucket Server / Data Center

    ScriptRunner for Bitbucket can come in handy when you are looking to mirror some or all of your Bitbucket Cloud repositories to Bitbucket Server / Data Center.

    It’s very simple.

    • Once the add-on is installed, navigate to Bitbucket Server / Data Center Administration and select Built-in Scripts in the Script Runner Section.
    • There, choose Mirror Bitbucket Team. You can mirror repositories both from team and user Bitbucket Cloud accounts.
    ScriptRunner-Built-in-Scripts
    • Enter your Bitbucket Cloud Team or User, select the target project in Bitbucket Server / Data Center, provide your Bitbucket Cloud user credentials.
    ScriptRunner-Mirror Bitbucket Team
    • Select one of the synchronization options (none, install hook or poll). If you choose:
      • ‘install hook’, Bitbucket Cloud will call your Bitbucket Server / Data Center instance when there are changes in repositories which will trigger synchronization
      • ‘poll’, each remote repository will be polled every 5 minutes
      • ‘none’, there will be no synchronization performed
    • In the Regex field, there is the ‘.*’ regular expression by default. If you leave it as it is, all repositories will be mirrored from your Bitbucket Cloud. To filter the set of repositories, insert a corresponding regular expression. The regular expression is done against the repository name, e.g. for the repository ‘StiltSoft Test’ you could use:
    •  
    .(?=.\bStiltSoft\b.)(?=.\bTest\b.).
    • Another useful feature. If you’d like new Bitbucket Server / Data Center repositories to be created and synchronized when there are newly-added remote repositories in Bitbucket Cloud, mark the ‘Sync new’ checkbox.
    • When all set, click Run. You’ll see the table with the repositories that are being mirrored. Initially there’s the ‘Create’ label. When the process is completed, it will say ‘Exists’ instead. You may tail the application log file to track the progress.
     ScriptRunner-Mirror Bitbucket Team
    • Once local repositories are created, you can refer to the Configure Mirrored Repositories in Built-in Scripts to view and check the status of your mirrored repositories and change the synchronization type.

    Use a Continuous Integration tool

    If you have some Continuous Integration solution, e.g. TeamCity, in your arsenal, you can use it. Here is one of the options for how to set up automatic update of mirrors in Bitbucket Server / Data Center using TeamCity.

    • To use SSH-authentication, copy a SSH key from the TeamCity server with a build agent you’ll be using. Then add this SSH key in your Bitbucket Cloud and Bitbucket Server / Data Center account settings.
    • Then you need to create a repository in Bitbucket Server / Data Center
    • After that, create a local copy of source repository on build agent (don’t forget to add Bitbucket Server / Data Center repository as remote):
    cd /home 
    mkdir mirror
    cd mirror
    git clone git@bitbucket.org:kkolina/demonstration-repository.git
    cd demonstration-repository
    git remote add bitbucket ssh://git@stash2.stiltsoft.com:7999/adp/demonstration-repository.git

    Now you should make some changes in build configuration in TeamCity:

    • Add a Command Line Runner build step with a script for updating mirrored repositories that will run every time this build step is invoked.

    Example of the script:

    cd /home/mirror/demonstration-repository 
    git pull
    git push --all bitbucket
    git push --tags bitbucket

    You may also add a VCS Trigger that will add a build to the queue when a VCS check-in is detected in the repository you are mirroring.

    • Before adding a trigger, you should attach a VSC root in the Version Control Settings:

    With the URL of the original repository and Bitbucket Cloud authentication settings:

    • Now you can add a VCS Trigger:

    Use an OS job scheduler

    You can also use OS job schedulers (Cron, Windows Task Scheduler) and configure an external job and schedule the launch of scripts that will be pulling changes and pushing them to a mirrored repository.

    • First we clone a repository:
    cd /home/mirror 
    git clone git@bitbucket.org:kkolina/demonstration-repository.git
    cd demonstration-repository
    git remote add bitbucket ssh://git@stash2.stiltsoft.com:7999/adp/demonstration-repository.git
    • If you use Cron, you’ll need to add a command in your crontab file that will run periodically on a given schedule and will be updating mirrored repositories, e.g.:
    0 0 * * * root /home/mirror/update_repos.sh 

     

    This command will run from the user ‘root’ every day at midnight and trigger a script that has a number of commands to perform update of mirrored repositories.

    For the case when you have one mirrored repository that should be updated, the content of ‘update_repos.sh’ will look like this :

     #!/bin/bash 

    cd /home/mirror/demonstration-repository
    git pull
    git push --all bitbucket
    git push --tags bitbucket

    If you need to mirror more than one repository, include 4 commands you can see above for each repository in ‘update_repos.sh’.

    Related posts

      How to Mirror to Remote Repositories From Bitbucket Data Center and Server

      September 18, 2015
      #How To#Bitbucket#Integration
      11 min

      Visualize, track and analyze repositories hosted in Bitbucket with Awesome Graphs. Benefit from what graphs, charts, and reports can tell you.

       

      Our Stiltsoft team has extensive expertise in Bitbucket Server and Data Center. There’s been a number of posts in our blog about the app we’ve created for Bitbucket users, Awesome Graphs for Bitbucket. In case you haven’t heard about it yet, the plugin helps engineering leaders evaluate team performance, track process improvements, and deliver products faster by transforming invisible development activities into easy-to-understand insights and reports.

      In today’s blog post, we’ll briefly go through some benefits from mirroring Bitbucket Server / Data Center repositories and several ways how to do it.

      Benefits

      Mirroring Bitbucket Data Center / Server repositories can come in very handy when you need to:

      • reduce the load on your system, e.g. when developers and Continuous Integration servers are sharing one Bitbucket instance and are constantly pushing and pulling to/from Bitbucket Server. It can cause resource issues on the Bitbucket server
      • reduce response time for Bitbucket users from different geographical locations
      • have a backup to restore data from if something goes wrong on your Bitbucket instance

      How to mirror Bitbucket Data Center / Server repositories

      You can use one of the following options to set up an automatic update of remote mirrored repositories:

      • Use an add-on for Bitbucket Data Center / Server
      • Use a Continuous Integration tool
      • Use an OS job scheduler

      In our example, we’ll be mirroring a repository from Bitbucket Data Center / Server to Bitbucket Cloud.

      Use an add-on for Bitbucket Data Center / Server

      You can use Repository Mirror Plugin for Bitbucket to automatically mirror to remote repositories.

      To get started, you need to enable the add-on in the ‘Hooks’ section of the repository settings in Bitbucket:

      • click the Mirror Hook text (or the pencil icon) in the Hooks section of the repository settings

      mirror hook bitbucket

      • enter mirror repository URL – the GIT URL (SSH, git, http(s), file) to the remote mirrored repository
      • enter your Bitbucket Cloud user name and the password (if pushing is via HTTP(S))

      mirror hook dialog bitbucket

      • click ‘Enable’

      If using SSH, ensure your private key is installed for the user Bitbucket runs under. Learn how to set up an SSH key and how to troubleshoot SSH issues.

      With mirror hooks, all configured remote mirrors will be receiving changes when a commit or pull request merge is performed.

      Use a Continuous Integration tool

      You can also use some Continuous Integration solutions, e.g. TeamCity, to pull changes from your Bitbucket server and push them to a remote repository. Below is the description of one of the ways how to do that.

      To use SSH authentication, copy an SSH key from the TeamCity server with a build agent you’ll be using. Then add this SSH key in your Bitbucket Cloud and Bitbucket Data Center / Server account settings.

      After that, you need to create a remote repository:

      git clone ssh://git@stash2.stiltsoft.com:7999/adp/demonstration-repository.git
      cd demonstration-repository
      git remote add bitbucket git@bitbucket.org:kkolina/demonstration-repository.git
      

      If you are working with TeamCity and would like to configure automatic updates of mirrored repositories, all you have to do is change the build configuration in TeamCity. You need to add a Command Line Runner build step with a script for updating mirrored repositories that will run every time this build step is invoked.

      teamcity build step

      Example of a script:

      cd /home/mirror/demonstration-repository
      git pull
      git push --all bitbucket
      git push --tags bitbucket
      

      You may also add a VCS Trigger that will add a build to the queue when a VCS check-in is detected in the repository you are mirroring.

      Before adding a trigger, you should attach a VSC root in the Version Control Settings:

      teamcity vcs root

      With the URL of the original repository and Bitbucket Data Center / Server authentication settings:

      teamcity vcs root dialog

      Then you can add a VCS Trigger:

      teamcity

      Use an OS job scheduler

      As another option, it’s possible to use OS job schedulers (Cron, Windows Task Scheduler) to set up an external job and schedule the launch of scripts that will be pulling changes and pushing them to a mirrored repository.

      Again, first, we clone a repository:

      git clone ssh://git@stash2.stiltsoft.com:7999/adp/demonstration-repository.git
      cd demonstration-repository
      git remote add bitbucket git@bitbucket.org:kkolina/demonstration-repository.git
      

      If you use Cron, you’ll need to add a command in your crontab file that will run periodically on a given schedule and will be updating mirrored repositories, e.g.:

      0  0    * * *   root    /mirror/update_repos.sh
      

      This command will run from the user ‘root’ every day at midnight and trigger a script that has a number of commands to perform the update of mirrored repositories.

      For the case when you have one mirrored repository that should be updated, the content of ‘update_repos.sh’ will look like this :

      #!/bin/bash
      
      cd /home/mirror/demonstration-repository
      git pull
      git push --all bitbucket
      git push --tags bitbucket
      

      If you need to mirror several repositories, include 4 commands you can see above for each repository in ‘update_repos.sh’.

      It was a brief overview of this topic to give you a general idea and encourage you to drill into one of these options if you believe you can benefit from it. Visualize, track and analyze repositories hosted in Bitbucket with Awesome Graphs.

      Feel free to comment below.

      You can also read how other teams benefit from using Bitbucket in a bundle with Awesome Graphs:

      Visualized Statistics of Git Repositories in Bitbucket

      June 10, 2015
      #Analytics#Bitbucket
      8 min

      It’s exciting to see Atlassian taking a new step in developing Bitbucket by engaging Atlassian vendors in the creation of apps for this web-based source code management and collaboration solution. Our team is thrilled to be among the pioneers who will be contributing to enriching the functionality of this platform for the benefit of the Bitbucket community. We are happy to introduce Awesome Graphs for Bitbucket Cloud.

      Our goal is to make the functionality of Awesome Graphs available in Bitbucket Cloud. We’ve started with just a couple of graphs, but there’s much more to come.

      Install the app on your workspace using this link and try all the features on your repositories.

      Contributors

      The Contributors graph helps you analyze trends and compare the input from different people in terms of commits and lines of code.

      This graph visualizes the number of commits, lines of code added and deleted in the repository. The cards below the graph represent the personal contributions of each developer with the total number of commits and lines of code made during the time span you select.

      If you want to analyze a specific period of time, click directly on the graph near its starting point and drag it to the end point.

      Commits

      In the Commits tab, you can see the number of commits made over the last year grouped by week and detailed daily commit statistics of the selected week.

      In the upper part, the interactive Commits bar chart displays all commits made over the last year grouped by week. Each bar represents one week. Click a bar to see the total number of commits made that week. You can use arrow keys on your keyboard to navigate from one bar to another. When you click a bar or switch from one bar to another, you see changes on the scatter chart below.

      Punchcard

      In the Punchcard tab, you can see the commit summary by day of the week and hour over the period starting from the first commit in this repository till now. For example, on the screenshot below, the “Thursday 2 pm” circle represents the total number of commits made on all the Thursdays from July 15, 2010, till May 28, 2015, from 2 pm to 3 pm. When you hover over a circle, you can see the exact number of commits made during that hour on that day.

      You can use this data to find out what hours of the week have been the most productive for your team. Moreover, your team can take advantage of this information to meet different goals.

      As an option, it’s useful when planning work or searching for ways to improve team performance. For example, knowing the most productive hours of your team, you might want not to plan any distracting activities for those hours and let everyone focus on work. Or you can take a closer look at those top hours and try to figure out what conditions and factors could have contributed to that efficiency. These insights can help you learn what improves your team’s performance, using this knowledge to optimize working conditions if needed.

      Installing the app

      To install Awesome Graphs, navigate to this link and select the workspace of your choice.

      When the installation is completed, you can start using the app. To view the graphs of the current repository, navigate to the Graphs section on the left-hand sidebar.

      Roadmap

      We’re going to add many more useful graphs to Awesome Graphs for Bitbucket Cloud in the future. For example, graphs on the workspace level and pull requests data.

      If you like to share your ideas or let us know which graphs you’d like us to implement next, please, contact us.

      You can also read how other teams benefit from using Bitbucket in a bundle with Awesome Graphs: