Skip to content

Part 6: Stages and CI

Terminal window
pnpm alchemy deploy --stage staging

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

Stages share your E2B team. Sandboxes a stage’s Worker creates carry the stage in their metadata (alchemy::stage), so you can list one stage’s sandboxes without touching another’s:

const staging = yield* sandboxes.list({ metadata: { "alchemy::stage": "staging" } });

Destroying a stage leaves its sandboxes running until they time out.

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

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

The Worker’s binding creates an API key at deploy, which takes your E2B access token: an API key can’t create other keys. Add the token to the repository’s secrets as E2B_ACCESS_TOKEN, and your team’s id as E2B_TEAM_ID. Alchemy reads them instead of a profile.

.github/workflows/deploy.yml
name: Deploy
on:
pull_request:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
env:
E2B_ACCESS_TOKEN: ${{ secrets.E2B_ACCESS_TOKEN }}
E2B_TEAM_ID: ${{ secrets.E2B_TEAM_ID }}
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 MyTemplate or Warm rebuilds them for that stage only. E2B has no keyless CI login, so the workflow uses the token.

.github/workflows/cleanup.yml
name: Cleanup
on:
pull_request:
types: [closed]
jobs:
destroy:
runs-on: ubuntu-latest
env:
E2B_ACCESS_TOKEN: ${{ secrets.E2B_ACCESS_TOKEN }}
E2B_TEAM_ID: ${{ secrets.E2B_TEAM_ID }}
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 template, its snapshot and the Worker’s key.