Spin up Railway preview environments for every GitHub PR

By General Input

Give every pull request a fresh Railway preview, post the URLs back to the PR, and tear it down the moment the PR closes.

Integrations

  • Railway
  • GitHub

Type

Deterministic Code

Categories

  • Engineering

Build me a code workflow that gives every pull request its own Railway preview environment and tears it down when the PR closes. The whole thing should be deterministic with no judgement steps.

Trigger: a GitHub webhook listening for pull_request events with actions opened, reopened, synchronize, and closed. The first thing the workflow does is check that the event's repository (owner and name) matches the configured target repo. If it doesn't match, exit immediately so unrelated webhooks never spin anything up.

Name preview environments deterministically as pr-<number> using the PR number from the webhook payload. This way the close handler can find the right environment by name without storing any state.

When the event action is opened or reopened:

1. Call Railway's Create Environment to make a new environment in the configured Railway project, cloning from the designated base environment so all variables and service config copy over. Name it pr-<number> and mark it ephemeral (Railway's isEphemeral flag) so Railway knows it's a preview.

2. For each cloned service in the new environment, call Update Service Instance Settings to point its source branch at the PR's head branch (from pull_request.head.ref).

3. For each cloned service, call Trigger Environment Deploy so the preview build kicks off against the PR branch.

4. For each service, call List Domains to fetch the Railway-provided URLs for the new environment.

5. Call GitHub's Create an Issue Comment on the pull request with a tidy markdown comment listing each service and its preview URL so the reviewer can click straight in.

When the event action is closed:

1. Call Railway's List Environments on the configured project and find the one named pr-<number> for this PR.

2. If it exists, call Delete Environment to clean it up. If it doesn't exist, skip and exit cleanly.

3. Call GitHub's Create an Issue Comment with a short confirmation that the preview was torn down.

For synchronize events (new commits pushed to an open PR), check if the pr-<number> environment already exists. If it does, do nothing in the workflow itself, Railway's normal auto-deploy from the branch will pick up the new commits. If it doesn't exist yet (rare race), run the same create flow as opened.

Inputs to expose at workflow setup: the target GitHub repository (owner and name), the Railway project ID, the Railway base environment ID to clone from, and the GitHub and Railway credentials.

Related prompts

Explore more prompts
Open a Heroku release war room the moment production looks wrongSee what Hacker News really thinks of the tools in your stackSee and fix Terraform variable drift across every workspaceA docs coverage board that catches what shipped undocumentedA technology radar board your architecture group works out ofTake-home review desk that runs candidate code in a sandboxDeploy control room for DigitalOcean App Platform appsClean up the Fly.io preview apps your team left behindFind the dbt models that are burning your warehouse budgetTranslation coverage board for your app's language files