Automate Database Change with Jira
In the previous tutorial, we have set up a manual database change workflow with Jira and Bytebase. In this tutorial, we will show you how to automate the process by leveraging Jira and Bytebase Webhook and API. You need to finish the previous tutorial first.
Bytebase is an open-source database DevSecOps solution for Developer, Security, DBA, and Platform Engineering teams. The GitLab for database DevSecOps.
Here is what you will achieve by the end of this tutorial:
Prerequisites
- A Jira workspace
- A Bytebase instance
- Manual Database Change with Jira completed
- Download the api-example repository, you'll only need
jira
folder for this tutorial
Workflow Overview
Setup
- Configure the environment variables and run the
jira
demo app - Setup Jira Webhook and API
- Setup Bytebase Webhook and API
Change process
- (Jira) Developer creates a Jira
Database Change
issue filling the summary, SQL, database, and description fields, the status isTodo
. - (Jira Webhook -> Bytebase API) Once the Jira issue is created, Jira webhook will trigger Bytebase API to create a corresponding issue.
- (Bytebase API -> Jira API) Once the Bytebase issue is created, the success response will trigger Jira API to set Jira issue with the Bytebase issue link and change the status to
In Progress
. - (Bytebase) DBA goes to Bytebase to roll out the database change.
- (Bytebase Webhook -> Jira API) Once the Bytebase issue rolls out and becomes
Done
, Bytebase Webhook will trigger Jira API to set Jira issue status toDone
.
Setup
Configure the environment variables and run the jira
demo app.
-
Go to the
jira
folder of theapi-example
repository, and copyenv-template.local
file as.env.local
. Replace the placeholders with yours. -
Run
pnpm i
andpnpm run dev
, you can run the demo app locally withlocalhost:xxxx
. However, the app need to listen to webhook from Jira and Bytebase, so you need to make the app network accessible from both. By using ngrok or vscode ports, you can acheive this.
Jira webhook: To trigger when Jira issue is created or updated
-
Go to Jira, click Settings and then System settings in the dropdown menu.
-
Click WebHooks on the left sidebar, and then click + Create a WebHook.
-
Fill in the URL, remember to append
/api/receive-jira-issue-webhook
to your base URL for the demo jira app, selectIssue created and updated
, and click Create.
Bytebase API: To create a Bytebase issue
-
Go to Bytebase, click IAM & Admin > Users & Groups on the left sidebar, and then click +Add User.
-
Choose
Service Account
as User Type, fill in the Email, give itWorkspace DBA
role, and then click Create. Copy the API Token to the.env.local
file.
Jira API: To update Jira issue status to In Progress
/ Done
and set its Bytebase issue link
-
Go to Atlassian Account >Security > API tokens to generate an API token. Copy the API Token to the
.env.local
file.
Bytebase Webhook: To trigger when Bytebase issue is set to Done
-
Go in to the project, click Integration > Webhooks on the left sidebar and click Add A Webhook.
-
Choose
Custom
as Destination, fill in the Webhook URL, remember to append/api/receive-bb-issue-webhook
to your base URL for the demo jira app, selectIssue status change
as Triggering activities and click Create.
Change process
Step 1 (Jira): Create a database change issue
-
You act as a developer, now go to the Jira project to create a
Database Change
issue, fill in the fields summary, SQL, database, and description, and click Create. Here's the screenshot of the issue. -
View the
jira
app demo, you will see there's a jira webhook received withTodo
status.
Step 2 (Jira Webhook -> Bytebase API) Once the Jira issue is created, Jira Webhook will trigger Bytebase API to create a corresponding issue
Go to the Bytebase project and find the issue which is waiting to rollout.
It's because the jira webhook trigger Bytebase API to create an issue there. The logic is in src/api/receive-jira-issue-webhook/route.ts
.
-
When it receive the trigger, it checks if the issue type is
Database Change
. and then if the webhook event isissue_created
. -
If both are true, via Bytebase API, it will try to match the Jira's
project key
with Bytebase'sproject key
to make sure they're the same. then it will try to match the Jira'sdatabase
with the database belonging to that matching Bytebase project. -
Only if the project and database are both matched, it will create a Bytebase issue.
which internally involves four steps:
Step 3 (Bytebase API -> Jira API) Once the Bytebase issue is created, the Jira API will set Bytebase issue link in Jira issue, and set status as In Progress
-
If you go back to Jira, you'll see the Jira issue status becomes
In Progress
with Bytebase url link filled. -
View the
jira
app demo, it's updated, too.
The logic is still in src/api/receive-jira-issue-webhook/route.ts
.
-
Once the Bytebase issue is created via API, the demo app will parse the Bytebase issue link.
-
Then call the Jira API to update Bytebase issue link field and change the status from
Todo
toIn Progress
.Here we need to call two Jira APIs:
/rest/api/3/issue/${issueKey}
to update Bytebase Link/rest/api/3/issue/${issueKey}/transitions
to change the status
Step 4 (Bytebase) DBA goes to Bytebase to roll out the database change.
-
You now act as DBA, go to Bytebase to roll out the database change.
-
Once change is rolled out, Bytebase will record the change in the database Change History.
-
You can also click View change to view the change diff.
Step 5 (Bytebase Webhook -> Jira API) Once the Bytebase issue rolls out and becomes Done
, Bytebase Webhook will trigger Jira API to set Jira issue status as Done
.
-
Once the issue has rolled out in Bytebase, the configured webhook will trigger
jira
app demo. -
Go to Jira, you'll see the Jira issue status becomes
Done
.
The logic is in src/app/api/receive-bb-issue-webhook/route.ts
. If it's a issue update, it will parse the Jira issue key from the Bytebase issue name, and then call the Jira API to update the issue status to Done
.
Summary
In this tutorial, you have successfully set up a automatic database change workflow with Jira and Bytebase. We eliminate most of the manual process in the last tutorial.
- Bytebase issue is automatically created. And the created issue link is set automatically in the Jira issue.
- Once Bytebase rolls out the SQL, the Jira issue status is updated automatically.
If you want to automate further, you can also call Bytebase API to approve and roll out the SQL.