多环境管理 #
环境管理概述 #
在实际开发中,通常需要多个环境来支持不同的开发阶段。Heroku 提供了 Pipeline 和 Review Apps 来管理多环境。
典型环境结构 #
text
┌─────────────────────────────────────────────────────┐
│ 多环境架构 │
├─────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Review Apps │ │
│ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │
│ │ │ PR #101 │ │ PR #102 │ │ PR #103 │ │ │
│ │ │ 临时环境│ │ 临时环境│ │ 临时环境│ │ │
│ │ └─────────┘ └─────────┘ └─────────┘ │ │
│ └─────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Development │ │
│ │ ┌─────────────────────────────────────┐ │ │
│ │ │ myapp-dev │ │ │
│ │ │ 开发环境 │ │ │
│ │ └─────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Staging │ │
│ │ ┌─────────────────────────────────────┐ │ │
│ │ │ myapp-staging │ │ │
│ │ │ 测试环境 │ │ │
│ │ └─────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Production │ │
│ │ ┌─────────────────────────────────────┐ │ │
│ │ │ myapp-production │ │ │
│ │ │ 生产环境 │ │ │
│ │ └─────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────┘
创建 Pipeline #
创建 Pipeline #
bash
# 创建 Pipeline 并添加 staging 应用
heroku pipelines:create myapp-pipeline -a myapp-staging -s staging
# 添加 production 应用
heroku pipelines:add myapp-pipeline -a myapp-production -s production
# 添加 development 应用
heroku pipelines:add myapp-pipeline -a myapp-dev -s development
查看 Pipeline #
bash
# 查看 Pipeline 信息
heroku pipelines:info myapp-pipeline
# 输出示例
# === myapp-pipeline
# Development: myapp-dev
# Staging: myapp-staging
# Production: myapp-production
Pipeline 管理 #
bash
# 列出所有 Pipeline
heroku pipelines
# 更新 Pipeline
heroku pipelines:update myapp-pipeline
# 删除 Pipeline
heroku pipelines:destroy myapp-pipeline
环境配置 #
创建各环境应用 #
bash
# 创建开发环境
heroku create myapp-dev --region us
# 创建测试环境
heroku create myapp-staging --region us
# 创建生产环境
heroku create myapp-production --region us
配置环境变量 #
bash
# 开发环境配置
heroku config:set --app myapp-dev \
NODE_ENV=development \
LOG_LEVEL=debug \
DATABASE_URL=postgres://...
# 测试环境配置
heroku config:set --app myapp-staging \
NODE_ENV=staging \
LOG_LEVEL=info \
DATABASE_URL=postgres://...
# 生产环境配置
heroku config:set --app myapp-production \
NODE_ENV=production \
LOG_LEVEL=warn \
DATABASE_URL=postgres://...
配置 Add-ons #
bash
# 开发环境 Add-ons
heroku addons:create heroku-postgresql:mini --app myapp-dev
heroku addons:create heroku-redis:mini --app myapp-dev
# 测试环境 Add-ons
heroku addons:create heroku-postgresql:basic --app myapp-staging
heroku addons:create heroku-redis:mini --app myapp-staging
# 生产环境 Add-ons
heroku addons:create heroku-postgresql:standard-0 --app myapp-production
heroku addons:create heroku-redis:premium-0 --app myapp-production
Review Apps #
启用 Review Apps #
- 在 Heroku Dashboard 中打开 Pipeline
- 点击 “Enable Review Apps”
- 选择父应用(通常是 Staging)
- 配置 app.json
app.json 配置 #
json
{
"name": "myapp",
"description": "My application",
"repository": "https://github.com/username/myapp",
"website": "https://myapp.herokuapp.com",
"env": {
"NODE_ENV": {
"description": "Environment",
"value": "review"
},
"SECRET_KEY": {
"description": "Secret key",
"generator": "secret"
}
},
"formation": {
"web": {
"quantity": 1,
"size": "eco"
}
},
"addons": [
{
"plan": "heroku-postgresql:mini"
}
],
"scripts": {
"postdeploy": "npm run db:migrate && npm run db:seed:review"
},
"environments": {
"review": {
"addons": [
{
"plan": "heroku-postgresql:mini"
}
],
"scripts": {
"postdeploy": "npm run db:migrate && npm run db:seed:test"
}
}
}
}
Review Apps 配置选项 #
text
┌─────────────────────────────────────────────────────┐
│ Review Apps 配置 │
├─────────────────────────────────────────────────────┤
│ │
│ 创建设置: │
│ ☑ 为每个 PR 自动创建 Review App │
│ ☐ 等待 CI 通过后再创建 │
│ │
│ 销毁设置: │
│ ☑ PR 关闭时自动销毁 │
│ ☑ 超过 N 天后自动销毁 │
│ │
│ 父应用: │
│ ○ myapp-staging │
│ │
└─────────────────────────────────────────────────────┘
管理 Review Apps #
bash
# 查看所有 Review Apps
heroku apps --filter "myapp-review"
# 删除特定 Review App
heroku apps:destroy myapp-review-pr123 --confirm myapp-review-pr123
# 查看 Review App 日志
heroku logs --tail --app myapp-review-pr123
部署流程 #
开发到测试流程 #
bash
# 1. 开发环境部署
git push heroku-dev main
# 2. 测试验证
heroku open --app myapp-dev
# 3. 推广到 Staging
heroku pipelines:promote -a myapp-dev
# 4. 验证 Staging
heroku open --app myapp-staging
测试到生产流程 #
bash
# 1. 确保 Staging 验证通过
heroku open --app myapp-staging
# 2. 推广到 Production
heroku pipelines:promote -a myapp-staging
# 3. 监控 Production
heroku logs --tail --app myapp-production
完整部署流程图 #
text
┌─────────────────────────────────────────────────────┐
│ 完整部署流程 │
├─────────────────────────────────────────────────────┤
│ │
│ Feature Branch │
│ │ │
│ ▼ │
│ ┌─────────┐ │
│ │ 创建 PR │ │
│ └────┬────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Review App (自动创建) │ │
│ │ - 运行测试 │ │
│ │ - 功能验证 │ │
│ │ - 代码审查 │ │
│ └─────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────┐ │
│ │ 合并 PR │ │
│ └────┬────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Development (自动部署) │ │
│ │ - 集成测试 │ │
│ │ - 团队测试 │ │
│ └─────────────────────────────────────────────┘ │
│ │ │
│ │ 手动推广 │
│ ▼ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Staging │ │
│ │ - UAT 测试 │ │
│ │ - 性能测试 │ │
│ │ - 安全扫描 │ │
│ └─────────────────────────────────────────────┘ │
│ │ │
│ │ 手动推广 │
│ ▼ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Production │ │
│ │ - 监控告警 │ │
│ │ - 用户验证 │ │
│ └─────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────┘
环境同步 #
配置同步 #
bash
# 从 Staging 复制配置到 Production
heroku config -s --app myapp-staging | \
grep -v DATABASE_URL | \
heroku config:set --app myapp-production
# 或使用插件
heroku plugins:install heroku-config
heroku config:pull --app myapp-staging -o .env.staging
heroku config:push --app myapp-production -f .env.staging
数据同步 #
bash
# 从生产环境导出数据
heroku pg:backups:capture --app myapp-production
heroku pg:backups:download --app myapp-production
# 导入到测试环境
heroku pg:backups:restore DATABASE_URL --app myapp-staging --confirm myapp-staging
环境变量管理 #
环境特定配置 #
javascript
// config/index.js
const config = {
development: {
database: {
url: process.env.DATABASE_URL || 'postgres://localhost:5432/dev'
},
redis: {
url: process.env.REDIS_URL || 'redis://localhost:6379'
},
logLevel: 'debug'
},
staging: {
database: {
url: process.env.DATABASE_URL
},
redis: {
url: process.env.REDIS_URL
},
logLevel: 'info'
},
production: {
database: {
url: process.env.DATABASE_URL,
poolSize: 20
},
redis: {
url: process.env.REDIS_URL
},
logLevel: 'warn'
}
};
const env = process.env.NODE_ENV || 'development';
module.exports = config[env];
配置模板 #
bash
# .env.template - 提交到仓库
NODE_ENV=
DATABASE_URL=
REDIS_URL=
SECRET_KEY=
LOG_LEVEL=
成本优化 #
环境成本规划 #
text
┌─────────────────────────────────────────────────────┐
│ 环境成本规划 │
├─────────────────────────────────────────────────────┤
│ │
│ Development │
│ ├── Eco Dyno: $5/月 │
│ ├── Mini PostgreSQL: $5/月 │
│ └── Mini Redis: $15/月 │
│ 小计: $25/月 │
│ │
│ Staging │
│ ├── Standard-1X Dyno: $25/月 │
│ ├── Basic PostgreSQL: $9/月 │
│ └── Mini Redis: $15/月 │
│ 小计: $49/月 │
│ │
│ Production │
│ ├── Standard-2X Dyno x2: $100/月 │
│ ├── Standard-0 PostgreSQL: $50/月 │
│ └── Premium-0 Redis: $30/月 │
│ 小计: $180/月 │
│ │
│ 总计: $254/月 │
│ │
└─────────────────────────────────────────────────────┘
节省成本建议 #
| 建议 | 说明 |
|---|---|
| 使用 Eco Dyno | 开发和 Review Apps |
| 定期清理 Review Apps | 设置自动销毁时间 |
| 按需扩展 | 非工作时间缩减 Dyno |
| 共享 Add-ons | 开发环境共享数据库 |
监控与告警 #
环境监控配置 #
bash
# 为每个环境配置监控
heroku addons:create newrelic:wayne --app myapp-production
heroku addons:create papertrail:choklad --app myapp-production
heroku addons:create librato:meter --app myapp-production
# Staging 环境基础监控
heroku addons:create papertrail:choklad --app myapp-staging
告警配置 #
yaml
# alerting.yml (示例配置)
alerts:
production:
- name: high_response_time
condition: response_time > 1000ms
action: notify_slack
- name: error_rate
condition: error_rate > 5%
action: notify_email, notify_slack
staging:
- name: app_down
condition: app_status == down
action: notify_email
最佳实践 #
1. 环境隔离 #
bash
# 使用不同的:
# - 应用名称
# - 数据库
# - Redis 实例
# - API 密钥
# - 域名
# 避免:
# - 共享生产数据库
# - 使用相同密钥
# - 混用配置
2. 部署检查清单 #
markdown
## 部署到 Staging 检查
- [ ] 代码已合并到主分支
- [ ] 单元测试通过
- [ ] Review App 测试通过
- [ ] 配置已更新
- [ ] 数据库迁移已准备
## 部署到 Production 检查
- [ ] Staging 测试完成
- [ ] 性能测试通过
- [ ] 安全扫描通过
- [ ] 回滚计划已准备
- [ ] 监控已配置
3. 环境命名规范 #
text
命名规范:
myapp-dev # 开发环境
myapp-staging # 测试环境
myapp-production # 生产环境
myapp-review-pr123 # Review App
下一步 #
多环境管理掌握后,接下来学习 Heroku Postgres 了解数据库管理!
最后更新:2026-03-28