Repository navigation
feat(preview): let each PR preview sleep when idle, after CI - #1186
Merged
Merged
Conversation
A PR environment copies production's settings once, when Railway creates it, and production must never sleep. Config as Code's `environments.pr` block was the way to give previews their own settings, and Railway stops reading it on 2026-12-01, so each preview's own copy is changed after the fact. `preview-sleep.yml` runs on `workflow_run` each time CI finishes with a PR — from main, checking nothing out from the PR, because RAILWAY_API_TOKEN is a workspace token that can change production — and runs `scripts/preview-sleep.ts --pr <n> --apply`. It also takes a manual dispatch for one PR or `all`. The script turns on Serverless for the preview's app and, once the commit the app runs carries the Postgres wake retry, for its Postgres too, with point-in-time recovery off (it uploads every minute). Each service whose running deployment lacks the setting is deployed fresh — a redeploy reuses the old settings snapshot. The app redeploys the commit it already runs, and an app still deploying is left to the CI run that follows. Every check on the target environment keeps production out, and production's settings are re-read before and after each write. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🚅 Deployed to the SahajCloud-pr-1186 environment in sahajcloud
1 service not affected by this PR
|
railway-app
Bot
temporarily deployed
to
sahajcloud / SahajCloud-pr-1186
November 17, 2026 18:30
Destroyed
Contributor
This was referenced Nov 17, 2026
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
PR previews were about $39 of last month's $61 Railway bill: ~5 previews running around the clock, each ~0.5 GB app plus ~0.25 GB Postgres. #1178 removed the job crons that kept an idle preview awake. What remains is turning Railway's Serverless (sleep) on for each preview.
Railway has no surviving way to give PR environments their own settings. A PR environment copies production's settings once, when it is created, and production must never sleep. Config as Code's
environments.proverride did exactly this, but Railway stops reading Config as Code on 2026-12-01. Its replacement,.railway/railway.ts, has no sleep setting, cannot target PR environments, and is never read on deploy. So this changes each preview's own copy after the fact.What
.github/workflows/preview-sleep.yml:workflow_runeach time CI completes for a PR (skipping cancelled runs), plus a manual dispatch for one PR orall.--apply.workflow_run:RAILWAY_API_TOKENis a workspace token that can also change production.workflow_runruns this file and the script frommainand checks nothing out from the PR, so no branch can change what runs with the token.scripts/preview-sleep.ts, with its decision half inscripts/preview-sleep-plan.ts:serviceInstanceDeployV2), never a Redeploy, which reuses the old settings snapshot. The app redeploys the commit it already runs, never the PR head, so the script cannot roll a preview back.--apply(or--force) is passed.SahajCloud-pr-<n>environment forked from production is touched. Railway's settings-update call, given anything other than such a fork, applies to every non-fork environment, production included. Production's sleep flags are re-read before and after every write, and the run fails loudly if either is on.DEPLOYMENT.md: Serverless as the one setting only previews get, a row in the preview table, and the token: what it can do, why it is a workspace token, and how to rotate it.scripts/AGENTS.md: rows for both scripts.Verification
tests/unit/preview-sleep-plan.spec.ts:--applymade all five changes. Afterwards production was unchanged (sleep off on both services, point-in-time recovery on). A second dry run reportedalready sleeps when idle./api/healthanswered200in 3.3 s. The fixture reportedloaded4 s later. The app log shows the refused connection,AggregateError(Node's dual-stack refusal), followed byPostgres refused a connection; retrying while it wakesand[fixture] loaded … 211 unchanged.workflow_runand dispatch only work once the file is onmain.After merge
all:gh workflow run preview-sleep.yml -f pr=all.🤖 Generated with Claude Code