Skip to content

Commit 5b26e84

Browse files
authored
create chartdb action (#1)
* create chartdb action * fix
1 parent 06129a0 commit 5b26e84

7 files changed

Lines changed: 875 additions & 661 deletions

File tree

.github/workflows/test.yml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: Test ChartDB Sync Action
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
workflow_dispatch:
7+
8+
jobs:
9+
test-postgresql:
10+
runs-on: ubuntu-latest
11+
name: Test with PostgreSQL
12+
13+
services:
14+
postgres:
15+
image: postgres:15
16+
env:
17+
POSTGRES_USER: testuser
18+
POSTGRES_PASSWORD: testpass
19+
POSTGRES_DB: testdb
20+
options: >-
21+
--health-cmd pg_isready
22+
--health-interval 10s
23+
--health-timeout 5s
24+
--health-retries 5
25+
ports:
26+
- 5432:5432
27+
28+
steps:
29+
- name: Checkout
30+
uses: actions/checkout@v4
31+
32+
- name: Create test schema
33+
run: |
34+
PGPASSWORD=testpass psql -h localhost -U testuser -d testdb << EOF
35+
CREATE TABLE users (
36+
id SERIAL PRIMARY KEY,
37+
email VARCHAR(255) UNIQUE NOT NULL,
38+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
39+
);
40+
41+
CREATE TABLE posts (
42+
id SERIAL PRIMARY KEY,
43+
user_id INTEGER REFERENCES users(id),
44+
title VARCHAR(255) NOT NULL,
45+
content TEXT,
46+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
47+
);
48+
49+
CREATE INDEX idx_posts_user_id ON posts(user_id);
50+
EOF
51+
52+
- name: Test ChartDB Sync Action
53+
id: chartdb-sync-postgres
54+
uses: ./
55+
with:
56+
db-host: "localhost"
57+
db-port: "5432"
58+
db-database: "testdb"
59+
db-username: "testuser"
60+
db-password: "testpass"
61+
db-type: "postgresql"
62+
chartdb-api-token: ${{ secrets.TEST_CHARTDB_API_TOKEN || 'test_token' }}
63+
chartdb-diagram-id: ${{ secrets.TEST_CHARTDB_DIAGRAM_ID || 'test_diagram' }}
64+
65+
- name: Check for errors in output
66+
run: |
67+
echo "Checking action output for errors..."
68+
if echo "${{ steps.chartdb-sync-postgres.outputs.* }}" | grep -i "error"; then
69+
echo "Error found in action output!"
70+
exit 1
71+
else
72+
echo "No errors found in output"
73+
fi

LICENSE

Lines changed: 201 additions & 661 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
# ChartDB Database Sync Action
2+
3+
Automatically sync your database schema with [ChartDB](https://chartdb.io) to keep your ER diagrams up-to-date.
4+
5+
## Features
6+
7+
- 🔄 Automatic database schema synchronization
8+
- 📊 Updates ChartDB diagrams in real-time
9+
- 🔒 Secure handling of credentials via GitHub Secrets
10+
- 🐳 Docker-based for consistent execution
11+
- 🗄️ Supports PostgreSQL, MySQL, MariaDB, and MSSQL
12+
13+
## Usage
14+
15+
### Basic Example
16+
17+
```yaml
18+
name: Sync Database with ChartDB
19+
on:
20+
push:
21+
branches: [ main ]
22+
schedule:
23+
- cron: '0 0 * * *' # Daily sync at midnight
24+
25+
jobs:
26+
sync:
27+
runs-on: ubuntu-latest
28+
steps:
29+
- name: Sync Database Schema
30+
uses: chartdb/chartdb-action@v1
31+
with:
32+
db-host: ${{ secrets.DB_HOST }}
33+
db-port: '5432'
34+
db-database: ${{ secrets.DB_DATABASE }}
35+
db-username: ${{ secrets.DB_USERNAME }}
36+
db-password: ${{ secrets.DB_PASSWORD }}
37+
db-type: 'postgresql'
38+
chartdb-api-token: ${{ secrets.CHARTDB_API_TOKEN }}
39+
chartdb-diagram-id: ${{ secrets.CHARTDB_DIAGRAM_ID }}
40+
```
41+
42+
### With GitHub-hosted Database
43+
44+
```yaml
45+
name: Sync GitHub Actions Database
46+
on:
47+
workflow_dispatch:
48+
push:
49+
paths:
50+
- 'migrations/**'
51+
- 'schema/**'
52+
53+
jobs:
54+
sync:
55+
runs-on: ubuntu-latest
56+
services:
57+
postgres:
58+
image: postgres:15
59+
env:
60+
POSTGRES_USER: testuser
61+
POSTGRES_PASSWORD: testpass
62+
POSTGRES_DB: testdb
63+
options: >-
64+
--health-cmd pg_isready
65+
--health-interval 10s
66+
--health-timeout 5s
67+
--health-retries 5
68+
ports:
69+
- 5432:5432
70+
71+
steps:
72+
- uses: actions/checkout@v4
73+
74+
- name: Run migrations
75+
run: |
76+
# Your migration commands here
77+
78+
- name: Sync with ChartDB
79+
uses: chartdb/chartdb-action@v1
80+
with:
81+
db-host: 'localhost'
82+
db-port: '5432'
83+
db-database: 'testdb'
84+
db-username: 'testuser'
85+
db-password: 'testpass'
86+
db-type: 'postgresql'
87+
chartdb-api-token: ${{ secrets.CHARTDB_API_TOKEN }}
88+
chartdb-diagram-id: ${{ secrets.CHARTDB_DIAGRAM_ID }}
89+
```
90+
91+
## Inputs
92+
93+
| Input | Description | Required | Default |
94+
|-------|-------------|----------|---------|
95+
| `db-host` | Database host address | Yes | - |
96+
| `db-port` | Database port | No | `5432` |
97+
| `db-database` | Database name | Yes | - |
98+
| `db-username` | Database username | Yes | - |
99+
| `db-password` | Database password (use secrets!) | No | - |
100+
| `db-type` | Database type (`postgresql`, `mysql`, `mariadb`, `mssql`) | Yes | - |
101+
| `chartdb-api-token` | ChartDB API token (use secrets!) | Yes | - |
102+
| `chartdb-diagram-id` | ChartDB diagram ID to sync with | Yes | - |
103+
| `network` | Docker network mode | No | `host` |
104+
105+
## Outputs
106+
107+
| Output | Description |
108+
|--------|-------------|
109+
| `status` | Sync status (`success` or `failed`) |
110+
111+
## Setup
112+
113+
### 1. Get ChartDB Credentials
114+
115+
1. Sign up at [ChartDB](https://chartdb.io)
116+
2. Create or open your diagram
117+
3. Navigate to diagram settings
118+
4. Generate an API token
119+
5. Copy your diagram ID from the URL or settings
120+
121+
### 2. Configure GitHub Secrets
122+
123+
Add the following secrets to your repository:
124+
125+
1. Go to Settings → Secrets and variables → Actions
126+
2. Add these secrets:
127+
- `DB_HOST`: Your database host
128+
- `DB_DATABASE`: Your database name
129+
- `DB_USERNAME`: Database username
130+
- `DB_PASSWORD`: Database password
131+
- `CHARTDB_API_TOKEN`: Your ChartDB API token
132+
- `CHARTDB_DIAGRAM_ID`: Your ChartDB diagram ID
133+
134+
### 3. Add Workflow
135+
136+
Create `.github/workflows/chartdb-sync.yml` in your repository with one of the examples above.
137+
138+
## Security Considerations
139+
140+
- **Always use GitHub Secrets** for sensitive information
141+
- Never commit credentials directly in workflow files
142+
- Consider using environment-specific secrets for different stages
143+
- Use least-privilege database users for syncing
144+
- Rotate API tokens regularly
145+
146+
## Database Support
147+
148+
### PostgreSQL
149+
```yaml
150+
db-type: 'postgresql'
151+
db-port: '5432' # default
152+
```
153+
154+
### MySQL
155+
```yaml
156+
db-type: 'mysql'
157+
db-port: '3306' # default
158+
```
159+
160+
### MariaDB
161+
```yaml
162+
db-type: 'mariadb'
163+
db-port: '3306' # default
164+
```
165+
166+
### Microsoft SQL Server
167+
```yaml
168+
db-type: 'mssql'
169+
db-port: '1433' # default
170+
```
171+
172+
## Advanced Usage
173+
174+
### Conditional Sync
175+
176+
```yaml
177+
- name: Sync with ChartDB
178+
if: github.ref == 'refs/heads/main'
179+
uses: chartdb/chartdb-action@v1
180+
with:
181+
# ... your configuration
182+
```
183+
184+
### Multiple Environments
185+
186+
```yaml
187+
jobs:
188+
sync-staging:
189+
runs-on: ubuntu-latest
190+
environment: staging
191+
steps:
192+
- uses: chartdb/chartdb-action@v1
193+
with:
194+
db-host: ${{ secrets.STAGING_DB_HOST }}
195+
chartdb-diagram-id: ${{ secrets.STAGING_DIAGRAM_ID }}
196+
# ... other staging config
197+
198+
sync-production:
199+
runs-on: ubuntu-latest
200+
environment: production
201+
if: github.ref == 'refs/heads/main'
202+
steps:
203+
- uses: chartdb/chartdb-action@v1
204+
with:
205+
db-host: ${{ secrets.PROD_DB_HOST }}
206+
chartdb-diagram-id: ${{ secrets.PROD_DIAGRAM_ID }}
207+
# ... other production config
208+
```
209+
210+
### With Status Check
211+
212+
```yaml
213+
- name: Sync with ChartDB
214+
id: chartdb-sync
215+
uses: chartdb/chartdb-action@v1
216+
with:
217+
# ... your configuration
218+
219+
- name: Check sync status
220+
if: steps.chartdb-sync.outputs.status == 'failed'
221+
run: |
222+
echo "ChartDB sync failed! Check the logs above."
223+
exit 1
224+
```
225+
226+
## Troubleshooting
227+
228+
### Connection Issues
229+
230+
If the action fails to connect to your database:
231+
232+
1. **Check network accessibility**: Ensure your database is accessible from GitHub Actions runners
233+
2. **Verify credentials**: Double-check all secrets are correctly set
234+
3. **Firewall rules**: Add GitHub Actions IP ranges to your database firewall if needed
235+
4. **Use self-hosted runners**: For private databases, consider using self-hosted runners
236+
237+
### Docker Network Modes
238+
239+
The action uses `host` network mode by default. For other configurations:
240+
241+
```yaml
242+
with:
243+
network: 'bridge' # or 'none', 'container:name', etc.
244+
```
245+
246+
## Contributing
247+
248+
Contributions are welcome! Please feel free to submit a Pull Request.
249+
250+
## License
251+
252+
MIT
253+
254+
## Support
255+
256+
- 📧 Email: support@chartdb.io
257+
- 🐛 Issues: [GitHub Issues](https://github.com/chartdb/chartdb-action/issues)
258+
- 📖 Documentation: [ChartDB Docs](https://docs.chartdb.io)
259+
260+
## Related
261+
262+
- [ChartDB](https://chartdb.io) - Visual database design tool
263+
- [ChartDB Syncer](https://github.com/chartdb/chartdb-syncer) - Docker image for database synchronization

0 commit comments

Comments
 (0)