ci: auto-merge generated types PR (#106) #87
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Deploy to dev — pushes migrations and regenerates types on every push to dev | |
| name: Deploy Dev | |
| on: | |
| push: | |
| branches: [dev] | |
| # Never cancel in-flight migrations — wait for previous run to finish | |
| concurrency: | |
| group: deploy-dev | |
| cancel-in-progress: false | |
| jobs: | |
| migrate: | |
| name: Push Migrations | |
| # Skip if triggered by bot's type regeneration commit to prevent infinite loops | |
| if: github.actor != 'github-actions[bot]' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: supabase/setup-cli@v1 | |
| with: | |
| version: latest | |
| - name: Link to dev project | |
| run: supabase link --project-ref "${{ secrets.SUPABASE_DEV_PROJECT_REF }}" | |
| working-directory: packages/db | |
| env: | |
| SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }} | |
| - name: Push migrations | |
| run: supabase db push | |
| working-directory: packages/db | |
| env: | |
| SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }} | |
| SUPABASE_DB_PASSWORD: ${{ secrets.SUPABASE_DEV_DB_PASSWORD }} | |
| - name: Summary | |
| if: always() | |
| run: echo "### Migrate ${{ job.status == 'success' && '✅' || '❌' }}" >> "$GITHUB_STEP_SUMMARY" | |
| schema-drift: | |
| name: Schema Drift Detection | |
| needs: [migrate] | |
| continue-on-error: true | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: supabase/setup-cli@v1 | |
| with: | |
| version: latest | |
| - name: Link to dev project | |
| run: supabase link --project-ref "${{ secrets.SUPABASE_DEV_PROJECT_REF }}" | |
| working-directory: packages/db | |
| env: | |
| SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }} | |
| - name: Check for schema drift | |
| run: | | |
| OUTPUT=$(supabase db diff --linked 2>&1) || true | |
| if [ -n "$OUTPUT" ] && [ "$OUTPUT" != "No changes found" ]; then | |
| echo "::warning::Schema drift detected against dev project" | |
| echo "### Schema Drift Detected" >> "$GITHUB_STEP_SUMMARY" | |
| echo '```sql' >> "$GITHUB_STEP_SUMMARY" | |
| echo "$OUTPUT" >> "$GITHUB_STEP_SUMMARY" | |
| echo '```' >> "$GITHUB_STEP_SUMMARY" | |
| else | |
| echo "### Schema Drift — None" >> "$GITHUB_STEP_SUMMARY" | |
| fi | |
| working-directory: packages/db | |
| env: | |
| SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }} | |
| SUPABASE_DB_PASSWORD: ${{ secrets.SUPABASE_DEV_DB_PASSWORD }} | |
| generate-types: | |
| name: Generate Types | |
| needs: [migrate] | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| ref: dev | |
| - uses: supabase/setup-cli@v1 | |
| with: | |
| version: latest | |
| - name: Link to dev project | |
| run: supabase link --project-ref "${{ secrets.SUPABASE_DEV_PROJECT_REF }}" | |
| working-directory: packages/db | |
| env: | |
| SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }} | |
| - name: Generate TypeScript types | |
| run: supabase gen types typescript --linked > types/database.ts | |
| working-directory: packages/db | |
| env: | |
| SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }} | |
| SUPABASE_DB_PASSWORD: ${{ secrets.SUPABASE_DEV_DB_PASSWORD }} | |
| - name: Create PR if types changed | |
| uses: peter-evans/create-pull-request@v7 | |
| id: types-pr | |
| with: | |
| branch: chore/regenerate-db-types | |
| commit-message: "chore(db): regenerate database types" | |
| title: "chore(db): regenerate database types" | |
| body: | | |
| Auto-generated by the **Deploy Dev** workflow after migrations were applied. | |
| This PR updates `packages/db/types/database.ts` to reflect the latest dev schema. | |
| add-paths: packages/db/types/database.ts | |
| delete-branch: true | |
| sign-commits: true | |
| - name: Enable auto-merge | |
| if: steps.types-pr.outputs.pull-request-number | |
| run: gh pr merge "$PR_NUMBER" --auto --squash --delete-branch | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| PR_NUMBER: ${{ steps.types-pr.outputs.pull-request-number }} | |
| - name: Summary | |
| run: | | |
| if [ "$PR_NUMBER" ]; then | |
| echo "### Types — PR [#${PR_NUMBER}]($PR_URL) created (auto-merge enabled) ✅" >> "$GITHUB_STEP_SUMMARY" | |
| else | |
| echo "### Types — No changes ✅" >> "$GITHUB_STEP_SUMMARY" | |
| fi | |
| env: | |
| PR_NUMBER: ${{ steps.types-pr.outputs.pull-request-number }} | |
| PR_URL: ${{ steps.types-pr.outputs.pull-request-url }} | |
| summary: | |
| name: Summary | |
| needs: [migrate, schema-drift, generate-types] | |
| if: always() | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Deploy dev summary | |
| run: | | |
| cat >> "$GITHUB_STEP_SUMMARY" <<'EOF' | |
| ## Deploy Dev Summary | |
| | Job | Status | | |
| |-----|--------| | |
| | Migrations | ${{ needs.migrate.result }} | | |
| | Schema Drift | ${{ needs.schema-drift.result }} | | |
| | Generate Types | ${{ needs.generate-types.result }} | | |
| EOF |