使用 TeamCity 部署
JetBrains CI/CD 服务器,支持 .NET 和 Java,基于 DSL 的流水线配置。
CI/CD进阶
为什么
TeamCity 提供优秀的 .NET/Java 工具链、VCS 支持的 DSL 和可靠的构建链与制品依赖。DORA 2024 DevOps 状态报告显示精英团队部署频率比低绩效团队高 973 倍。
带自动化测试的 CI/CD 管道将变更失败率降至 5-15%,而手动部署为 40-60%。
何时使用
需要 IDE 集成 CI 和丰富构建功能的 Java 或 .NET 项目。
如何做
# 团队City Kotlin DSL
version = "2024.1"
project {
buildType {
id = "Build"
name = "Build and Test"
steps {
gradle { tasks = "clean build" }
}
artifactRules = "+:build/libs/*.jar"
}
buildType {
id = "Deploy"
name = "Deploy to Prod"
dependsOn(AbsoluteId("Build"))
steps {
script {
scriptContent = "kubectl apply -f k8s/"
}
}
}
}
4. 验证部署 — curl -I https://your-app.example.com
5. 监控日志 — 前 5 分钟检查错误
6. 需要时回滚 — 回退到上一版本
示例
// 配置 — deploy-teamcity
const config = require("./config");
# Docker-based 团队City 代理
FROM jetbrains/teamcity-agent:latest
ENV SERVER_URL=http://teamcity:8111
COPY gradle.properties /opt/buildagent/conf/
常见问题
- ⚠Agent 磁盘满 — 定期清理构建制品
- ⚠DSL 不同步 — 用 TeamCity DSL watcher
- ⚠环境间配置漂移 — 使用基础设施即代码(Terraform/Pulumi)保持 dev/staging/prod 一致
最佳实践
- ✓用 Kotlin DSL — 版本控制的流水线配置
- ✓用构建链和制品依赖 — 避免重复构建
- ✓设置健康检查和优雅关闭 — 处理 SIGTERM 以在退出前排空进行中的请求