想要实现一个开源记账软件的多数据切换,但由于 prisma 不提供线上环境的数据库切换功能,因为该框架构建时会基于不同的数据库构建不同的底层支持,所以打包时必须明确一种数据库。
为了适配 prisma 同时实现多数据库,决定采用多个分支管理代码的方式,每一个分支对应一种数据库方案,这样就可以实现多数据库同步维护,但每次修改代码,每个分支都要同步合并代码,手动管理太过麻烦,因此决定通过 Github Actions 的方式,每次主分支提交代码,通过工作流自动将代码合并到其他分支,大大降低了手动维护成本!
下面是老狗的一个 开源软件 中正在使用的 Github Acitons
配置文件:
yamlname: Auto Sync Branches
on:
push:
branches:
- main
# 添加必要的权限
permissions:
contents: write
actions: read
jobs:
sync-branches:
runs-on: ubuntu-latest
steps:
- name: Checkout main branch
uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0 # 获取完整历史记录
token: ${{ secrets.MERGE_TOKEN }}
- name: Configure Git
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Sync to main-mysql branch
run: |
echo "Syncing to main-mysql branch..."
git checkout main-mysql || git checkout -b main-mysql
git merge main --no-edit || {
echo "Merge conflict detected for main-mysql, manual intervention required"
exit 1
}
git push origin main-mysql
- name: Sync to main-pgsql branch
run: |
echo "Syncing to main-pgsql branch..."
git checkout main-pgsql || git checkout -b main-pgsql
git merge main --no-edit || {
echo "Merge conflict detected for main-pgsql, manual intervention required"
exit 1
}
git push origin main-pgsql
- name: Sync to main-sqlite branch
run: |
echo "Syncing to main-sqlite branch..."
git checkout main-sqlite || git checkout -b main-sqlite
git merge main --no-edit || {
echo "Merge conflict detected for main-sqlite, manual intervention required"
exit 1
}
git push origin main-sqlite
- name: Sync to main-sqlserver branch
run: |
echo "Syncing to main-sqlserver branch..."
git checkout main-sqlserver || git checkout -b main-sqlserver
git merge main --no-edit || {
echo "Merge conflict detected for main-sqlserver, manual intervention required"
exit 1
}
git push origin main-sqlserver
- name: Summary
run: |
echo "✅ Successfully synced main branch to all database branches:"
echo " - main-mysql"
echo " - main-pgsql"
echo " - main-sqlite"
echo " - main-sqlserver"
本文作者:DingDangDog
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!