关于 railway
用于在 Railway 上部署和管理应用的技能,涵盖命令行配置、服务部署、数据库、存储、网络和定时任务。
主要功能
- 01配置构建与部署
- 02设置环境变量
- 03添加 PostgreSQL、Redis 和 MySQL
- 04管理内网和持久卷
- 05配置定时任务与健康检查
适用场景
- 01使用 Railway CLI 部署应用
- 02配置多服务项目
- 03为服务挂载持久存储
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ hoodini/ ai-agents-skills --skill railway安装后在 Claude Code 中输入/railway确认已加载。
详细介绍
Railway 部署
在 Railway 平台上部署和管理应用程序。
快速开始
# Install Railway CLI
npm i -g @railway/cli
# Login
railway login
# Initialize project
railway init
# Deploy
railway up
railway.toml 配置
[build]
builder = "nixpacks"
buildCommand = "npm run build"
[deploy]
startCommand = "npm start"
healthcheckPath = "/health"
healthcheckTimeout = 300
restartPolicyType = "on_failure"
restartPolicyMaxRetries = 3
[service]
internalPort = 3000
Nixpacks 配置
# nixpacks.toml
[phases.setup]
nixPkgs = ["nodejs-18_x", "python311"]
[phases.install]
cmds = ["npm ci"]
[phases.build]
cmds = ["npm run build"]
[start]
cmd = "npm start"
环境变量
# Set variable
railway variables set DATABASE_URL="postgres://..."
# Set from file
railway variables set < .env
# Link to service
railway service
railway variables set API_KEY="secret"
数据库服务
PostgreSQL
# Add PostgreSQL
railway add -d postgres
# Get connection string
railway variables get DATABASE_URL
Redis
railway add -d redis
# Access via REDIS_URL
MySQL
railway add -d mysql
# Access via MYSQL_URL
专用网络
# Services can communicate via internal DNS
# Format: ${{service-name}}.railway.internal
# Example: API calling database service
DATABASE_HOST: ${{postgres.railway.internal}}
DATABASE_PORT: 5432
卷(持久化存储)
# Create volume
railway volume create my-data
# Mount in service
railway volume attach my-data:/app/data
在代码中:
// Data persists across deploys
const dataPath = '/app/data';
fs.writeFileSync(`${dataPath}/file.json`, JSON.stringify(data));
Cron 作业
# railway.toml
[deploy]
startCommand = "node cron.js"
cronSchedule = "0 */6 * * *" # Every 6 hours
多服务设置
my-project/
├── api/
│ ├── railway.toml
│ └── ...
├── worker/
│ ├── railway.toml
│ └── ...
└── frontend/
├── railway.toml
└── ...
分别部署:
cd api && railway up
cd ../worker && railway up
cd ../frontend && railway up
Dockerfile 部署
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
# railway.toml
[build]
builder = "dockerfile"
dockerfilePath = "./Dockerfile"
健康检查
// Express health endpoint
app.get('/health', (req, res) => {
res.status(200).json({
status: 'healthy',
timestamp: new Date().toISOString()
});
});
# railway.toml
[deploy]
healthcheckPath = "/health"
healthcheckTimeout = 100
资源
- Railway 文档:https://docs.railway.app
- Railway CLI:https://docs.railway.app/develop/cli
评论
支持 Markdown