environment
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseEnvironment Configuration
环境配置
Read and edit Railway environment configuration using the CLI.
使用CLI读取和编辑Railway环境配置。
Prerequisites
前置条件
Requires Railway CLI v4.27.3+. Check with:
bash
railway --versionIf below 4.27.3, upgrade:
bash
railway upgrade需要Railway CLI v4.27.3+。使用以下命令检查版本:
bash
railway --version如果版本低于4.27.3,请执行升级:
bash
railway upgradeQuick Actions
快速操作
When user asks "what's the config" or "show configuration":
bash
railway environment config --jsonPresent: source (repo/image), build settings, deploy settings, variables per service.
When user asks "what variables" or "show env vars":
Same command — includes variables per service and shared variables.
railway environment config --jsonFor rendered (resolved) variable values:
railway variables --json当用户询问“what's the config”或“show configuration”时:
bash
railway environment config --json展示内容:源(仓库/镜像)、构建设置、部署设置、各服务的变量。
当用户询问“what variables”或“show env vars”时:
使用相同命令 —— 包含各服务的变量以及共享变量。
railway environment config --json如需查看**渲染后(已解析)**的变量值:
railway variables --jsonWhen to Use
使用场景
- User wants to create a new environment
- User wants to duplicate an environment (e.g., "copy production to staging")
- User wants to switch to a different environment
- User asks about current build/deploy settings, variables, replicas, health checks, domains
- User asks to change service source (Docker image, branch, commit, root directory)
- User wants to connect a service to a GitHub repo
- User wants to deploy from a GitHub repo (create empty service first via skill, then use this)
new - User asks to change build or start command
- User wants to add/update/delete environment variables
- User wants to change replica count or configure health checks
- User asks to delete a service, volume, or bucket
- Auto-fixing build errors detected in logs
- 用户想要创建新环境
- 用户想要复制环境(例如“将生产环境复制到预发布环境”)
- 用户想要切换到其他环境
- 用户询问当前的构建/部署设置、变量、副本数、健康检查、域名
- 用户想要更改服务源(Docker镜像、分支、提交记录、根目录)
- 用户想要将服务关联到GitHub仓库
- 用户想要从GitHub仓库部署(先通过Skill创建空服务,再使用本Skill)
new - 用户想要更改构建或启动命令
- 用户想要添加/更新/删除环境变量
- 用户想要更改副本数或配置健康检查
- 用户想要删除服务、存储卷或存储桶
- 自动修复日志中检测到的构建错误
Create Environment
创建环境
Create a new environment in the linked project:
bash
railway environment new <name>Duplicate an existing environment:
bash
railway environment new staging --duplicate productionWith service-specific variables:
bash
railway environment new staging --duplicate production --service-variable api PORT=3001在关联的项目中创建新环境:
bash
railway environment new <name>复制现有环境:
bash
railway environment new staging --duplicate production包含服务特定变量:
bash
railway environment new staging --duplicate production --service-variable api PORT=3001Switch Environment
切换环境
Link a different environment to the current directory:
bash
railway environment <name>Or by ID:
bash
railway environment <environment-id>将其他环境关联到当前目录:
bash
railway environment <name>或通过ID切换:
bash
railway environment <environment-id>Get Context
获取上下文
JSON output — project/environment IDs and service list:
bash
railway status --jsonExtract:
- — project ID
project.id - — environment ID
environment.id
Plain output — linked service name:
bash
railway statusShows line with the currently linked service.
Service: <name>JSON输出 —— 项目/环境ID和服务列表:
bash
railway status --json提取内容:
- —— 项目ID
project.id - —— 环境ID
environment.id
纯文本输出 —— 当前关联的服务名称:
bash
railway status展示包含的行,即当前关联的服务。
Service: <name>Resolve Service ID
解析服务ID
Get service IDs from the environment config:
bash
railway environment config --json | jq '.services | keys'Map service IDs to names via status:
bash
railway status --jsonThe array contains for each service. Match against the service keys from .
project.services{ id, name }environment config从环境配置中获取服务ID:
bash
railway environment config --json | jq '.services | keys'通过状态信息映射服务ID与名称:
bash
railway status --jsonproject.services{ id, name }environment configRead Configuration
读取配置
Fetch current environment configuration:
bash
railway environment config --json获取当前环境配置:
bash
railway environment config --jsonResponse Structure
响应结构
json
{
"services": {
"<serviceId>": {
"source": { "repo": "...", "branch": "main" },
"build": { "buildCommand": "npm run build", "builder": "NIXPACKS" },
"deploy": {
"startCommand": "npm start",
"multiRegionConfig": { "us-west2": { "numReplicas": 1 } }
},
"variables": { "NODE_ENV": { "value": "production" } },
"networking": { "serviceDomains": {}, "customDomains": {} }
}
},
"sharedVariables": { "DATABASE_URL": { "value": "..." } }
}For complete field reference, see reference/environment-config.md.
For variable syntax and service wiring patterns, see reference/variables.md.
json
{
"services": {
"<serviceId>": {
"source": { "repo": "...", "branch": "main" },
"build": { "buildCommand": "npm run build", "builder": "NIXPACKS" },
"deploy": {
"startCommand": "npm start",
"multiRegionConfig": { "us-west2": { "numReplicas": 1 } }
},
"variables": { "NODE_ENV": { "value": "production" } },
"networking": { "serviceDomains": {}, "customDomains": {} }
}
},
"sharedVariables": { "DATABASE_URL": { "value": "..." } }
}如需完整字段参考,请查看reference/environment-config.md。
如需了解变量语法和服务连接模式,请查看reference/variables.md。
Get Rendered Variables
获取渲染后的变量
environment config${{shared.DOMAIN}}To see rendered (resolved) values as they appear at runtime:
bash
undefinedenvironment config${{shared.DOMAIN}}如需查看**渲染后(已解析)**的运行时实际值:
bash
undefinedCurrent linked service
当前关联的服务
railway variables --json
railway variables --json
Specific service
指定服务
railway variables --service <service-name> --json
**When to use:**
- Debugging connection issues (see actual URLs/ports)
- Verifying variable resolution is correct
- Viewing Railway-injected values (RAILWAY_*)railway variables --service <service-name> --json
**使用场景:**
- 调试连接问题(查看实际URL/端口)
- 验证变量解析是否正确
- 查看Railway注入的值(RAILWAY_*开头的变量)Edit Configuration
编辑配置
Pass a JSON patch to to apply changes. The patch is merged with existing config and committed immediately, triggering deploys.
railway environment editbash
railway environment edit --json <<< '<json-patch>'With a commit message:
bash
railway environment edit -m "description of change" --json <<< '<json-patch>'通过向传递JSON补丁来应用更改。补丁会与现有配置合并并立即提交,触发部署。
railway environment editbash
railway environment edit --json <<< '<json-patch>'添加提交信息:
bash
railway environment edit -m "变更描述" --json <<< '<json-patch>'Examples
示例
Set build command:
bash
railway environment edit --json <<< '{"services":{"SERVICE_ID":{"build":{"buildCommand":"npm run build"}}}}'Add variable:
bash
railway environment edit -m "add API_KEY" --json <<< '{"services":{"SERVICE_ID":{"variables":{"API_KEY":{"value":"secret"}}}}}'Delete variable:
bash
railway environment edit --json <<< '{"services":{"SERVICE_ID":{"variables":{"OLD_VAR":null}}}}'Delete service:
bash
railway environment edit --json <<< '{"services":{"SERVICE_ID":{"isDeleted":true}}}'Set replicas:
bash
railway environment edit --json <<< '{"services":{"SERVICE_ID":{"deploy":{"multiRegionConfig":{"us-west2":{"numReplicas":3}}}}}}'Add shared variable:
bash
railway environment edit --json <<< '{"sharedVariables":{"DATABASE_URL":{"value":"postgres://..."}}}'设置构建命令:
bash
railway environment edit --json <<< '{"services":{"SERVICE_ID":{"build":{"buildCommand":"npm run build"}}}}'添加变量:
bash
railway environment edit -m "添加API_KEY" --json <<< '{"services":{"SERVICE_ID":{"variables":{"API_KEY":{"value":"secret"}}}}}'删除变量:
bash
railway environment edit --json <<< '{"services":{"SERVICE_ID":{"variables":{"OLD_VAR":null}}}}'删除服务:
bash
railway environment edit --json <<< '{"services":{"SERVICE_ID":{"isDeleted":true}}}'设置副本数:
bash
railway environment edit --json <<< '{"services":{"SERVICE_ID":{"deploy":{"multiRegionConfig":{"us-west2":{"numReplicas":3}}}}}}'添加共享变量:
bash
railway environment edit --json <<< '{"sharedVariables":{"DATABASE_URL":{"value":"postgres://..."}}}'Batching Multiple Changes
批量变更
Include multiple fields in a single patch to apply them atomically:
bash
railway environment edit -m "configure build, start, and env" --json <<< '{"services":{"SERVICE_ID":{"build":{"buildCommand":"npm run build"},"deploy":{"startCommand":"npm start"},"variables":{"NODE_ENV":{"value":"production"}}}}}'在单个补丁中包含多个字段,以原子方式应用变更:
bash
railway environment edit -m "配置构建、启动命令及环境变量" --json <<< '{"services":{"SERVICE_ID":{"build":{"buildCommand":"npm run build"},"deploy":{"startCommand":"npm start"},"variables":{"NODE_ENV":{"value":"production"}}}}}'Error Handling
错误处理
Command Not Found
命令未找到
If is not recognized, upgrade the CLI:
railway environment editbash
railway upgrade如果未被识别,请升级CLI:
railway environment editbash
railway upgradeService Not Found
服务未找到
Service "foo" not found in project. Available services: api, web, workerService "foo" not found in project. Available services: api, web, workerInvalid Configuration
配置无效
Common issues:
- and
buildCommandcannot be identicalstartCommand - only valid with NIXPACKS builder
buildCommand - only valid with DOCKERFILE builder
dockerfilePath
常见问题:
- 和
buildCommand不能相同startCommand - 仅在使用NIXPACKS构建器时有效
buildCommand - 仅在使用DOCKERFILE构建器时有效
dockerfilePath
No Permission
无权限
You don't have permission to modify this environment. Check your Railway role.You don't have permission to modify this environment. Check your Railway role.No Linked Project
未关联项目
No project linked. Run `railway link` to link a project.No project linked. Run `railway link` to link a project.Composability
组合使用
- Create service: Use skill
service - View logs: Use skill
deployment - Add domains: Use skill
domain - Deploy local code: Use skill
deploy
- 创建服务:使用Skill
service - 查看日志:使用Skill
deployment - 添加域名:使用Skill
domain - 部署本地代码:使用Skill
deploy