Part 6: Stages and CI
Deploy another stage
Section titled “Deploy another stage”pnpm alchemy deploy --stage stagingbun alchemy deploy --stage stagingnpx alchemy deploy --stage stagingyarn alchemy deploy --stage stagingEach 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.
Keep state in Cloudflare
Section titled “Keep state in Cloudflare”CI runs start from an empty disk, so store state where every run can read it:
{ providers: Layer.mergeAll(Cloudflare.providers(), E2B.providers()), state: Alchemy.localState(), state: Cloudflare.state(), },Store the token
Section titled “Store the token”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.
Deploy per pull request
Section titled “Deploy per pull request”name: Deployon: 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" --yesEach 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.
Clean up closed pull requests
Section titled “Clean up closed pull requests”name: Cleanupon: 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 }}" --yesDestroying the stage deletes its template, its snapshot and the Worker’s key.