| 1 | --- |
| 2 | title: Environments |
| 3 | description: Extra deploy environments under a Project. Production is created with the Project — do not duplicate it. |
| 4 | --- |
| 5 | |
| 6 | A [`Railway.Environment`](/providers/railway/environment) is an extra |
| 7 | deploy environment under a [Project](/railway/compute/projects) |
| 8 | (staging, preview, …). The production environment is created with |
| 9 | the Project — do **not** recreate it as an Environment resource. |
| 10 | |
| 11 | ## Create an extra environment |
| 12 | |
| 13 | Alchemy generates a unique name unless you pass one. Production is |
| 14 | already on the Project as `environmentId`. |
| 15 | |
| 16 | ```typescript |
| 17 | const site = yield* Railway.Project("Site"); |
| 18 | const staging = yield* Railway.Environment("Staging", { |
| 19 | project: site, |
| 20 | }); |
| 21 | ``` |
| 22 | |
| 23 | :::note |
| 24 | Prefer omitting `name` in tests and CI so names stay unique and |
| 25 | reclaimable. |
| 26 | ::: |
| 27 | |
| 28 | ## A stable name |
| 29 | |
| 30 | Pass `name` when you need a stable environment name (`staging`). |
| 31 | Changing it later updates the environment in place via |
| 32 | `environmentRename`. |
| 33 | |
| 34 | ```typescript |
| 35 | const staging = yield* Railway.Environment("Staging", { |
| 36 | project: site, |
| 37 | name: "staging", |
| 38 | }); |
| 39 | ``` |
| 40 | |
| 41 | ## Fork from production |
| 42 | |
| 43 | `sourceEnvironmentId` copies services, volumes, configuration, and |
| 44 | variables from another environment. Create-only. |
| 45 | |
| 46 | ```typescript |
| 47 | const staging = yield* Railway.Environment("Staging", { |
| 48 | project: site, |
| 49 | sourceEnvironmentId: site.environmentId, |
| 50 | }); |
| 51 | ``` |
| 52 | |
| 53 | ## Target an environment from a child |
| 54 | |
| 55 | Services, Volumes, Postgres, Redis, Variables, and Buckets default |
| 56 | to the Project's primary environment. Pass `environment` to put them |
| 57 | in staging. |
| 58 | |
| 59 | ```typescript |
| 60 | const api = yield* Railway.Service("StagingApi", { |
| 61 | project: site, |
| 62 | environment: staging, |
| 63 | image: "hashicorp/http-echo", |
| 64 | port: 5678, |
| 65 | }); |
| 66 | ``` |
| 67 | |
| 68 | :::caution[Changing `project` replaces the Environment] |
| 69 | The Environment is created in the new Project. The old Environment |
| 70 | is deleted. |
| 71 | ::: |
| 72 | |
| 73 | ## Module-scope declarations |
| 74 | |
| 75 | Declare the Project once. Pass it into every child. |
| 76 | |
| 77 | ```typescript |
| 78 | // src/project.ts |
| 79 | import * as Railway from "alchemy/Railway"; |
| 80 | |
| 81 | export const Site = Railway.Project("Site"); |
| 82 | export const Staging = Railway.Environment("Staging", { |
| 83 | project: Site, |
| 84 | }); |
| 85 | ``` |
| 86 | |
| 87 | Resource-valued props accept the resource or an Effect producing it. |
| 88 | |
| 89 | ## Where next |
| 90 | |
| 91 | [Projects](/railway/compute/projects) own the production environment. |
| 92 | [Services](/railway/compute/services) take `environment`. |
| 93 | The [`Environment` reference](/providers/railway/environment) lists |
| 94 | every prop. |