|
| 1 | +name: Preview Environment |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + pr_number: |
| 9 | + description: "PR number to deploy" |
| 10 | + required: true |
| 11 | + type: number |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: read |
| 15 | + pull-requests: write |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: preview-${{ github.event.issue.number || inputs.pr_number }} |
| 19 | + cancel-in-progress: true |
| 20 | + |
| 21 | +jobs: |
| 22 | + deploy-preview: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + if: | |
| 25 | + (github.event_name == 'issue_comment' && |
| 26 | + github.event.issue.pull_request && |
| 27 | + contains(github.event.comment.body, '/deploy-preview')) || |
| 28 | + github.event_name == 'workflow_dispatch' |
| 29 | + env: |
| 30 | + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 31 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 32 | + CLOUDFLARE_DATABASE_ID: ${{ secrets.CLOUDFLARE_DATABASE_ID }} |
| 33 | + PR_NUMBER: ${{ github.event_name == 'workflow_dispatch' && inputs.pr_number || github.event.issue.number }} |
| 34 | + steps: |
| 35 | + - name: Add reaction to comment |
| 36 | + if: github.event_name == 'issue_comment' |
| 37 | + uses: actions/github-script@v7 |
| 38 | + with: |
| 39 | + script: | |
| 40 | + github.rest.reactions.createForIssueComment({ |
| 41 | + owner: context.repo.owner, |
| 42 | + repo: context.repo.repo, |
| 43 | + comment_id: context.payload.comment.id, |
| 44 | + content: 'rocket' |
| 45 | + }); |
| 46 | +
|
| 47 | + - name: Get PR branch |
| 48 | + id: pr-branch |
| 49 | + uses: actions/github-script@v7 |
| 50 | + with: |
| 51 | + script: | |
| 52 | + const pr = await github.rest.pulls.get({ |
| 53 | + owner: context.repo.owner, |
| 54 | + repo: context.repo.repo, |
| 55 | + pull_number: process.env.PR_NUMBER |
| 56 | + }); |
| 57 | + core.setOutput('ref', pr.data.head.ref); |
| 58 | + core.setOutput('sha', pr.data.head.sha); |
| 59 | +
|
| 60 | + - name: Checkout PR branch |
| 61 | + uses: actions/checkout@v4 |
| 62 | + with: |
| 63 | + ref: ${{ steps.pr-branch.outputs.sha }} |
| 64 | + |
| 65 | + - name: Setup PNPM |
| 66 | + uses: pnpm/action-setup@v4 |
| 67 | + |
| 68 | + - name: Setup Node.js |
| 69 | + uses: actions/setup-node@v4 |
| 70 | + with: |
| 71 | + node-version: 22 |
| 72 | + cache: pnpm |
| 73 | + |
| 74 | + - name: Install dependencies |
| 75 | + run: pnpm install --frozen-lockfile |
| 76 | + |
| 77 | + - name: Generate wrangler.toml from example |
| 78 | + run: | |
| 79 | + cp wrangler.toml.example wrangler.toml |
| 80 | + sed -i "s/PLACEHOLDER_DATABASE_ID/${CLOUDFLARE_DATABASE_ID}/g" wrangler.toml |
| 81 | +
|
| 82 | + - name: Generate API wrangler.toml from example |
| 83 | + working-directory: apps/api |
| 84 | + run: | |
| 85 | + cp wrangler.toml.example wrangler.toml |
| 86 | + sed -i "s/PLACEHOLDER_DATABASE_ID/${CLOUDFLARE_DATABASE_ID}/g" wrangler.toml |
| 87 | +
|
| 88 | + - name: Deploy API to preview environment |
| 89 | + working-directory: apps/api |
| 90 | + run: | |
| 91 | + WORKER_NAME="tabitabi-api-preview-pr-${PR_NUMBER}" |
| 92 | + pnpm wrangler deploy --name ${WORKER_NAME} --env preview |
| 93 | +
|
| 94 | + - name: Get API preview URL |
| 95 | + id: api-url |
| 96 | + run: | |
| 97 | + echo "url=https://tabitabi-api-preview-pr-${PR_NUMBER}.oranda.workers.dev/api/v1" >> $GITHUB_OUTPUT |
| 98 | +
|
| 99 | + - name: Build web with preview API URL |
| 100 | + run: pnpm run build |
| 101 | + env: |
| 102 | + VITE_API_URL: ${{ steps.api-url.outputs.url }} |
| 103 | + |
| 104 | + - name: Deploy Web to Pages preview |
| 105 | + id: pages-deploy |
| 106 | + working-directory: apps/web |
| 107 | + run: | |
| 108 | + BRANCH_NAME="preview-pr-${PR_NUMBER}" |
| 109 | + OUTPUT=$(pnpm wrangler pages deploy .svelte-kit/cloudflare --project-name=tabitabi --branch=${BRANCH_NAME}) |
| 110 | + echo "$OUTPUT" |
| 111 | + URL=$(echo "$OUTPUT" | grep -oP 'https://[a-z0-9-]+\.tabitabi\.pages\.dev' | head -1) |
| 112 | + echo "url=${URL}" >> $GITHUB_OUTPUT |
| 113 | +
|
| 114 | + - name: Comment PR with preview URLs |
| 115 | + uses: actions/github-script@v7 |
| 116 | + with: |
| 117 | + script: | |
| 118 | + const apiUrl = '${{ steps.api-url.outputs.url }}'; |
| 119 | + const webUrl = '${{ steps.pages-deploy.outputs.url }}'; |
| 120 | +
|
| 121 | + const comment = `## 🚀 Preview Environment Ready |
| 122 | +
|
| 123 | + Your preview environment has been deployed: |
| 124 | +
|
| 125 | + - **Web App**: ${webUrl} |
| 126 | + - **API**: ${apiUrl} |
| 127 | +
|
| 128 | + The Web app is configured to access the preview API automatically. |
| 129 | + To update this preview, comment \`/deploy-preview\` again. |
| 130 | +
|
| 131 | + > **Note**: Preview environments share the same database as production.`; |
| 132 | +
|
| 133 | + github.rest.issues.createComment({ |
| 134 | + issue_number: process.env.PR_NUMBER, |
| 135 | + owner: context.repo.owner, |
| 136 | + repo: context.repo.repo, |
| 137 | + body: comment |
| 138 | + }); |
0 commit comments