|
| 1 | +--- |
| 2 | +title: Integrate TiDB Cloud Serverless Branching (Beta) with GitHub |
| 3 | +summary: Learn how to integrate the TiDB Cloud Serverless branching feature with GitHub. |
| 4 | +--- |
| 5 | + |
| 6 | +# Integrate TiDB Cloud Serverless Branching (Beta) with GitHub |
| 7 | + |
| 8 | +> **Note:** |
| 9 | +> |
| 10 | +> The integration is built upon [TiDB Cloud Serverless branching](/tidb-cloud/branch-overview.md). Make sure that you are familiar with TiDB Cloud Serverless branching before reading this document. |
| 11 | +
|
| 12 | +If you use GitHub for application development, you can integrate TiDB Cloud Serverless branching into your GitHub CI/CD pipeline, which lets you automatically test your pull requests with branches without affecting the production database. |
| 13 | + |
| 14 | +In the integration process, you will be prompted to install the [TiDB Cloud Branching](https://github.com/apps/tidb-cloud-branching) GitHub App. The app can automatically manage TiDB Cloud Serverless branches according to pull requests in your GitHub repository. For example, when you create a pull request, the app will create a corresponding branch for your TiDB Cloud Serverless cluster, in which you can work on new features or bug fixes in isolation without affecting the production database. |
| 15 | + |
| 16 | +This document covers the following topics: |
| 17 | + |
| 18 | +1. How to integrate TiDB Cloud Serverless branching with GitHub |
| 19 | +2. How does the TiDB Cloud Branching app work |
| 20 | +3. How to build a branching-based CI workflow to test every pull request using branches rather than the production cluster |
| 21 | + |
| 22 | +## Before you begin |
| 23 | + |
| 24 | +Before the integration, make sure that you have the following: |
| 25 | + |
| 26 | +- A GitHub account |
| 27 | +- A GitHub repository for your application |
| 28 | +- A [TiDB Cloud Serverless cluster](/tidb-cloud/create-tidb-cluster-serverless.md) |
| 29 | + |
| 30 | +## Integrate TiDB Cloud Serverless branching with your GitHub repository |
| 31 | + |
| 32 | +To integrate TiDB Cloud Serverless branching with your GitHub repository, take the following steps: |
| 33 | + |
| 34 | +1. In the [TiDB Cloud console](https://tidbcloud.com/), navigate to the [**Clusters**](https://tidbcloud.com/console/clusters) page of your project, and then click the name of your target TiDB Cloud Serverless cluster to go to its overview page. |
| 35 | + |
| 36 | +2. Click **Branches** in the left navigation pane. |
| 37 | + |
| 38 | +3. In the upper-right corner of the **Branches** page, click **Connect to GitHub**. |
| 39 | + |
| 40 | + - If you have not logged into GitHub, you will be asked to log into GitHub first. |
| 41 | + - If it is the first time you use the integration, you will be asked to authorize the **TiDB Cloud Branching** app. |
| 42 | + |
| 43 | + <img src="https://docs-download.pingcap.com/media/images/docs/tidb-cloud/branch/github-authorize.png" width="80%" /> |
| 44 | + |
| 45 | +4. In the **Connect to GitHub** dialog, select a GitHub account in the **GitHub Account** drop-down list. |
| 46 | + |
| 47 | + If your account does not exist in the list, click **Install Other Account**, and then follow the on-screen instructions to install the account. |
| 48 | + |
| 49 | +5. Select your target repository in the **GitHub Repository** drop-down list. If the list is long, you can search the repository by typing the name. |
| 50 | + |
| 51 | +6. Click **Connect** to connect between your TiDB Cloud Serverless cluster and your GitHub repository. |
| 52 | + |
| 53 | + <img src="https://docs-download.pingcap.com/media/images/docs/tidb-cloud/branch/github-connect.png" width="40%" /> |
| 54 | + |
| 55 | +## TiDB Cloud Branching app behaviors |
| 56 | + |
| 57 | +After you connect your TiDB Cloud Serverless cluster to your GitHub repository, for each pull request in this repository, the [TiDB Cloud Branching](https://github.com/apps/tidb-cloud-branching) GitHub App can automatically manage its corresponding TiDB Cloud Serverless branch. The following lists the default behaviors for pull request changes: |
| 58 | + |
| 59 | +| Pull request changes | TiDB Cloud Branching app behaviors | |
| 60 | +|------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 61 | +| Create a pull request | When you create a pull request in the repository, the [TiDB Cloud Branching](https://github.com/apps/tidb-cloud-branching) app creates a branch for your TiDB Cloud Serverless cluster. The branch name is in the `${github_branch_name}_${pr_id}_${commit_sha}` format. Note that the number of branches has a [limit](/tidb-cloud/branch-overview.md#limitations-and-quotas). | |
| 62 | +| Push new commits to a pull request | Every time you push a new commit to a pull request in the repository, the [TiDB Cloud Branching](https://github.com/apps/tidb-cloud-branching) app deletes the previous TiDB Cloud Serverless branch and creates a new branch for the latest commit. | |
| 63 | +| Close or merge a pull request | When you close or merge a pull request, the [TiDB Cloud Branching](https://github.com/apps/tidb-cloud-branching) app deletes the branch for this pull request. | |
| 64 | +| Reopen a pull request | When you reopen a pull request, the [TiDB Cloud Branching](https://github.com/apps/tidb-cloud-branching) app creates a branch for the lasted commit of the pull request. | |
| 65 | + |
| 66 | +## Configure TiDB Cloud Branching app |
| 67 | + |
| 68 | +To configure the behaviors of [TiDB Cloud Branching](https://github.com/apps/tidb-cloud-branching) app, you can add a `tidbcloud.yml` file to the root directory of your repository, and then add the desired configurations to this file according to the following instructions. |
| 69 | + |
| 70 | +### branch.blockList |
| 71 | + |
| 72 | +**Type:** array of string. **Default:** `[]`. |
| 73 | + |
| 74 | +Specify the GitHub branches that forbid the TiDB Cloud Branching app, even if they are in the `allowList`. |
| 75 | + |
| 76 | +```yaml |
| 77 | +github: |
| 78 | + branch: |
| 79 | + blockList: |
| 80 | + - ".*_doc" |
| 81 | + - ".*_blackList" |
| 82 | +``` |
| 83 | +
|
| 84 | +### branch.allowList |
| 85 | +
|
| 86 | +**type:** array of string. **Default:** `[.*]`. |
| 87 | + |
| 88 | +Specify the GitHub branches that allow the TiDB Cloud Branching app. |
| 89 | + |
| 90 | +```yaml |
| 91 | +github: |
| 92 | + branch: |
| 93 | + allowList: |
| 94 | + - ".*_db" |
| 95 | +``` |
| 96 | + |
| 97 | +### branch.autoReserved |
| 98 | + |
| 99 | +**Type:** boolean. **Default:** `false`. |
| 100 | + |
| 101 | +If it is set to `true`, the TiDB Cloud Branching app will not delete the TiDB Cloud Serverless branch that is created in the previous commit. |
| 102 | + |
| 103 | +```yaml |
| 104 | +github: |
| 105 | + branch: |
| 106 | + autoReserved: false |
| 107 | +``` |
| 108 | + |
| 109 | +### branch.autoDestroy |
| 110 | + |
| 111 | +**Type:** boolean. **Default:** `true`. |
| 112 | + |
| 113 | +If it is set to `false`, the TiDB Cloud Branching app will not delete the TiDB Cloud Serverless branch when a pull request is closed or merged. |
| 114 | + |
| 115 | +```yaml |
| 116 | +github: |
| 117 | + branch: |
| 118 | + autoDestroy: true |
| 119 | +``` |
| 120 | + |
| 121 | +## Create a branching CI workflow |
| 122 | + |
| 123 | +One of the best practices for using branches is to create a branching CI workflow. With the workflow, you can test your code using a TiDB Cloud Serverless branch instead of using the production cluster before merging the pull request. You can find a live demo [here](https://github.com/shiyuhang0/tidbcloud-branch-gorm-example). |
| 124 | + |
| 125 | +Here are the main steps to create the workflow: |
| 126 | + |
| 127 | +1. [Integrate TiDB Cloud Serverless branching with your GitHub repository](#integrate-tidb-cloud-serverless-branching-with-your-github-repository). |
| 128 | + |
| 129 | +2. Get the branch connection information. |
| 130 | + |
| 131 | + You can use the [wait-for-tidbcloud-branch](https://github.com/tidbcloud/wait-for-tidbcloud-branch) action to wait for the readiness of the TiDB Cloud Serverless branch and get the connection information of the branch. |
| 132 | + |
| 133 | + Example usage: |
| 134 | + |
| 135 | + ```yaml |
| 136 | + steps: |
| 137 | + - name: Wait for TiDB Cloud Serverless branch to be ready |
| 138 | + uses: tidbcloud/wait-for-tidbcloud-branch@v0 |
| 139 | + id: wait-for-branch |
| 140 | + with: |
| 141 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 142 | + public-key: ${{ secrets.TIDB_CLOUD_API_PUBLIC_KEY }} |
| 143 | + private-key: ${{ secrets.TIDB_CLOUD_API_PRIVATE_KEY }} |
| 144 | +
|
| 145 | + - name: Test with TiDB Cloud Serverless branch |
| 146 | + run: | |
| 147 | + echo "The host is ${{ steps.wait-for-branch.outputs.host }}" |
| 148 | + echo "The user is ${{ steps.wait-for-branch.outputs.user }}" |
| 149 | + echo "The password is ${{ steps.wait-for-branch.outputs.password }}" |
| 150 | + ``` |
| 151 | + |
| 152 | + - `token`: GitHub will automatically create a [GITHUB_TOKEN](https://docs.github.com/en/actions/security-guides/automatic-token-authentication) secret. You can use it directly. |
| 153 | + - `public-key` and `private-key`: The TiDB Cloud [API key](https://docs.pingcap.com/tidbcloud/api/v1beta#section/Authentication/API-Key-Management). |
| 154 | + |
| 155 | +3. Modify your test code. |
| 156 | + |
| 157 | + Modify your test code to accept the connection information from GitHub Actions. For example, you can accept the connection information through the environment, as demonstrated in the [live demo](https://github.com/shiyuhang0/tidbcloud-branch-gorm-example). |
| 158 | + |
| 159 | +## What's next |
| 160 | + |
| 161 | +Learn how to use the branching GitHub integration with the following examples: |
| 162 | + |
| 163 | +- [branching-gorm-example](https://github.com/tidbcloud/branching-gorm-example) |
| 164 | +- [branching-django-example](https://github.com/tidbcloud/branching-django-example) |
| 165 | +- [branching-rails-example](https://github.com/tidbcloud/branching-rails-example) |
| 166 | + |
| 167 | +You can also build your branching CI/CD workflow without the branching GitHub integration. For example, you can use [`setup-tidbcloud-cli`](https://github.com/tidbcloud/setup-tidbcloud-cli) and GitHub Actions to customize your CI/CD workflows. |
0 commit comments