|
| 1 | +name: Automatically create and cleanup Turso DB branches for each PR |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: |
| 6 | + - opened |
| 7 | + - reopened |
| 8 | + - synchronize |
| 9 | + - closed |
| 10 | + |
| 11 | +jobs: |
| 12 | + setup: |
| 13 | + outputs: |
| 14 | + branch_id: ${{ steps.branch_db_id.outputs.result }} |
| 15 | + runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + contents: read |
| 18 | + steps: |
| 19 | + - name: Get branch name |
| 20 | + id: branch_name |
| 21 | + uses: tj-actions/branch-names@v8 |
| 22 | + - name: Checkout |
| 23 | + uses: actions/checkout@v4 |
| 24 | + - name: Generate branched DB ID |
| 25 | + id: branch_db_id |
| 26 | + uses: actions/github-script@v7 |
| 27 | + with: |
| 28 | + result-encoding: string |
| 29 | + script: | |
| 30 | + const { previewDbId } = await import("${{github.workspace}}/src/server/db/utils.js"); |
| 31 | + const dbId = previewDbId(${{ github.event.number }}, "${{steps.branch_name.outputs.current_branch}}"); |
| 32 | + console.log(`Branched DB ID: ${dbId}`); |
| 33 | + return dbId; |
| 34 | +
|
| 35 | + ensure_db_branch: |
| 36 | + name: Ensure the DB branch exists and is up to date |
| 37 | + needs: setup |
| 38 | + if: | |
| 39 | + github.event_name == 'pull_request' && ( |
| 40 | + github.event.action == 'synchronize' |
| 41 | + || github.event.action == 'opened' |
| 42 | + || github.event.action == 'reopened') |
| 43 | + runs-on: ubuntu-latest |
| 44 | + steps: |
| 45 | + - name: Create DB branch |
| 46 | + id: create_db_branch |
| 47 | + run: | |
| 48 | + RESPONSE=$(curl -L -X POST \ |
| 49 | + -H "Authorization: Bearer ${{ secrets.TURSO_API_TOKEN }}" \ |
| 50 | + -H "Content-Type: application/json" \ |
| 51 | + -d '{"name": "${{ needs.setup.outputs.branch_id }}", "group": "${{ secrets.TURSO_GROUP_NAME }}", "seed": {"type": "database", "name": "${{ secrets.TURSO_DATABASE_NAME }}"} }' \ |
| 52 | + "https://api.turso.tech/v1/organizations/${{ secrets.TURSO_ORGANIZATION_NAME }}/databases") |
| 53 | +
|
| 54 | + if [ $? -ne 0 ]; then |
| 55 | + echo "Unable to create database, attempting to fetch the existing database instead" |
| 56 | + RESPONSE=$(curl -L 'https://api.turso.tech/v1/organizations/${{ secrets.TURSO_ORGANIZATION_NAME }}/databases/${{ needs.setup.outputs.branch_id }}' \ |
| 57 | + -H 'Authorization: Bearer ${{ secrets.TURSO_API_TOKEN }}') |
| 58 | + |
| 59 | + if [ $? -ne 0 ]; then |
| 60 | + echo "Could not create or fetch database" |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + fi |
| 64 | +
|
| 65 | + echo $RESPONSE |
| 66 | + HOSTNAME=$(echo $RESPONSE | jq -r '.database.Hostname') |
| 67 | + if [ -z "$HOSTNAME" ]; then |
| 68 | + echo "Hostname not found in response" |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | +
|
| 72 | + echo "hostname=$HOSTNAME" >> $GITHUB_OUTPUT |
| 73 | + - name: Checkout |
| 74 | + uses: actions/checkout@v4 |
| 75 | + - name: Run migrations |
| 76 | + env: |
| 77 | + DATABASE_URL: "libsql://${{ steps.create_db_branch.hostname }}" |
| 78 | + TURSO_AUTH_TOKEN: ${{ secrets.TURSO_API_TOKEN }} |
| 79 | + run: npm install && npm run db:generate && npm run db:migrate |
| 80 | + delete_db_branch: |
| 81 | + name: Delete DB branch and migrate prod |
| 82 | + needs: setup |
| 83 | + if: | |
| 84 | + github.event_name == 'pull_request' && |
| 85 | + github.event.action == 'closed' |
| 86 | + runs-on: ubuntu-latest |
| 87 | + steps: |
| 88 | + - name: Delete DB branch |
| 89 | + run: | |
| 90 | + curl -L -X DELETE 'https://api.turso.tech/v1/organizations/${{ secrets.TURSO_ORGANIZATION_NAME }}/databases/${{ needs.setup.outputs.branch_id }}' \ |
| 91 | + -H 'Authorization: Bearer ${{ secrets.TURSO_API_TOKEN }}' |
| 92 | + # Migrate prod DB, hopefully faster than the vercel build, or at least before anyone tries to open the website |
| 93 | + - name: Checkout |
| 94 | + if: github.event.pull_request.merged == true |
| 95 | + uses: actions/checkout@v4 |
| 96 | + - name: Apply migrations to production |
| 97 | + if: github.event.pull_request.merged == true |
| 98 | + env: |
| 99 | + DATABASE_URL: "libsql://${{ secrets.TURSO_DATABASE_NAME }}" |
| 100 | + TURSO_AUTH_TOKEN: ${{ secrets.TURSO_API_TOKEN }} |
| 101 | + run: npm install && npm run db:generate && npm run db:migrate |
0 commit comments