|
| 1 | +--- |
| 2 | +title: Database GitOps with Azure DevOps Pipeline |
| 3 | +author: Adela |
| 4 | +updated_at: 2025/03/28 18:00 |
| 5 | +tags: Tutorial |
| 6 | +integrations: 'API, Azure DevOps' |
| 7 | +level: Advanced |
| 8 | +estimated_time: '40 mins' |
| 9 | +description: 'Learn the new GitOps workflow for database release with Bytebase.' |
| 10 | +--- |
| 11 | + |
| 12 | +This is one of our database GitOps series with Bytebase: |
| 13 | + |
| 14 | +- [Database GitOps with GitHub Actions](/docs/tutorials/gitops-github-workflow) |
| 15 | +- Database GitOps with Azure DevOps Pipeline (this one) |
| 16 | +- Database GitOps with GitLab CI (TBD) |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +This tutorial shows you how to build an database GitOps workflow using Azure DevOps Pipeline and Bytebase API. You'll learn to: |
| 21 | + |
| 22 | +1. Create a streamlined database release workflow where you can: |
| 23 | + |
| 24 | + - Submit SQL migrations through Azure DevOps |
| 25 | + - Automatically run SQL reviews on pull requests |
| 26 | + - Auto-create and deploy Bytebase releases when merging to `main` |
| 27 | + |
| 28 | +While we use Azure DevOps Pipeline in this guide, you can apply these concepts to other CI platforms like [GitHub Actions](/docs/tutorials/gitops-github-workflow), GitLab CI, or Bitbucket Pipelines using the Bytebase API. |
| 29 | + |
| 30 | +<HintBlock type="info"> |
| 31 | + |
| 32 | +This tutorial code repository is at [https://dev.azure.com/bytebase-hq/_git/bytebase-example](https://dev.azure.com/bytebase-hq/_git/bytebase-example) |
| 33 | + |
| 34 | +</HintBlock> |
| 35 | + |
| 36 | +## Prerequisites |
| 37 | + |
| 38 | +- [Docker](https://www.docker.com/) installed |
| 39 | +- An [ngrok](https://ngrok.com/) account |
| 40 | + |
| 41 | +## Automatic Rollout across environments |
| 42 | + |
| 43 | +### Step 1 - Start Bytebase with ngrok |
| 44 | + |
| 45 | +<IncludeBlock url="/docs/get-started/install/vcs-with-ngrok"></IncludeBlock> |
| 46 | + |
| 47 | +### Step 2 - Create Service Account |
| 48 | + |
| 49 | +<IncludeBlock url="/docs/share/tutorials/create-service-account"></IncludeBlock> |
| 50 | + |
| 51 | +If you have **Enterprise Plan**, you can create a **Custom Role** for the service account which require fewer permissions, and assign this role instead of DBA: |
| 52 | + |
| 53 | + - plans.create |
| 54 | + - plans.get |
| 55 | + - plans.preview |
| 56 | + - releases.check |
| 57 | + - releases.create |
| 58 | + - releases.get |
| 59 | + - rollouts.create |
| 60 | + - rollouts.get |
| 61 | + - rollouts.list |
| 62 | + - sheets.create |
| 63 | + - sheets.get |
| 64 | + - taskRuns.create |
| 65 | + - planCheckRuns.list |
| 66 | + - planCheckRuns.run |
| 67 | + |
| 68 | +### Step 3 - Configure SQL Review in Bytebase |
| 69 | + |
| 70 | +Since you will need to run SQL review on your PRs, you need to configure the SQL review in Bytebase. |
| 71 | + |
| 72 | +1. Go to **CI/CD** > **SQL Review**, click **Create SQL Review**. |
| 73 | + |
| 74 | +1. Select the `Sample Template` and click **Next**. |
| 75 | +  |
| 76 | + |
| 77 | +1. Select `Prod` environment as the attached resources and click **Confirm**. Now the SQL review is enabled for the `Prod` environment. |
| 78 | +  |
| 79 | + |
| 80 | +### Step 4 - Copy from the Example Repository and Configure Variables |
| 81 | + |
| 82 | +1. Create a new project. Copy `pipelines` folder from [https://dev.azure.com/bytebase-hq/_git/bytebase-example](https://dev.azure.com/bytebase-hq/_git/bytebase-example). There are two workflows in this repository: |
| 83 | + |
| 84 | + - `pipelines/check-release.yml`: [Lint the SQL](/docs/sql-review/overview/) migration files after the PR is created. |
| 85 | + - `pipelines/rollout-release.yml`: Create a release in Bytebase after the PR is merged to the `main` branch. |
| 86 | + |
| 87 | +1. Go into `pipelines/check-release.yml` and `pipelines/rollout-release.yml`. In the `env` section, replace the variable values with your own and commit the changes. |
| 88 | + |
| 89 | + - **BYTEBASE_URL**: your ngrok url |
| 90 | + - **BYTEBASE_SERVICE_ACCOUNT **: `[email protected]` (the service account you created in the previous step) |
| 91 | + - **BYTEBASE_PROJECT**: `projects/project-sample` (the sample project in the Bytebase) |
| 92 | + - **BYTEBASE_TARGETS**: `instances/test-sample-instance/databases/hr_test,instances/prod-sample-instance/databases/hr_prod` (the two default databases in the sample project) |
| 93 | + - **FILE_PATTERN**: `migrations/*.sql` (the pattern of the migration files) |
| 94 | + |
| 95 | +1. Go to branch policy for `main` branch, add `check-release` as a required check. |
| 96 | + |
| 97 | +  |
| 98 | + |
| 99 | +  |
| 100 | + |
| 101 | +### Step 5 - Create the migration files |
| 102 | + |
| 103 | +To create migration files to trigger release creation, the files have to match the following pattern: |
| 104 | + |
| 105 | +- A migration file should start with digits, which is also its version. e.g. `202503131500_create_table_t1_ddl.sql`. |
| 106 | +- A migration file may end with 'ddl' or 'dml' to indicate its change type. If it doesn't end with any of the two, its change type is DDL by default. |
| 107 | + |
| 108 | +1. Within your forked repository, create the following migration files under `migrations` directory: |
| 109 | + |
| 110 | + - 202503131500_create_table_t1_ddl.sql |
| 111 | + |
| 112 | + ```sql |
| 113 | + CREATE TABLE t1 ( |
| 114 | + id SERIAL PRIMARY KEY, |
| 115 | + name TEXT |
| 116 | + ); |
| 117 | + ``` |
| 118 | + |
| 119 | +1. Commit to a new branch and create a pull request, the `check-release` workflow will be triggered. There will be a warning in the SQL review result. |
| 120 | + |
| 121 | +  |
| 122 | + |
| 123 | +  |
| 124 | + |
| 125 | +1. According to the SQL review result, you can do some changes to the SQL files and push to the branch. Then you should see the SQL review has passed. There are no warnings in the SQL review result. |
| 126 | + |
| 127 | + ```sql |
| 128 | + CREATE TABLE t1 ( |
| 129 | + id SERIAL PRIMARY KEY, |
| 130 | + name TEXT NOT NULL |
| 131 | + ); |
| 132 | + ``` |
| 133 | + |
| 134 | +  |
| 135 | + |
| 136 | +1. When the SQL review is passed, you can merge the pull request. The `rollout-release` workflow will be triggered to create a **release** in Bytebase and then roll out automatically. |
| 137 | + |
| 138 | +  |
| 139 | +  |
| 140 | +  |
| 141 | + |
| 142 | +1. If you click the test stage and expand the different sections, you can follow the links to Bytebase. |
| 143 | + |
| 144 | +  |
| 145 | + |
| 146 | +## Summary |
| 147 | + |
| 148 | +Now you have learned how to database GitOps with Azure DevOps Pipeline. If you want to trigger a release creation with other git providers (e.g. GitLab, Bitbucket, [GitHub Actions](/docs/tutorials/gitops-github-workflow)), you may customize the workflow file. |
0 commit comments