Optimize Software Deployment: How TeamCity and Mergify Streamline CI/CD Pipelines

Optimize Software Deployment: How TeamCity and Mergify Streamline CI/CD Pipelines

CI/CD is more than just a current trend in the software development landscape. It's an essential element of a streamlined development cycle. Two tools often linked with these methodologies are TeamCity and Mergify.

Julien Danjou

The adoption of continuous integration (CI) and continuous deployment (CD) is more than just a current trend in the software development landscape. They are essential elements of a streamlined development cycle. Two tools often linked with these methodologies are TeamCity and Mergify. TeamCity, a powerful build management and CI server developed by JetBrains, automates the building, testing, and deployment of your software. On the other side, Mergify is a robust GitHub automation tool and merge queue that can simplify the management of pull requests.

Setting Things Up

Before we delve into the advantages of combining TeamCity and Mergify, let's take a brief look at setting them up.

To kickstart with TeamCity, you need to install it on your server. Afterward, you can leverage its extensive plugins and features to design your build, test, and deployment pipelines.

Mergify, contrastingly, doesn't require any installation. You simply integrate it with your GitHub account, and then define your automation rules in a YAML file housed in your repository.

The Power Couple: TeamCity and Mergify

Individually, TeamCity and Mergify are robust tools. Still, when combined, they can supercharge your DevOps workflows. TeamCity's extensive build and test automation capabilities pair perfectly with Mergify's GitHub workflow automation capabilities, such as merging, labeling, backporting, and more.

By integrating TeamCity and Mergify, you're crafting a powerful CI/CD pipeline. You're enabling automatic software build, test, and deployment while also efficiently managing various GitHub workflows.

Automating Pull Request Updates with TeamCity and Mergify

A notable example of TeamCity and Mergify's synergistic power is in automating pull request updates. Typically, if a TeamCity build fails due to a commit, a team member must notify the developer to update their pull request manually.

With Mergify, you can fully automate this process. Using Mergify's pull request rules, you can configure a rule that will automatically request changes and comment on the pull request if the TeamCity build fails. This saves time and ensures that issues are quickly identified and resolved.

Step 1: Configure TeamCity Build Status Reporting

First, ensure TeamCity reports the build status back to GitHub. TeamCity offers built-in support for reporting the build status to GitHub. Navigate to the VCS Roots settings of your TeamCity project and provide your GitHub repository details. Then, enable the "reporting of the build status to GitHub" option.

Step 2: Set Up Mergify Rules

Next, in your GitHub repository, create a .mergify.yml file where you'll define your automation rules. Here's an example of a rule that comments on and labels a pull request if the TeamCity build fails:

pull_request_rules:
  - name: comment on PR if build fails
    conditions:
      - check-failure=TeamCity
    actions:
      comment:
        message: "The TeamCity build has failed. Please review your changes."
      label:
        toggle:
          - build-failed

In this configuration:

  • The conditions section checks if the TeamCity build fails.
  • The actions section defines what actions Mergify should take if the conditions are met. In this case, it will add a comment to the pull request and apply the label build-failed.

If you wanted to also update the pull request with the latest change from its base branch to see if that'd make it work, you could use the update action from Mergify as well:

pull_request_rules:
  - name: comment and update on PR if build fails
    conditions:
      # Replace with the actual name of your TeamCity reported check
      - check-failure=TeamCity
    actions:
      comment:
        message: "The TeamCity build has failed. Please review your changes."
      label:
        add:
          - build-failed
      # Merge PR base branch into the PR, this will retrigger the CI
      update:

In this configuration, we are extending our previous rule to include the update action from Mergify. The update action automatically updates the pull request with the latest changes from the base branch when the TeamCity build fails. This triggers a new build in TeamCity and can sometimes help fix build issues related to out-of-date branches.

Finally, commit the updated .mergify.yml file to your repository. Mergify will now start automating based on the newly defined rules.

Ensuring Secure Merges with TeamCity and Mergify using the Merge Queue

Another efficient way to leverage TeamCity and Mergify is by using Mergify's Merge Queue feature. The Merge Queue ensures that the main branch's integrity is always maintained by validating each pull request before merging.

With TeamCity as your continuous integration (CI) system, you can guarantee thorough checks of each pull request before it gets added to the merge queue. Here's how you can configure this:

Step 1: Set Up TeamCity for CI

Ensure your TeamCity setup correctly reports the build status to GitHub, as explained previously. Each pull request should trigger a TeamCity job, which subsequently updates the pull request status based on the build result.

Step 2: Configure Mergify Merge Queue

Next, in your .mergify.yml file, you can set up a rule that places the pull request in a merge queue once it's approved and all CI checks, including TeamCity, pass. Here's an example configuration:

queue_rules:
  - name: default
    merge_conditions:
      - "#approved-reviews-by>=1"
      - check-success=TeamCity
    routing_conditions:
      - "#approved-reviews-by>=1"
      - check-success=TeamCity

In this configuration, the queue_rules section sets up a merge queue named default. A pull request is added to this queue when it has at least one approval (#approved-reviews-by>=1) and the TeamCity check is successful (check-success=TeamCity).

To add a pull request to the queue simply type @mergify queue in a comment.

The pull request is merged once it's updated and still has TeamCity checks passing, plus the required approval.

Step 3: Commit and Push .mergify.yml File

After updating the .mergify.yml file with the new rules, commit and push the file to your repository.

With this setup, each pull request must pass the TeamCity build and receive approval before Mergify adds it to the merge queue. Only when the pull request at the front of the queue passes all checks after being updated, it gets merged, ensuring the main branch's integrity.

By integrating TeamCity's robust CI capabilities with Mergify's merge queue, your development workflow can be more efficient, save your team valuable time, and reduce the risk of breaking your main branch.

Conclusion: Amplify Your TeamCity Workflow with Mergify

With TeamCity managing the building, testing, and deploying of your software, and Mergify streamlining your GitHub workflows, you have a well-tuned CI/CD pipeline. This combination allows for workflow automation, improved efficiency, and, consequently, better software delivery.

By adopting tools like TeamCity and Mergify, you are not only aligning with modern DevOps practices but also bolstering the long-term productivity and success of your software development projects.

So, are you ready to amplify your TeamCity workflow with Mergify?