Skip to content

Commit da17308

Browse files
authored
Merge pull request #42 from tianzhou/main
chore: add CLAUDE.md
2 parents b7c3eca + a36ae77 commit da17308

File tree

2 files changed

+238
-0
lines changed

2 files changed

+238
-0
lines changed

.github/workflows/chatops-migrate.yml

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
name: ChatOps Migration Deployment
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
env:
8+
BYTEBASE_URL: https://demo.bytebase.com
9+
BYTEBASE_SERVICE_ACCOUNT: api@service.bytebase.com
10+
BYTEBASE_SERVICE_ACCOUNT_SECRET: ${{ secrets.BYTEBASE_SERVICE_ACCOUNT_SECRET }}
11+
BYTEBASE_PROJECT: "projects/hr"
12+
13+
jobs:
14+
parse-command:
15+
if: github.event.issue.pull_request && startsWith(github.event.comment.body, '/migrate')
16+
runs-on: ubuntu-latest
17+
outputs:
18+
environment: ${{ steps.parse.outputs.environment }}
19+
valid-command: ${{ steps.parse.outputs.valid-command }}
20+
steps:
21+
- name: Parse migrate command
22+
id: parse
23+
run: |
24+
COMMENT="${{ github.event.comment.body }}"
25+
echo "Comment: $COMMENT"
26+
27+
# Extract environment from "/migrate <environment>"
28+
if [[ $COMMENT =~ ^/migrate[[:space:]]+([a-zA-Z]+) ]]; then
29+
ENVIRONMENT="${BASH_REMATCH[1]}"
30+
echo "Parsed environment: $ENVIRONMENT"
31+
32+
# Validate environment
33+
case $ENVIRONMENT in
34+
test|prod)
35+
echo "environment=$ENVIRONMENT" >> $GITHUB_OUTPUT
36+
echo "valid-command=true" >> $GITHUB_OUTPUT
37+
echo "✅ Valid environment: $ENVIRONMENT"
38+
;;
39+
*)
40+
echo "valid-command=false" >> $GITHUB_OUTPUT
41+
echo "❌ Invalid environment: $ENVIRONMENT"
42+
;;
43+
esac
44+
else
45+
echo "valid-command=false" >> $GITHUB_OUTPUT
46+
echo "❌ Invalid command format"
47+
fi
48+
49+
- name: Add reaction to comment
50+
if: steps.parse.outputs.valid-command == 'true'
51+
uses: peter-evans/create-or-update-comment@v3
52+
with:
53+
comment-id: ${{ github.event.comment.id }}
54+
reactions: rocket
55+
56+
- name: Comment on invalid command
57+
if: steps.parse.outputs.valid-command == 'false'
58+
uses: peter-evans/create-or-update-comment@v3
59+
with:
60+
issue-number: ${{ github.event.issue.number }}
61+
body: |
62+
❌ Invalid migrate command.
63+
64+
**Usage:** `/migrate <environment>`
65+
**Valid environments:** `test`, `prod`
66+
67+
**Example:** `/migrate test`
68+
reactions: confused
69+
70+
deploy:
71+
needs: parse-command
72+
if: needs.parse-command.outputs.valid-command == 'true'
73+
runs-on: ubuntu-latest
74+
environment: ${{ needs.parse-command.outputs.environment }}
75+
container:
76+
image: bytebase/bytebase-action:latest
77+
steps:
78+
- name: Checkout PR merge commit
79+
uses: actions/checkout@v4
80+
with:
81+
ref: refs/pull/${{ github.event.issue.number }}/merge
82+
83+
- name: Comment deployment started
84+
uses: peter-evans/create-or-update-comment@v3
85+
with:
86+
issue-number: ${{ github.event.issue.number }}
87+
body: |
88+
🚀 **Migration deployment started**
89+
90+
**Environment:** `${{ needs.parse-command.outputs.environment }}`
91+
**PR:** #${{ github.event.issue.number }}
92+
**Triggered by:** @${{ github.event.comment.user.login }}
93+
94+
Deploying database changes...
95+
96+
- name: Set environment-specific targets
97+
id: env-config
98+
run: |
99+
case "${{ needs.parse-command.outputs.environment }}" in
100+
test)
101+
echo "bytebase-targets=instances/test-sample-instance/databases/hr_test" >> $GITHUB_OUTPUT
102+
echo "bytebase-stage=environments/test" >> $GITHUB_OUTPUT
103+
;;
104+
prod)
105+
echo "bytebase-targets=instances/prod-sample-instance/databases/hr_prod" >> $GITHUB_OUTPUT
106+
echo "bytebase-stage=environments/prod" >> $GITHUB_OUTPUT
107+
;;
108+
esac
109+
110+
- name: Create rollout plan
111+
id: create-rollout
112+
env:
113+
BYTEBASE_TARGETS: ${{ steps.env-config.outputs.bytebase-targets }}
114+
FILE_PATTERN: "migrations-semver/*.sql"
115+
BYTEBASE_OUTPUT: ${{ runner.temp }}/bytebase-metadata.json
116+
run: |
117+
echo "Creating rollout plan for ${{ needs.parse-command.outputs.environment }}..."
118+
119+
bytebase-action rollout \
120+
--url=${{ env.BYTEBASE_URL }} \
121+
--service-account=${{ env.BYTEBASE_SERVICE_ACCOUNT }} \
122+
--service-account-secret=${{ env.BYTEBASE_SERVICE_ACCOUNT_SECRET }} \
123+
--project=${{ env.BYTEBASE_PROJECT }} \
124+
--file-pattern=${{ env.FILE_PATTERN }} \
125+
--targets=${{ env.BYTEBASE_TARGETS }} \
126+
--output=${{ env.BYTEBASE_OUTPUT }}
127+
128+
PLAN=$(jq -r .plan ${{ runner.temp }}/bytebase-metadata.json)
129+
echo "plan=$PLAN" >> $GITHUB_OUTPUT
130+
131+
- name: Execute rollout
132+
env:
133+
BYTEBASE_TARGET_STAGE: ${{ steps.env-config.outputs.bytebase-stage }}
134+
run: |
135+
echo "Executing rollout to ${{ needs.parse-command.outputs.environment }}..."
136+
137+
bytebase-action rollout \
138+
--url=${{ env.BYTEBASE_URL }} \
139+
--service-account=${{ env.BYTEBASE_SERVICE_ACCOUNT }} \
140+
--service-account-secret=${{ env.BYTEBASE_SERVICE_ACCOUNT_SECRET }} \
141+
--project=${{ env.BYTEBASE_PROJECT }} \
142+
--target-stage=${{ env.BYTEBASE_TARGET_STAGE }} \
143+
--plan=${{ steps.create-rollout.outputs.plan }}
144+
145+
- name: Comment deployment success
146+
if: success()
147+
uses: peter-evans/create-or-update-comment@v3
148+
with:
149+
issue-number: ${{ github.event.issue.number }}
150+
body: |
151+
✅ **Migration deployment completed successfully**
152+
153+
**Environment:** `${{ needs.parse-command.outputs.environment }}`
154+
**PR:** #${{ github.event.issue.number }}
155+
**Triggered by:** @${{ github.event.comment.user.login }}
156+
157+
Database schema has been updated in the `${{ needs.parse-command.outputs.environment }}` environment.
158+
159+
- name: Comment deployment failure
160+
if: failure()
161+
uses: peter-evans/create-or-update-comment@v3
162+
with:
163+
issue-number: ${{ github.event.issue.number }}
164+
body: |
165+
❌ **Migration deployment failed**
166+
167+
**Environment:** `${{ needs.parse-command.outputs.environment }}`
168+
**PR:** #${{ github.event.issue.number }}
169+
**Triggered by:** @${{ github.event.comment.user.login }}
170+
171+
Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.

CLAUDE.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Project Overview
6+
7+
This is a GitOps demonstration repository for database CI/CD using Bytebase and GitHub Actions with GitHub Flow. The repository shows how to integrate database schema changes with application deployment pipelines.
8+
9+
## Repository Structure
10+
11+
- `migrations-semver/`: Contains SQL migration files following semantic versioning naming convention (e.g., `1.0.0_init.sql`, `1.1.1_comment.sql`)
12+
- `schema/`: Contains base database schema definitions
13+
- `.github/workflows/`: GitHub Actions workflows for CI/CD pipeline
14+
15+
## Migration File Naming
16+
17+
Migration files MUST follow semantic versioning pattern: `{major}.{minor}.{patch}_{description}.sql`
18+
- Examples: `1.0.0_init.sql`, `1.1.1_comment.sql`, `1.13.0_phone.sql`
19+
- Files are processed by Bytebase in semantic version order
20+
21+
## GitHub Actions Workflows
22+
23+
### SQL Review Workflow (`sql-review-action.yml`)
24+
- Triggers on pull requests to `main` branch when `migrations-semver/*.sql` files change
25+
- Uses `bytebase/bytebase-action:latest` Docker image
26+
- Runs SQL validation against production database
27+
- Requires `BYTEBASE_SERVICE_ACCOUNT_SECRET` repository secret
28+
29+
### Release Workflow (`release-action.yml`)
30+
- Triggers on push to `main` branch when `migrations-semver/*.sql` files change
31+
- Three-stage process:
32+
1. `build`: Mock application build step
33+
2. `create-rollout`: Creates Bytebase rollout plan for both test and prod databases
34+
3. `deploy-to-test`: Deploys to test environment automatically
35+
4. `deploy-to-prod`: Deploys to production (requires manual approval via GitHub environment protection)
36+
37+
## Environment Configuration
38+
39+
Both workflows use these environment variables:
40+
- `BYTEBASE_URL`: Bytebase instance URL
41+
- `BYTEBASE_SERVICE_ACCOUNT`: Service account email
42+
- `BYTEBASE_SERVICE_ACCOUNT_SECRET`: Service account password (stored in GitHub secrets)
43+
- `BYTEBASE_PROJECT`: Target Bytebase project
44+
- `BYTEBASE_TARGETS`: Comma-separated list of database targets
45+
- `FILE_PATTERN`: Glob pattern for migration files (`migrations-semver/*.sql`)
46+
47+
## Database Schema
48+
49+
The schema includes:
50+
- Employee management system with tables: `employee`, `department`, `dept_manager`, `dept_emp`, `title`, `salary`
51+
- Audit logging system with trigger-based change tracking
52+
- Views for current department assignments
53+
54+
## Development Workflow
55+
56+
1. Create feature branch
57+
2. Add SQL migration files to `migrations-semver/` with proper semantic versioning
58+
3. Create pull request - triggers SQL review workflow
59+
4. Merge to main - triggers release workflow
60+
5. Test environment deployment happens automatically
61+
6. Production deployment requires manual approval through GitHub environment protection
62+
63+
## Key Integration Points
64+
65+
- All database changes go through Bytebase for review and deployment
66+
- GitHub environment protection rules control production deployments
67+
- Migration files are validated against actual database schemas during PR review

0 commit comments

Comments
 (0)