Files
docker_practice/15_cases/ci/devops_workflow.md
Baohua Yang b44c9acd6c Restruct
2026-02-09 12:56:12 -08:00

85 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## DevOps 工作流完整示例
本章将演示一个基于 Docker, Kubernetes Jenkins/GitLab CI 的完整 DevOps 工作流
### 工作流概览
1. **Code**: 开发人员提交代码到 GitLab
2. **Build**: GitLab CI 触发构建任务
3. **Test**: 运行单元测试和集成测试
4. **Package**: 构建 Docker 镜像并推送到 Harbor/Registry
5. **Deploy (Staging)**: 自动部署到测试环境 Kubernetes 集群
6. **Verify**: 人工或自动化验证
7. **Release (Production)**: 审批后自动部署到生产环境
### 关键配置示例
#### 1. Dockerfile 多阶段构建
使用 Docker 多阶段构建可以有效减小镜像体积
Dockerfile 内容如下
```dockerfile
## Build stage
FROM golang:1.18 AS builder
WORKDIR /app
COPY . .
RUN go build -o main .
## Final stage
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/main .
CMD ["./main"]
```
#### 2. GitLab CI 配置
GitLab CI.gitlab-ci.yml配置如下
```yaml
stages:
- test
- build
- deploy
unit_test:
stage: test
image: golang:1.18
script:
- go test ./...
build_image:
stage: build
image: docker:20.10.16
services:
- docker:20.10.16-dind
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
deploy_staging:
stage: deploy
image: dtzar/helm-kubectl
script:
- kubectl config set-cluster k8s --server=$KUBE_URL --insecure-skip-tls-verify=true
- kubectl config set-credentials admin --token=$KUBE_TOKEN
- kubectl config set-context default --cluster=k8s --user=admin
- kubectl config use-context default
- kubectl set image deployment/myapp myapp=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA -n staging
only:
- develop
```
### 最佳实践
1. **不可变基础设施**: 一旦镜像构建完成在各个环境Dev, Staging, Prod中都应该使用同一个镜像 tag (通常是 commit hash)而不是重新构建
2. **配置分离**: 使用 ConfigMap Secret 管理环境特定的配置不要打包进镜像
3. **GitOps**: 考虑引入 ArgoCD将部署配置也作为代码存储在 Git 实现 Git 驱动的部署同步