How to integrate Teamcity and GitLab CI

I note down the step I did to integrate TeamCity build server and GitLab CI pipeline. I am using TeamCity 2017.1.15 and GitLab 10.0.3.

What you will get with this integration:

  • Small green tick for each commit showing that a build has run successfully on the commit
  • Bigger tick for each merge request (MR) showing that a build has run successfully on the head of the merge request
  • Ability to enforce a successful build before any MR can be merged

How to set up the integration

Tldr, you need to go through 3 main steps:

  1. Enable TeamCity integration in your GitLab project settings
  2. Enable TeamCity build configuration to monitor and trigger build for all (CI) branches
  3. Enable “Commit status publisher” build feature

You can continue below to get more details on the steps. Luckily, most the integration is already built-in, although not all works as expected.

  1. Enable TeamCity integration in your GitLab project settings
    1. Open Settings > Integration
    2. Fill in TeamCity URL (server URL), Build type (build configuration ID), username and password
    3. Tick Active and Trigger
  2. Enable TeamCity build configuration to monitor and trigger build for all (CI) branches
    1. Edit VCS Root, make sure Branch specification include +:refs/heads/(*) and +:/refs/(merge-requests/*)/head (don’t set it to +:*, which makes TeamCity confused about the branch name sent from GitLab)
    2. You don’t need to add any trigger in TeamCity, GitLab will automatically trigger a build for each push
  3. Enable Commit status publisher build feature
    1. Edit Build Configuration > Build Features, add Commit status publisher
    2. Choose VCS Root as All attached VCS Roots, choose Publisher as GitLab and fill in required GitLab information (you have to use an account with Developer permission in the GitLab project)

You have completed the setup. Right now if you push any change to GitLab repository, TeamCity will pick up the change, start the build and report the result back to GitLab.

Note

  • Although GitLab has refs/merge_requests/#/head, you don’t need to monitor this branch in TeamCity. The branch of the MR can be built instead.
  • refs/merge_requests/#/head does not work for MR from fork. You will still see the MR in TeamCity, but the build result will not be recorded back in GitLab.
  • GitLab does not seem to have something like refs/merge_requests/#/merge just yet.

Links

https://gitlab.com/gitlab-org/gitlab-ce/blob/v10.0.4/app/models/project_services/teamcity_service.rb

https://confluence.jetbrains.com/display/TCD10/Commit+Status+Publisher

 

4 thoughts to “How to integrate Teamcity and GitLab CI”

  1. Hi,

    Interesting article, we have tried it and we can’t make it work for merge requests, we are using:
    “TeamCity 2017.2.3 (build 51047), effective release date 2017-11-27, currently running in the professional mode.
    Maximum number of authorized agents: 3 agents provided by professional edition license
    Maximum number of build configurations: 100 build configurations provided by professional edition license ”
    And gitLab as a hosted service.

    We don’t see the option mentioned for Git lab “Tick Active and Trigger”. Do we need to install git lab in our premises ?

    Any advice would be more than welcomed.

    1. In newer version of GitLab (both on GitLab.com and on premise), it seems that they removed the Trigger checkbox. You just need to tick Active as the screenshot below.

      The Trigger checkbox was also confusing to me. I am glad they removed it. Please let me know if you still have any difficulty.

  2. Thanks on the comment. As specified in the mentioned article, we needed to add this into the VCS root:

    “+:refs/heads/(*) and +:/refs/(merge-requests/*)/head”
    This actually gets the merge requests available as branches to be built from.

    Well we sorted out by creating a trigger with a slightly different format: “+:merge-requests/*”

    This triggers the build when a merge request.

    We haven’t been able to make the trigger from gitlab.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.