Release v1.5.0: Restructure chapters and update for Docker v30.x

This commit is contained in:
Baohua Yang
2026-02-04 22:12:38 -08:00
parent b4b0d4160a
commit fdb879dcf2
304 changed files with 1314 additions and 364 deletions

7
14_cases/ci/README.md Normal file
View File

@@ -0,0 +1,7 @@
# CI/CD
**持续集成(Continuous integration)** 是一种软件开发实践每次集成都通过自动化的构建包括编译发布自动化测试来验证从而尽早地发现集成错误
**持续部署(continuous deployment)** 是通过自动化的构建测试和部署循环来快速交付高质量的产品
`Jenkins` 不同的是基于 Docker CI/CD 每一步都运行在 Docker 容器中所以理论上支持所有的编程语言

View File

@@ -0,0 +1,28 @@
# GitHub Actions
GitHub [Actions](https://github.com/features/actions) 是 GitHub 推出的一款 CI/CD 工具。
我们可以在每个 `job` `step` 中使用 Docker 执行构建步骤
```yaml
on: push
name: CI
jobs:
my-job:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
with:
fetch-depth: 2
- name: run docker container
uses: docker://golang:alpine
with:
args: go version
```
## 参考资料
* [Actions Docs](https://docs.github.com/en/actions)

View File

@@ -0,0 +1,74 @@
# 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 (多阶段构建)
```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.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 驱动的部署同步

View File

@@ -0,0 +1,6 @@
DRONE_SERVER_HOST=
DRONE_SERVER_PROTO=
DRONE_RPC_SECRET=
HOSTNAME=
DRONE_GITHUB_CLIENT_ID=
DRONE_GITHUB_CLIENT_SECRET=

2
14_cases/ci/drone/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
.env
ssl/*

View File

@@ -0,0 +1,99 @@
# Drone
基于 `Docker` `CI/CD` 工具 `Drone` 所有编译测试的流程都在 `Docker` 容器中进行
开发者只需在项目中包含 `.drone.yml` 文件将代码推送到 git 仓库`Drone` 就能够自动化的进行编译测试发布
本小节以 `GitHub` + `Drone` 来演示 `Drone` 的工作流程当然在实际开发过程中你的代码也许不在 GitHub 托管那么你可以尝试使用 `Gogs` + `Drone` 来进行 `CI/CD`
## Drone 关联项目
Github 新建一个名为 `drone-demo` 的仓库
打开我们已经 [部署好的 Drone 网站](install.md) 或者 [Drone Cloud](https://cloud.drone.io),使用 GitHub 账号登录,在界面中关联刚刚新建的 `drone-demo` 仓库。
## 编写项目源代码
初始化一个 git 仓库
```bash
$ mkdir drone-demo
$ cd drone-demo
$ git init
$ git remote add origin git@github.com:username/drone-demo.git
```
这里以一个简单的 `Go` 程序为例该程序输出 `Hello World!`
编写 `app.go` 文件
```go
package main
import "fmt"
func main(){
fmt.Printf("Hello World!\n");
}
```
编写 `.drone.yml` 文件
```yaml
kind: pipeline
type: docker
name: build
steps:
- name: build
image: golang:alpine
pull: if-not-exists # always never
environment:
KEY: VALUE
commands:
- echo $KEY
- pwd
- ls
- CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
- ./app
trigger:
branch:
- master
```
现在目录结构如下
```bash
.
├── .drone.yml
└── app.go
```
## 推送项目源代码到 GitHub
```bash
$ git add .
$ git commit -m "test drone ci"
$ git push origin master
```
## 查看项目构建过程及结果
打开我们部署好的 `Drone` 网站或者 Drone Cloud即可看到构建结果
![](../../../_images/drone-build.png)
当然我们也可以把构建结果上传到 GitHubDocker Registry云服务商提供的对象存储或者生产环境中
本书 GitBook 也使用 Drone 进行 CI/CD具体配置信息请查看本书根目录 [`.drone.yml`](../../../.drone.yml) 文件
## 参考链接
* [Drone Github](https://github.com/drone/drone)
* [Drone 文档](https://docs.drone.io/)
* [Drone 示例](https://github.com/docker-practice/drone-demo)

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

View File

@@ -0,0 +1,19 @@
kind: pipeline
type: docker
name: build
steps:
- name: build
image: golang:alpine
pull: if-not-exists # always never
environment:
KEY: VALUE
commands:
- echo $KEY
- pwd
- ls
- CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
- ./app
trigger:
branch:
- master

View File

@@ -0,0 +1,22 @@
# Drone CI Demo 项目
这是一个基于 Go 语言编写的简单 Web 应用示例用于演示 Drone CI 的持续集成流程
## 目录结构
* `app.go`: 简单的 Go Web 服务器代码
* `.drone.yml`: Drone CI 的配置文件定义了构建和测试流程
* `Dockerfile`: 定义了如何将该应用构建为 Docker 镜像
## 如何运行
1. 确保本地已安装 Docker 环境
2. 进入本目录构建镜像
```bash
docker build -t drone-demo-app .
```
3. 运行容器
```bash
docker run -p 8080:8080 drone-demo-app
```
4. 访问 `http://localhost:8080` 查看效果

View File

@@ -0,0 +1,7 @@
package main
import "fmt"
func main(){
fmt.Printf("Hello World!\n");
}

View File

@@ -0,0 +1,38 @@
services:
drone-server:
image: drone/drone:2.3.1
ports:
- 443:443
- 80:80
volumes:
- drone-data:/data:rw
- ./ssl:/etc/certs
restart: always
environment:
- DRONE_SERVER_HOST=${DRONE_SERVER_HOST:-drone.domain.com}
- DRONE_SERVER_PROTO=${DRONE_SERVER_PROTO:-https}
- DRONE_RPC_SECRET=${DRONE_RPC_SECRET:-secret}
- DRONE_GITHUB_SERVER=https://github.com
- DRONE_GITHUB_CLIENT_ID=${DRONE_GITHUB_CLIENT_ID}
- DRONE_GITHUB_CLIENT_SECRET=${DRONE_GITHUB_CLIENT_SECRET}
drone-agent:
image: drone/drone-runner-docker:1
restart: always
depends_on:
- drone-server
volumes:
- /var/run/docker.sock:/var/run/docker.sock:rw
environment:
- DRONE_RPC_PROTO=http
- DRONE_RPC_HOST=drone-server
- DRONE_RPC_SECRET=${DRONE_RPC_SECRET:-secret}
- DRONE_RUNNER_NAME=${HOSTNAME:-demo}
- DRONE_RUNNER_CAPACITY=2
dns: 114.114.114.114
volumes:
drone-data:

View File

@@ -0,0 +1,86 @@
# 部署 Drone
## 要求
* 拥有公网 IP域名 (如果你不满足要求可以尝试在本地使用 Gogs + Drone)
* 域名 SSL 证书 (目前国内有很多云服务商提供免费证书)
* 熟悉 `Docker` 以及 `Docker Compose`
* 熟悉 `Git` 基本命令
* `CI/CD` 有一定了解
## 新建 GitHub 应用
登录 GitHub https://github.com/settings/applications/new 新建一个应用。
![](https://docs.drone.io/screenshots/github_application_create.png)
接下来查看这个应用的详情记录 `Client ID` `Client Secret`之后配置 Drone 会用到
## 配置 Drone
我们通过使用 `Docker Compose` 来启动 `Drone`编写 `docker-compose.yml` 文件
```yaml
services:
drone-server:
image: drone/drone:2.3.1
ports:
- 443:443
- 80:80
volumes:
- drone-data:/data:rw
- ./ssl:/etc/certs
restart: always
environment:
- DRONE_SERVER_HOST=${DRONE_SERVER_HOST:-https://drone.yeasy.com}
- DRONE_SERVER_PROTO=${DRONE_SERVER_PROTO:-https}
- DRONE_RPC_SECRET=${DRONE_RPC_SECRET:-secret}
- DRONE_GITHUB_SERVER=https://github.com
- DRONE_GITHUB_CLIENT_ID=${DRONE_GITHUB_CLIENT_ID}
- DRONE_GITHUB_CLIENT_SECRET=${DRONE_GITHUB_CLIENT_SECRET}
drone-agent:
image: drone/drone-runner-docker:1
restart: always
depends_on:
- drone-server
volumes:
- /var/run/docker.sock:/var/run/docker.sock:rw
environment:
- DRONE_RPC_PROTO=http
- DRONE_RPC_HOST=drone-server
- DRONE_RPC_SECRET=${DRONE_RPC_SECRET:-secret}
- DRONE_RUNNER_NAME=${HOSTNAME:-demo}
- DRONE_RUNNER_CAPACITY=2
dns: 114.114.114.114
volumes:
drone-data:
```
新建 `.env` 文件输入变量及其值
```bash
# 必填 服务器地址,例如 drone.domain.com
DRONE_SERVER_HOST=
DRONE_SERVER_PROTO=https
DRONE_RPC_SECRET=secret
HOSTNAME=demo
# 必填 在 GitHub 应用页面查看
DRONE_GITHUB_CLIENT_ID=
# 必填 在 GitHub 应用页面查看
DRONE_GITHUB_CLIENT_SECRET=
```
### 启动 Drone
```bash
$ docker-compose up -d
```