Skip to content

Part 6: Stages and CI

Terminal window
pnpm alchemy deploy --stage staging

Each stage builds its own snapshots, named after the stage, and its own Worker and API key, so changing the snapshots in one stage never reaches another. Without --stage, you deploy to live_$USER.

Sandboxes a stage’s Worker creates carry the stage in their alchemy.stage label. Destroying a stage doesn’t delete them, so list a stage’s sandboxes by that label to clean them up.

CI runs start from an empty disk, so store state where every run can read it:

alchemy.run.ts
{
providers: Layer.mergeAll(Cloudflare.providers(), Daytona.providers()),
state: Alchemy.localState(),
state: Cloudflare.state(),
},

Daytona has no keyless CI login. Create an API key with manage:api_keys (the Worker’s binding creates a key of its own), and add it to the repository’s secrets as DAYTONA_API_KEY. Alchemy reads it instead of a profile.

.github/workflows/deploy.yml
name: Deploy
on:
pull_request:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
env:
DAYTONA_API_KEY: ${{ secrets.DAYTONA_API_KEY }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
STAGE: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || 'prod' }}
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v2
- run: bun install
- run: bun alchemy deploy --stage "$STAGE" --yes

Each pull request gets its own stage, and so its own base: a pull request that changes Base or Warm rebuilds them for that stage only.

.github/workflows/cleanup.yml
name: Cleanup
on:
pull_request:
types: [closed]
jobs:
destroy:
runs-on: ubuntu-latest
env:
DAYTONA_API_KEY: ${{ secrets.DAYTONA_API_KEY }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v2
- run: bun install
- run: bun alchemy destroy --stage "pr-${{ github.event.number }}" --yes

Destroying the stage deletes its snapshots and the Worker’s key.