Skip to content

Commit 8709e35

Browse files
committed
feat: examples
1 parent 187a96a commit 8709e35

File tree

3 files changed

+231
-0
lines changed

3 files changed

+231
-0
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# About
2+
3+
This repository demonstrates how to use Bytebase and GitHub actions to do database release CI/CD with a code base following [GitHub flow](https://docs.github.com/en/get-started/using-github/github-flow).
4+
5+
For GitHub flow, feature branches are merged into the main branch and the main branch is deployed to the, for example, "test" and "prod" environments in a deploy pipeline.
6+
7+
[sql-review.yml](/workflows/sql-review.yml) checks the SQL migration files against the databases when pull requests are created.
8+
9+
[release.yml](/workflows/release.yml) builds the code and then for each environment migrate the databases and deploy the code. Using [environments with protection rules](https://docs.github.com/en/actions/managing-workflow-runs-and-deployments/managing-deployments/managing-environments-for-deployment#required-reviewers), it can deploy to the test environment automatically and push to the prod environment after approval.
10+
11+
## How to configure sql-review.yml
12+
13+
Copy [sql-review.yml](/workflows/sql-review.yml) to your repository.
14+
15+
Modify the environment variables to match your setup.
16+
```yml
17+
env:
18+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # set GITHUB_TOKEN because the 'Check release' step needs it to comment the pull request with check results.
19+
BYTEBASE_URL: https://demo.bytebase.com
20+
BYTEBASE_SERVICE_ACCOUNT: [email protected]
21+
BYTEBASE_PROJECT: "projects/project-sample"
22+
BYTEBASE_TARGETS: "instances/test-sample-instance/databases/hr_test" # the database targets to check against.
23+
FILE_PATTERN: "migrations/*.sql" # the glob pattern matching the migration files.
24+
```
25+
26+
Set your service account password in the repository secrets setting with the name `BYTEBASE_SERVICE_ACCOUNT_SECRET`.
27+
28+
The migration filename should comply to the naming scheme described in [bytebase/create-release-action](https://github.com/bytebase/create-release-action/tree/main).
29+
30+
## How to configure release.yml
31+
32+
Copy [release.yml](/workflows/release.yml) to your repository.
33+
34+
Modify the environment variables to match your setup.
35+
```yml
36+
env:
37+
BYTEBASE_URL: https://demo.bytebase.com
38+
BYTEBASE_SERVICE_ACCOUNT: [email protected]
39+
BYTEBASE_PROJECT: "projects/project-sample"
40+
# The Bytebase rollout pipeline will deploy to 'test' and 'prod' environments.
41+
# 'deploy_to_test' job rollouts the 'test' stage and 'deploy_to_prod' job rollouts the 'prod' stage.
42+
BYTEBASE_TARGETS: "instances/test-sample-instance/databases/hr_test,instances/prod-sample-instance/databases/hr_prod"
43+
FILE_PATTERN: "migrations/*.sql" # the glob pattern matching the migration files.
44+
```
45+
46+
In the repository environments setting, create two environments: "test" and "prod". In the "prod" environment setting, configure "Deployment protection rules", check "Required reviewers" and add reviewers in order to rollout the "prod" environment after approval.
47+
48+
Set your service account password in the repository secrets setting with the name `BYTEBASE_SERVICE_ACCOUNT_SECRET`.
49+
50+
The migration filename should comply to the naming scheme described in [bytebase/create-release-action](https://github.com/bytebase/create-release-action/tree/main).

workflows/release.yml

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
name: Build and push release image
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v4
14+
- name: Build app and upload
15+
run: |
16+
echo "Building..."
17+
echo "Build done!"
18+
echo "Uploading..."
19+
echo "Upload done!"
20+
deploy_to_test:
21+
needs: build
22+
runs-on: ubuntu-latest
23+
environment: test
24+
env:
25+
BYTEBASE_URL: https://demo.bytebase.com
26+
BYTEBASE_SERVICE_ACCOUNT: [email protected]
27+
BYTEBASE_PROJECT: "projects/project-sample"
28+
# The Bytebase rollout pipeline will deploy to 'test' and 'prod' environments.
29+
# 'deploy_to_test' job rollouts the 'test' stage and 'deploy_to_prod' job rollouts the 'prod' stage.
30+
BYTEBASE_TARGETS: "instances/test-sample-instance/databases/hr_test,instances/prod-sample-instance/databases/hr_prod"
31+
FILE_PATTERN: "migrations/*.sql"
32+
outputs:
33+
bytebase_plan: ${{ steps.create_plan.outputs.plan }}
34+
deployment_required: ${{ steps.create_plan.outputs.deployment-required }}
35+
steps:
36+
- name: Checkout
37+
uses: actions/checkout@v4
38+
- name: Login to Bytebase
39+
id: login
40+
uses: bytebase/login-action@v1
41+
with:
42+
bytebase-url: ${{ env.BYTEBASE_URL }}
43+
service-key: ${{ env.BYTEBASE_SERVICE_ACCOUNT }}
44+
service-secret: ${{secrets.BYTEBASE_SERVICE_ACCOUNT_SECRET}} # Please use secrets for sensitive data in production.
45+
- name: Create release
46+
id: create_release
47+
uses: bytebase/create-release-action@v1
48+
with:
49+
url: ${{ env.BYTEBASE_URL }}
50+
token: ${{ steps.login.outputs.token }}
51+
file-pattern: ${{ env.FILE_PATTERN }}
52+
# fail the action if release checks report any error.
53+
check-release: "FAIL_ON_ERROR"
54+
project: ${{ env.BYTEBASE_PROJECT }}
55+
targets: ${{ env.BYTEBASE_TARGETS }}
56+
validate-only: false
57+
- name: Create plan
58+
id: create_plan
59+
uses: bytebase/create-plan-from-release-action@v1
60+
with:
61+
url: ${{ env.BYTEBASE_URL }}
62+
token: ${{ steps.login.outputs.token }}
63+
project: ${{ env.BYTEBASE_PROJECT }}
64+
release: ${{ steps.create_release.outputs.release }}
65+
targets: ${{ env.BYTEBASE_TARGETS }}
66+
check-plan: "SKIP"
67+
- name: Rollout
68+
id: rollout
69+
uses: bytebase/rollout-action@v1
70+
if: ${{ steps.create_plan.outputs.deployment_required == 'true' }}
71+
with:
72+
url: ${{ env.BYTEBASE_URL }}
73+
token: ${{ steps.login.outputs.token }}
74+
plan: ${{ steps.create_plan.outputs.plan }}
75+
target-stage: 'Test Stage'
76+
- name: Deploy app
77+
run: |
78+
echo "Deploying app to test environment..."
79+
echo "Deploy app to test environment done!"
80+
deploy_to_prod:
81+
needs: deploy_to_test
82+
runs-on: ubuntu-latest
83+
environment: prod
84+
env:
85+
BYTEBASE_URL: https://demo.bytebase.com
86+
BYTEBASE_SERVICE_ACCOUNT: [email protected]
87+
if: ${{ needs.deploy_to_test.outputs.deployment_required == 'true' }}
88+
steps:
89+
- name: Checkout
90+
uses: actions/checkout@v4
91+
- name: Login to Bytebase
92+
id: login
93+
uses: bytebase/login-action@v1
94+
with:
95+
bytebase-url: ${{ env.BYTEBASE_URL }}
96+
service-key: ${{ env.BYTEBASE_SERVICE_ACCOUNT }}
97+
service-secret: ${{secrets.BYTEBASE_SERVICE_ACCOUNT_SECRET}} # Please use secrets for sensitive data in production.
98+
- name: Rollout
99+
id: rollout
100+
uses: bytebase/rollout-action@v1
101+
with:
102+
url: ${{ env.BYTEBASE_URL }}
103+
token: ${{ steps.login.outputs.token }}
104+
plan: ${{ needs.deploy_to_test.outputs.bytebase_plan }}
105+
target-stage: 'Prod Stage'
106+
- name: Deploy app
107+
run: |
108+
echo "Deploying app to prod environment..."
109+
echo "Deploy app to prod environment done!"

workflows/sql-review.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: SQL review on pull request
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
check_release_on_test:
10+
permissions:
11+
pull-requests: write # write permission required to allow the action writes the check results to the comment.
12+
runs-on: ubuntu-latest
13+
env:
14+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # set GITHUB_TOKEN because the 'Check release' step needs it to comment the pull request with check results.
15+
BYTEBASE_URL: https://demo.bytebase.com
16+
BYTEBASE_SERVICE_ACCOUNT: [email protected]
17+
BYTEBASE_PROJECT: "projects/project-sample"
18+
BYTEBASE_TARGETS: "instances/test-sample-instance/databases/hr_test"
19+
FILE_PATTERN: "migrations/*.sql"
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
- name: Login to Bytebase
24+
id: login
25+
uses: bytebase/login-action@v1
26+
with:
27+
bytebase-url: ${{ env.BYTEBASE_URL }}
28+
service-key: ${{ env.BYTEBASE_SERVICE_ACCOUNT }}
29+
service-secret: ${{secrets.BYTEBASE_SERVICE_ACCOUNT_SECRET}} # Please use secrets for sensitive data in production.
30+
- name: Check release
31+
uses: bytebase/create-release-action@v1
32+
with:
33+
url: ${{ env.BYTEBASE_URL }}
34+
token: ${{ steps.login.outputs.token }}
35+
file-pattern: ${{ env.FILE_PATTERN }}
36+
# fail the action if release checks report any error.
37+
check-release: "FAIL_ON_ERROR"
38+
project: ${{ env.BYTEBASE_PROJECT }}
39+
targets: ${{ env.BYTEBASE_TARGETS }}
40+
validate-only: true
41+
check_release_on_prod:
42+
permissions:
43+
pull-requests: write # write permission required to allow the action writes the check results to the comment.
44+
runs-on: ubuntu-latest
45+
env:
46+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # set GITHUB_TOKEN because the 'Check release' step needs it to comment the pull request with check results.
47+
BYTEBASE_URL: https://demo.bytebase.com
48+
BYTEBASE_SERVICE_ACCOUNT: [email protected]
49+
BYTEBASE_PROJECT: "projects/project-sample"
50+
BYTEBASE_TARGETS: "instances/prod-sample-instance/databases/hr_prod"
51+
FILE_PATTERN: "migrations/*.sql"
52+
steps:
53+
- name: Checkout
54+
uses: actions/checkout@v4
55+
- name: Login to Bytebase
56+
id: login
57+
uses: bytebase/login-action@v1
58+
with:
59+
bytebase-url: ${{ env.BYTEBASE_URL }}
60+
service-key: ${{ env.BYTEBASE_SERVICE_ACCOUNT }}
61+
service-secret: ${{secrets.BYTEBASE_SERVICE_ACCOUNT_SECRET}} # Please use secrets for sensitive data in production.
62+
- name: Check release
63+
uses: bytebase/create-release-action@v1
64+
with:
65+
url: ${{ env.BYTEBASE_URL }}
66+
token: ${{ steps.login.outputs.token }}
67+
file-pattern: ${{ env.FILE_PATTERN }}
68+
# fail the action if release checks report any error.
69+
check-release: "FAIL_ON_ERROR"
70+
project: ${{ env.BYTEBASE_PROJECT }}
71+
targets: ${{ env.BYTEBASE_TARGETS }}
72+
validate-only: true

0 commit comments

Comments
 (0)