Part 5: CI/CD
Deploy a preview for each pull request, and remove it when the pull request closes.
Keep state between runs
Section titled “Keep state between runs”Alchemy.localState() doesn’t survive a CI run. Switch the Stack to a remote state store first, so destroy can find what deploy made.
Add the secrets
Section titled “Add the secrets”Create a team access token for CI in your Convex team settings, then add it to your repository as CONVEX_ACCESS_TOKEN. Convex recommends a service account.
The Stack also deploys the Worker from part 2, so add CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID too. See CI.
Deploy on pull requests
Section titled “Deploy on pull requests”name: Previewon: pull_request: types: [opened, synchronize, reopened]
jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: oven-sh/setup-bun@v2 - run: bun install - run: bun alchemy deploy --stage pr-${{ github.event.number }} --yes env: CONVEX_ACCESS_TOKEN: ${{ secrets.CONVEX_ACCESS_TOKEN }} CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}In CI, Alchemy reads these variables instead of a profile.
Remove previews on close
Section titled “Remove previews on close”name: Cleanupon: pull_request: types: [closed]
jobs: destroy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: oven-sh/setup-bun@v2 - run: bun install - run: bun alchemy destroy --stage pr-${{ github.event.number }} --yes env: CONVEX_ACCESS_TOKEN: ${{ secrets.CONVEX_ACCESS_TOKEN }} CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}