How to Use the Space Git Flow With TeamCity
Tags: cicd
Published: 2023-04-27 15:44:45

Do you use feature branches or Git flow to ensure code quality?

The powerful combination of JetBrains Space Git flow with TeamCity offers a complete solution to help you achieve better code quality and keep your main branch green with stable builds. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6o6xkig27qyo4dlcmgmj.png)

[Space](https://www.jetbrains.com/space/?_ga=2.35286884.416760693.1682323121-1928317243.1667914633&_gl=1*114w577*_ga*MTkyODMxNzI0My4xNjY3OTE0NjMz*_ga_9J976DJZ68*MTY4MjYwMzk4NC4xODAuMS4xNjgyNjA5NzU1LjIzLjAuMA..) provides you with Git hosting, code reviews, and quality gates, while [TeamCity](https://www.jetbrains.com/teamcity) offers a build pipeline. The native integration between tools will save you time and effort by ensuring you have a complete and well-integrated flow with a unified UI. 

Adopting the Space Git flow with TeamCity is easy and allows you to keep your main branch stable while controlling changes automatically. 

In this blog post, we’ll introduce the combo of the Space Git flow and TeamCity, and guide you through the process of using it in your project.

## What Is the Space Git Flow, and Why Use it?
The Space Git flow is a branching strategy that is similar to GitHub flow, but with a greater emphasis on safety when introducing changes to the main branch and the ability to scale to large projects and teams.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ly3j3qguayhnwq0t0bl4.png)

Getting started with the Space Git flow is easy and doesn’t require going all-in, as you can mirror your existing Git repository without having to migrate, and you can switch back any time.

Using the Space Git flow integrated with TeamCity allows you to:

- 
Achieve higher-quality code and a stable main branch by configuring quality gates based on TeamCity build status for merge requests.

- 
Introduce a code review process your team will love, integrated with IntelliJ IDEA and available on the go with Space iOS and Android apps.
[Read this article](https://blog.jetbrains.com/space/2023/04/18/space-git-flow/) to learn more about the Space Git flow.

## How to Use the Space Git Flow With TeamCity
**Mirror or host your Git repository in Space**
Start by creating bi-directional mirrors of your repository from GitHub or another Git service and link it to Space in a few clicks. 

You can also use Space to host your source code privately. If you host it in Space, you can take advantage of the web-based interface and JetBrains IDE integration for a seamless development experience.

###Protect your main branch from accidental pushes
To prevent accidental pushes to the main branch, you can set up branch protection in Space. This helps you protect repositories from unauthenticated commits, restrict who can push or merge changes into the branch, and prevent accidental branch deletion.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6ju5rc5vy8g6m4q3nk0.png)

Once you’ve set up branch protection, the only way a change can make it to main is through a merge request.

###Create a feature branch
To start working on a new feature, you’ll need to either clone the repository to your machine, or use [remote development](https://blog.jetbrains.com/space/2021/11/29/introducing-remote-development-with-space/) with the IDE hosted on a virtual machine in the Space cloud.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2weard7x13kyjp0gp0u.png)

With the Space Git flow, you make changes in a separate feature branch and then request a code review before merging it back to main. From your IDE, you can make changes, add a commit message, and then push this branch back to Space. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x7clyls6i6iyj3d3tme1.png)

### Create a merge request
Create a merge request to begin the process of moving your new code into main. You can do this from within your IDE using the native integration, or you can find your branch in Space and create a merge request from there.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vrwvjqdzwk2ra86d64o8.png)

You can create a title and description for your merge request and see the commits that will be part of it. The quality gates configured in Space require at least one person to review these changes.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d72rnanriv221sczh85z.png)

[Quality gates](https://www.jetbrains.com/help/space/branch-and-merge-restrictions.html?_ga=2.224707423.416760693.1682323121-1928317243.1667914633&_gl=1*25a72i*_ga*MTkyODMxNzI0My4xNjY3OTE0NjMz*_ga_9J976DJZ68*MTY4MjYwMzk4NC4xODAuMS4xNjgyNjA5NDc3LjYwLjAuMA..#quality-gates-for-merge-requests) are a mandatory and customizable set of conditions that need to be fulfilled before merging. Apart from code reviewer’s approval, you can set up a Space Automation job and a TeamCity check as gates.

###Space will require a review based on code ownership**
In your repository, you can create a special file named CODEOWNERS that specifies who is responsible for specific folders and files. Later, you and your colleagues will have the option to select a code owner to review your changes as a part of quality gates.

###Wait for your team to review the changes **
Now you wait for your colleagues to review the changes. They can add comments and suggestions, or they can simply approve the request. With turn-based code reviews, Space makes it easy for both the author and the reviewer to understand whose turn it is to take action.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xrr4yi3fv6iwd1c8248d.png)

Space allows you to collect your comments as drafts and send them in batches so as not to overload your colleagues with unnecessary notifications.  

After you’ve agreed on changes with your reviewer, you can proceed with merging them.

Ensure your build is green 
With Space and TeamCity, there are a few options that you can use to make sure your build is green before merging your changes. You can use either or both of them for extra security.

### Run CI/CD checks
After you create a merge request, Space Automation and/or TeamCity CI jobs are triggered to validate the changes by building the code and running tests. As part of quality gates, the changes will be merged into main only if a CI/CD server can successfully build the feature branch. 

The checks can be set and completed by both Space Automation and TeamCity.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zldw4qn3f7uknsz0ey6b.png)

These checks, however, only ensure the new code is valid and integrates well with the main branch at the time of branching. With Safe Merge, validation is done by attempting to integrate the new code with the very latest code from the main branch.

###Run Safe Merge 
You can use Safe Merge before merging the changes directly into main. Safe Merge acts like a time machine – it runs a preview of what your main branch would look like with the changes. 

This can be especially useful for large projects because while you were working on a feature branch, the main branch could have received changes that conflict with your work. Safe Merge allows you to catch these conflicts before actually merging the branches.

Space creates a temporary commit with the latest changes, which can then be used to perform the required quality checks using an Automation job or TeamCity build.

In TeamCity, you’ll see Space trigger a special build, and TeamCity will report the build status back to Space when it’s finished. The changes are merged into the main branch only if the build succeeds. Otherwise, the merge is rejected.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e5iwabyft2r2gxn1wgon.png)