|
| 1 | +name: Deploy to Cloudflare Workers |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened, closed] |
| 6 | + push: |
| 7 | + branches: [main] |
| 8 | + |
| 9 | +jobs: |
| 10 | + # Check code quality on PRs and main pushes |
| 11 | + check: |
| 12 | + if: github.event.action != 'closed' |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v5 |
| 16 | + - uses: ./.github/actions/check |
| 17 | + |
| 18 | + # Deploy preview environment on PRs |
| 19 | + preview: |
| 20 | + if: github.event_name == 'pull_request' && github.event.action != 'closed' |
| 21 | + needs: check |
| 22 | + runs-on: ubuntu-latest |
| 23 | + environment: |
| 24 | + name: preview |
| 25 | + url: https://startkit-preview-pr-${{ github.event.number }}.startkit-dev.workers.dev |
| 26 | + outputs: |
| 27 | + preview-url: ${{ steps.deploy.outputs.deployment-url }} |
| 28 | + db-name: ${{ steps.setup-db.outputs.db-name }} |
| 29 | + steps: |
| 30 | + - uses: actions/checkout@v4 |
| 31 | + |
| 32 | + - name: Setup Bun |
| 33 | + uses: oven-sh/setup-bun@v2 |
| 34 | + with: |
| 35 | + bun-version: latest |
| 36 | + |
| 37 | + - name: Install dependencies |
| 38 | + run: bun install --frozen-lockfile |
| 39 | + |
| 40 | + - name: Setup preview database and run migrations |
| 41 | + id: setup-db |
| 42 | + run: | |
| 43 | + DB_NAME="startkit-preview-pr-${{ github.event.number }}" |
| 44 | + echo "db-name=$DB_NAME" >> $GITHUB_OUTPUT |
| 45 | +
|
| 46 | + # Create preview database and capture the database ID |
| 47 | + bunx wrangler d1 create "$DB_NAME" --output json > db-output.json |
| 48 | + DB_ID=$(cat db-output.json | jq -r '.result.uuid') |
| 49 | + echo "DB_ID=$DB_ID" >> $GITHUB_ENV |
| 50 | +
|
| 51 | + # Run migrations on preview database |
| 52 | + bunx wrangler d1 migrations apply "$DB_NAME" --remote |
| 53 | + env: |
| 54 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 55 | + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 56 | + |
| 57 | + - name: Build application |
| 58 | + run: bun run build |
| 59 | + env: |
| 60 | + BETTER_AUTH_URL: ${{ vars.BETTER_AUTH_URL || format('https://startkit-preview-pr-{0}.startkit-dev.workers.dev', github.event.number) }} |
| 61 | + BETTER_AUTH_SECRET: ${{ secrets.BETTER_AUTH_SECRET }} |
| 62 | + GITHUB_CLIENT_ID: ${{ secrets.OAUTH_GITHUB_CLIENT_ID }} |
| 63 | + GITHUB_CLIENT_SECRET: ${{ secrets.OAUTH_GITHUB_CLIENT_SECRET }} |
| 64 | + |
| 65 | + - name: Create temporary wrangler config for preview |
| 66 | + run: | |
| 67 | + cat > wrangler.preview.jsonc << EOF |
| 68 | + { |
| 69 | + "name": "startkit-preview-pr-${{ github.event.number }}", |
| 70 | + "main": "@tanstack/react-start/server-entry", |
| 71 | + "compatibility_date": "2025-09-24", |
| 72 | + "compatibility_flags": ["nodejs_compat"], |
| 73 | + "observability": { |
| 74 | + "enabled": true |
| 75 | + }, |
| 76 | + "vars": { |
| 77 | + "BETTER_AUTH_URL": "${{ vars.BETTER_AUTH_URL || format('https://startkit-preview-pr-{0}.startkit-dev.workers.dev', github.event.number) }}", |
| 78 | + "GITHUB_CLIENT_ID": "${{ secrets.OAUTH_GITHUB_CLIENT_ID }}" |
| 79 | + }, |
| 80 | + "d1_databases": [ |
| 81 | + { |
| 82 | + "binding": "DB", |
| 83 | + "database_name": "startkit-preview-pr-${{ github.event.number }}", |
| 84 | + "database_id": "${{ env.DB_ID }}" |
| 85 | + } |
| 86 | + ] |
| 87 | + } |
| 88 | + EOF |
| 89 | +
|
| 90 | + - name: Deploy to Cloudflare Workers |
| 91 | + id: deploy |
| 92 | + uses: cloudflare/wrangler-action@v3 |
| 93 | + with: |
| 94 | + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 95 | + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 96 | + command: deploy --config wrangler.preview.jsonc |
| 97 | + |
| 98 | + - name: Comment PR |
| 99 | + uses: actions/github-script@v7 |
| 100 | + with: |
| 101 | + script: | |
| 102 | + github.rest.issues.createComment({ |
| 103 | + issue_number: context.issue.number, |
| 104 | + owner: context.repo.owner, |
| 105 | + repo: context.repo.repo, |
| 106 | + body: `🚀 **Preview deployment is ready!** |
| 107 | +
|
| 108 | + **Preview URL:** ${{ steps.deploy.outputs.deployment-url }} |
| 109 | + **Database:** startkit-preview-pr-${{ github.event.number }} |
| 110 | +
|
| 111 | + This preview will be automatically deleted when the PR is closed.` |
| 112 | + }) |
| 113 | +
|
| 114 | + # Clean up preview environment when PR is closed |
| 115 | + cleanup: |
| 116 | + if: github.event_name == 'pull_request' && github.event.action == 'closed' |
| 117 | + runs-on: ubuntu-latest |
| 118 | + steps: |
| 119 | + - name: Delete preview deployment |
| 120 | + uses: cloudflare/wrangler-action@v3 |
| 121 | + continue-on-error: true |
| 122 | + with: |
| 123 | + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 124 | + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 125 | + command: delete startkit-preview-pr-${{ github.event.number }} --force |
| 126 | + |
| 127 | + - name: Delete preview database |
| 128 | + uses: cloudflare/wrangler-action@v3 |
| 129 | + continue-on-error: true |
| 130 | + with: |
| 131 | + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 132 | + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 133 | + command: d1 delete startkit-preview-pr-${{ github.event.number }} --force |
| 134 | + |
| 135 | + - name: Comment PR |
| 136 | + uses: actions/github-script@v7 |
| 137 | + with: |
| 138 | + script: | |
| 139 | + github.rest.issues.createComment({ |
| 140 | + issue_number: context.issue.number, |
| 141 | + owner: context.repo.owner, |
| 142 | + repo: context.repo.repo, |
| 143 | + body: `🧹 **Preview environment cleaned up** |
| 144 | +
|
| 145 | + The preview deployment and database for this PR have been deleted.` |
| 146 | + }) |
| 147 | +
|
| 148 | + # Deploy to production on main branch |
| 149 | + production: |
| 150 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 151 | + needs: check |
| 152 | + runs-on: ubuntu-latest |
| 153 | + environment: |
| 154 | + name: production |
| 155 | + url: ${{ vars.BETTER_AUTH_URL }} |
| 156 | + steps: |
| 157 | + - uses: actions/checkout@v4 |
| 158 | + |
| 159 | + - name: Setup Bun |
| 160 | + uses: oven-sh/setup-bun@v2 |
| 161 | + with: |
| 162 | + bun-version: latest |
| 163 | + |
| 164 | + - name: Install dependencies |
| 165 | + run: bun install --frozen-lockfile |
| 166 | + |
| 167 | + - name: Run database migrations |
| 168 | + uses: cloudflare/wrangler-action@v3 |
| 169 | + with: |
| 170 | + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 171 | + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 172 | + command: d1 migrations apply DB --remote |
| 173 | + |
| 174 | + - name: Build application |
| 175 | + run: bun run build |
| 176 | + env: |
| 177 | + BETTER_AUTH_URL: ${{ vars.BETTER_AUTH_URL }} |
| 178 | + BETTER_AUTH_SECRET: ${{ secrets.BETTER_AUTH_SECRET }} |
| 179 | + GITHUB_CLIENT_ID: ${{ secrets.OAUTH_GITHUB_CLIENT_ID }} |
| 180 | + GITHUB_CLIENT_SECRET: ${{ secrets.OAUTH_GITHUB_CLIENT_SECRET }} |
| 181 | + |
| 182 | + - name: Deploy to production |
| 183 | + uses: cloudflare/wrangler-action@v3 |
| 184 | + with: |
| 185 | + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 186 | + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 187 | + command: deploy |
0 commit comments