The Database CI/CD Best Practice with Bitbucket

Estimated: 30 mins
The Database CI/CD Best Practice with Bitbucket

Wanna other VCS providers instead? 👉

The Database CI/CD Workflow

Database change is a tricky part of the application development process: it usually involves multiple databases from different environments and cross-team collaboration, to add on top of it, databases are touch and go. It got us thinking: can we treat database the same way we treat application code?

DORA (DevOps Research & Assessment) pointed out that integrating database work into the software delivery process positively contributes to continuous delivery. It’s about time to make databases a part of the CI/CD cycle.

But how does it work, really? Here, we present our envisioned Database CI/CD workflow.

database-devops-workflow

  1. The developer creates a Pull Request containing the SQL script;
  2. SQL Review CI is automatically triggered to review SQL and offers suggestions via a comment in the PR;
  3. The team leader or another peer on the dev teams approves the change and merges the SQL script into the watched branch (default is the main branch);
  4. The merge event automatically triggers the rollout pipeline in Bytebase and creates a ticket capturing the intended change;
  5. (Optional) an approval flow will be auto matched based on the change risk and be followed via Bytebase’s built-in UI;
  6. Approved scripts are executed gradually according to the configured rollout stages;
  7. When the rollout is completed, Bitbucket CI may get notified and proceed to deploy the application.

Set Up Database CI/CD with Bitbucket in Bytebase (Free Plan)

Here's a step-by-step tutorial on how to set up this Database CI/CD with Bitbucket in Bytebase.

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.

ngrok-reverse-proxy

  1. Run Bytebase in Docker with the following command:

    docker run --rm --init \
      --name bytebase \
      --publish 8080:8080 --pull always \
      --volume ~/.bytebase/data:/var/opt/bytebase \
      bytebase/bytebase:3.1.0
  2. Bytebase is running successfully in Docker, and you can visit it via localhost:8080. Register an admin account and it will be granted the workspace admin role automatically.

  3. 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.

  4. 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:

    terminal-ngrok

  5. 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.

    external-url

  6. Now you can access Bytebase via <<YOURS>>.ngrok-free.app.

Step 2 - Add Bitbucket.org as a Git provider in Bytebase

  1. Visit Bytebase via your ngrok URL. Click CI/CD > GitOps, choose Bitbucket.org.

    bb-gitops-bitb

  2. Follow the app password link to https://bitbucket.org/account/settings/app-passwords/ and click Create app password. Fill in the Label, then check the following fields and click Create.

    • Account (Read)
    • Workspace membership (Read)
    • Projects (Read)
    • Webhooks (Read and Write)
    • Repositories (Read and Write)
    • Pull requests (Read and Write)

    bitb-app-pwd

  3. Copy the generated password, go back to Bytebase, and paste the password into the field with your bitbucket username and click Confirm and add. The provider is added successfully.

Step 3 - Configure a GitOps Workflow in Bytebase

  1. Go to bitbucket.org, under workspace bytebase-demo, create a new project bb-gitops-2024 and a repository bb-test.

  2. Go to Bytebase, go to the Sample Project. Click Integration >GitOps on the left and click Add GitOps connector. Choose Bitbucket.org 2024 (the git provider you just configured) and bytebase-demo/bb-test (the repository you just created).

  3. Keep the default settings for the remaining fields and click Finish. The gitops connector is created successfully.

    bb-gitops-bitb-configure

Step 4 - Configure SQL Review in Prod

  1. Go to CI/CD > SQL Review in workspace, choose Prod as the environment. Make sure a SQL review policy is attached and enabled on Prod.

    bb-sql-review

  2. Click Edit, click PostgreSQL tab. Make sure Enforce "NOT NULL" constraints on columns is enabled. This is to make sure the SQL Review can work.

    bb-sql-review-not-null

Step 5 - Create a Pull Request to trigger issue creation

  1. Go to bb-test on Bitbucket. Add a new file 20240919_create_table_t2024.sql under bb-test/bytebase/ which is the directory configured in the previous step. Copy the following SQL script into the file and commit the change via a new branch which will create a Pull Request automatically.

    CREATE TABLE "public"."t2024" (
          "id" integer PRIMARY KEY,
          "name" text
    );
  2. Wait for a while, there is a SQL Review comment added. As we configured in the previous step, NOT NULL is a warning level SQL Review rule.

    bitb-sql-review-warning

  3. Edit our sql file as following and commit it on the same branch, and merge the PR.

    CREATE TABLE "public"."t2024" (
          "id" integer NOT NULL PRIMARY KEY,
          "name" text NOT NULL
    );
  4. There will be a new comment saying the PR has triggered a Bytebase rollout.

    bitb-merged

  5. Follow the link to go to Bytebase. There's an issue with two stages, this is because we have two databases in this project, by default, the SQL will be applied to all databases within the project. If you merge the previous version SQL script, the SQL Review task run here will show yellow warning and waiting for rollout. Click Resolve to resolve the issue.

    bb-issue-rollout

  6. After the rollout completes, click View change to see the diff.

    bb-view-change-diff

  7. You may also go to a specific database page to view all its change history.

    bb-db-change-history

Advanced Features (Enterprise Plan)

You may upgrade to Enterprise plan to explore more features.

Manual Rollout

Go to Environments > Prod, Find Rollout policy section, and choose Manual rollout > Require rolling out by dedicated roles.

bb-env-prod-manual-rollout

Custom Approval

  1. Go to CI/CD > Custom Approval. Set Project Owner -> DBA as Approval flow for DDL > High Risk.

    bb-custom-approval

  2. Go to CI/CD > Risk Center. Click Add rule and click Load for the first template. Click Add.

    bb-risk-center-ddl-high

Schema Drift

Bytebase has built-in schema drift detection to detect unexpected schema changes. Let's use the SQL Editor Admin Mode to simulate this.

  1. Click terminal icon (SQL Editor) on the top right. You'll be redirected to SQL Editor. Click Admin mode. Everything you do in this mode is the same as connecting directly to the server, which is not recorded by Bytebase.

  2. Select (Prod) employee on the left, and paste and run the following script:

    ALTER TABLE "public"."employee"
        ADD COLUMN "city" text NOT NULL DEFAULT '';
  3. Go back to Bytebase Console, and click Databases > employee under Prod. Click Sync Now. After seeing the success message, refresh the page. You'll see the schema drift. You may configure auto scan on instance detail page to avoid manual sync.

    bb-db-schema-drift

  4. Go to Database > Anomalies, and you'll see the Schema drift there too.

Summary

Now with Bytebase, you have a complete Database CI/CD workflow with Bitbucket. You can apply this workflow to your own project and customize it to fit your needs. If you have any questions, please feel free join and discuss in Discord.

Further Readings

Edit this page on GitHub

Subscribe to Newsletter

By subscribing, you agree with Bytebase's Terms of Service and Privacy Policy.