Database CI/CD and Schema Migration with MySQL and GitHub
A series of articles about Database CI/CD and Schema Migration with MySQL
- Database CI/CD and Schema Migration with MySQL
- Database CI/CD and Schema Migration with MySQL and GitHub (this one)
Overview
In the last article Database CI/CD and Schema Migration with MySQL, you have tried UI workflow in Bytebase.
This tutorial will bring your MySQL schema change to the next level by introducing the GitOps workflow, where you commit the schema change script to the GitHub repository, which will in turn trigger the schema deployment pipeline in Bytebase.
You can use Bytebase's Community Plan to finish the tutorial.
Prerequisites
Before you start this tutorial, make sure you have the following ready:
- Finished our previous UI-based change tutorial Database CI/CD and Schema Migration with MySQL.
- A local MySQL server.
- A GitHub account and a public GitHub repository, e.g.
test-bb-gitops
. - Docker installed locally.
- An ngrok account.
Step 1 - Run Bytebase in Docker and set the External URL generated by ngrok
ngrok is a reverse proxy tunnel, and in our case, we need it for a public network address in order to receive webhooks from VCS. ngrok we used here is for demonstration purposes. For production use, we recommend using Caddy.
-
Run Bytebase in Docker with the following command:
-
Bytebase is running successfully in Docker, and you can visit it via
localhost:8080
. Register an admin account and it will be granted theworkspace admin
role automatically. -
Login to ngrok Dashboard and complete the Getting Started steps to install and configure. If you want to use the same domain each time you launch ngrok, go to Cloud Edge > Domains, where you'll find the domain
<<YOURS>>.ngrok-free.app
linked to your account. -
Run the ngrok command
ngrok http --domain=<<YOURS>>.ngrok-free.app 8080
to start ngrok with your specific domain, and you will see the output displayed below: -
Log in Bytebase and click the gear icon (Settings) on the top right. Click General under Workspace. Paste
<<YOURS>>.ngrok-free.app
as External URL under Network section and click Update. -
Now you can access Bytebase via
<<YOURS>>.ngrok-free.app
.
Step 2 - Find your MySQL in Bytebase
-
Visit Bytebase Console through the browser via your ngrok URL. Log in using your admin account.
-
Create one or two new databases on your Spanner instances for different environments, refer to previous tutorial if you need help.
Step 3 - Connect Bytebase with GitHub.com
-
Go to Bytebase homepage, and click Integration > GitOps on the left sidebar. Choose
GitHub.com
as Git provider. What we need is a github personal access token. -
Go to your GitHub account. Click your avatar and then click Settings on the menu. Click Developer settings on the left sidebar, and then click Personal access tokens > Fine-grained tokens.
-
Click Generate new token, fill in the fields and check the scopes according to the description on Bytebase. Click Generate token.
-
Copy the token and paste it back into Bytebase Integration > GitOps. Click Confirm and add.
Step 4 - Enable GitOps workflow with MySQL
-
Go to the project
Sample Project
, click Integration > GitOps. Click Add Enable GitOps connector. -
Choose
GitHub.com
- the provider you just added. It will display all the repositories you can manipulate. Choosetest-bb-gitops
. -
Keep the default setting, and click Finish. Pay attention to Database Group, which is the database group that the schema change will be applied to. With Community Plan, the changes will automatically affect all databases in the project. With Enterprise Plan, you'll have the option to specify the target database group.
Step 5 - Change schema for MySQL by pushing SQL schema change files to GitHub
-
In your GitHub repository
test-bb-gitops
, create a folderbytebase
, then create an sql file202404151600_create_table_t1.sql
.Paste the sql script in it.
-
Create a new branch for this commit and start a pull request. Click Merge pull request to merge the new branch into the main branch.
-
There will be a comment saying there's a rollout in Bytebase. Click and go to the issue page, you’ll see
-
The issue is created via GitHub.com, there's a link to the GitHub commit.
-
The SQL is exactly the one we have committed to the GitHub repository.
-
The SQL has passed the automatic task checks and rollout automatically.
-
Since there're two databases in the project, Bytebase creates a 2-staged pipeline to roll out the change sequentially.
-
-
Click View change, you can view the schema diff.
Summary and What's Next
Now you have tried out GitOps workflow, which will store your database schema in GitHub and trigger the change upon committing the change to the repository via Pull Request, to bring your database change workflow to the next level of Database DevOps - Database as Code.
If the built-in workflow is not suitable, you can opt to Bytebase API to fully customize the workflow to integrate with your CI pipeline. Automating Database Schema Change workflow Using GitHub Actions is an example.