environment

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Environment Configuration

环境配置

Read and edit Railway environment configuration using the CLI.
使用CLI读取和编辑Railway环境配置。

Prerequisites

前置条件

Requires Railway CLI v4.27.3+. Check with:
bash
railway --version
If below 4.27.3, upgrade:
bash
railway upgrade
需要Railway CLI v4.27.3+。使用以下命令检查版本:
bash
railway --version
如果版本低于4.27.3,请执行升级:
bash
railway upgrade

Quick Actions

快速操作

When user asks "what's the config" or "show configuration":
bash
railway environment config --json
Present: source (repo/image), build settings, deploy settings, variables per service.
When user asks "what variables" or "show env vars":
Same command —
railway environment config --json
includes variables per service and shared variables.
For 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 --json

When 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
    new
    skill, then use this)
  • 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仓库部署(先通过
    new
    Skill创建空服务,再使用本Skill)
  • 用户想要更改构建或启动命令
  • 用户想要添加/更新/删除环境变量
  • 用户想要更改副本数或配置健康检查
  • 用户想要删除服务、存储卷或存储桶
  • 自动修复日志中检测到的构建错误

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 production
With 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=3001

Switch 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 --json
Extract:
  • project.id
    — project ID
  • environment.id
    — environment ID
Plain output — linked service name:
bash
railway status
Shows
Service: <name>
line with the currently linked service.
JSON输出 —— 项目/环境ID和服务列表:
bash
railway status --json
提取内容:
  • project.id
    —— 项目ID
  • environment.id
    —— 环境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 --json
The
project.services
array contains
{ id, name }
for each service. Match against the service keys from
environment config
.
从环境配置中获取服务ID:
bash
railway environment config --json | jq '.services | keys'
通过状态信息映射服务ID与名称:
bash
railway status --json
project.services
数组包含每个服务的
{ id, name }
信息,可与
environment config
中的服务键进行匹配。

Read Configuration

读取配置

Fetch current environment configuration:
bash
railway environment config --json
获取当前环境配置:
bash
railway environment config --json

Response 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
returns unrendered variables — template syntax like
${{shared.DOMAIN}}
is preserved. This is correct for management/editing.
To see rendered (resolved) values as they appear at runtime:
bash
undefined
environment config
返回未渲染的变量 —— 保留
${{shared.DOMAIN}}
这类模板语法,这适用于管理/编辑操作。
如需查看**渲染后(已解析)**的运行时实际值:
bash
undefined

Current 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
railway environment edit
to apply changes. The patch is merged with existing config and committed immediately, triggering deploys.
bash
railway environment edit --json <<< '<json-patch>'
With a commit message:
bash
railway environment edit -m "description of change" --json <<< '<json-patch>'
通过向
railway environment edit
传递JSON补丁来应用更改。补丁会与现有配置合并并立即提交,触发部署。
bash
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
railway environment edit
is not recognized, upgrade the CLI:
bash
railway upgrade
如果
railway environment edit
未被识别,请升级CLI:
bash
railway upgrade

Service Not Found

服务未找到

Service "foo" not found in project. Available services: api, web, worker
Service "foo" not found in project. Available services: api, web, worker

Invalid Configuration

配置无效

Common issues:
  • buildCommand
    and
    startCommand
    cannot be identical
  • buildCommand
    only valid with NIXPACKS builder
  • dockerfilePath
    only valid with DOCKERFILE builder
常见问题:
  • buildCommand
    startCommand
    不能相同
  • buildCommand
    仅在使用NIXPACKS构建器时有效
  • dockerfilePath
    仅在使用DOCKERFILE构建器时有效

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
    service
    skill
  • View logs: Use
    deployment
    skill
  • Add domains: Use
    domain
    skill
  • Deploy local code: Use
    deploy
    skill
  • 创建服务:使用
    service
    Skill
  • 查看日志:使用
    deployment
    Skill
  • 添加域名:使用
    domain
    Skill
  • 部署本地代码:使用
    deploy
    Skill