mirror of
				https://github.com/yeasy/docker_practice.git
				synced 2025-11-04 03:41:34 +00:00 
			
		
		
		
	Update CI
* Update drone to v1.x * Add GitHub Actions
This commit is contained in:
		
							
								
								
									
										32
									
								
								.drone.yml
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								.drone.yml
									
									
									
									
									
								
							@@ -1,17 +1,15 @@
 | 
			
		||||
workspace:
 | 
			
		||||
  base: /srv/gitbook-src
 | 
			
		||||
  path: .
 | 
			
		||||
pipeline:
 | 
			
		||||
  build:
 | 
			
		||||
     image: yeasy/docker_practice:latest
 | 
			
		||||
     # pull: true
 | 
			
		||||
     environment:
 | 
			
		||||
       - TZ=Asia/Shanghai
 | 
			
		||||
     secrets: [key1, key2]
 | 
			
		||||
     commands:
 | 
			
		||||
       # - echo $${key1}
 | 
			
		||||
       # - echo $KEY2
 | 
			
		||||
       - docker-entrypoint.sh build
 | 
			
		||||
     when:
 | 
			
		||||
       event: [push, pull_request, tag, deployment]
 | 
			
		||||
       branch: master
 | 
			
		||||
kind: pipeline
 | 
			
		||||
type: docker
 | 
			
		||||
name: build
 | 
			
		||||
steps:
 | 
			
		||||
  - name: build
 | 
			
		||||
    image: yeasy/docker_practice:latest
 | 
			
		||||
    pull: if-not-exists # always never
 | 
			
		||||
    environment:
 | 
			
		||||
      TZ: Asia/Shanghai
 | 
			
		||||
    commands:
 | 
			
		||||
      - docker-entrypoint.sh build
 | 
			
		||||
 | 
			
		||||
trigger:
 | 
			
		||||
  branch:
 | 
			
		||||
    - master
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										18
									
								
								.github/workflows/ci.yaml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								.github/workflows/ci.yaml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
on:
 | 
			
		||||
  push:
 | 
			
		||||
  pull_request:
 | 
			
		||||
 | 
			
		||||
name: CI
 | 
			
		||||
 | 
			
		||||
jobs:
 | 
			
		||||
  build:
 | 
			
		||||
    name: Build GitBook
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@master
 | 
			
		||||
        with:
 | 
			
		||||
          fetch-depth: 2
 | 
			
		||||
      - name: Build
 | 
			
		||||
        uses: docker://yeasy/docker_practice
 | 
			
		||||
        with:
 | 
			
		||||
          args: build
 | 
			
		||||
@@ -6,12 +6,14 @@ if [ $1 = "sh" ];then sh ; exit 0; fi
 | 
			
		||||
 | 
			
		||||
rm -rf node_modules _book
 | 
			
		||||
 | 
			
		||||
cp -a . ../gitbook
 | 
			
		||||
srcDir=$PWD
 | 
			
		||||
 | 
			
		||||
cd ../gitbook
 | 
			
		||||
cp -a . /srv/gitbook
 | 
			
		||||
 | 
			
		||||
cd /srv/gitbook
 | 
			
		||||
 | 
			
		||||
main(){
 | 
			
		||||
  if [ "$1" = build ];then gitbook build; cp -a _book ../gitbook-src; echo $START; date "+%F %T"; exit 0; fi
 | 
			
		||||
  if [ "$1" = build ];then gitbook build; cp -a _book $srcDir; echo $START; date "+%F %T"; exit 0; fi
 | 
			
		||||
  exec gitbook serve
 | 
			
		||||
  exit 0
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -144,8 +144,10 @@
 | 
			
		||||
    * [CentOS Fedora](cases/os/centos.md)
 | 
			
		||||
    * [本章小结](cases/os/summary.md)
 | 
			
		||||
* [实战案例-CI/CD](cases/ci/README.md)
 | 
			
		||||
    * [Drone](cases/ci/drone.md)
 | 
			
		||||
    * [Travis CI](cases/ci/travis.md)
 | 
			
		||||
    * [GitHub Actions](cases/ci/actions/README.md)
 | 
			
		||||
    * [Drone](cases/ci/drone/README.md)
 | 
			
		||||
        * [部署 Drone](cases/ci/drone/install.md)
 | 
			
		||||
    * [Travis CI](cases/ci/travis/README.md)
 | 
			
		||||
* [Docker 开源项目](opensource/README.md)
 | 
			
		||||
    * [LinuxKit](opensource/linuxkit.md)
 | 
			
		||||
* [附录](appendix/README.md)
 | 
			
		||||
 
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							| 
		 Before Width: | Height: | Size: 86 KiB  | 
							
								
								
									
										28
									
								
								cases/ci/actions/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								cases/ci/actions/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
# GitHub Actions
 | 
			
		||||
 | 
			
		||||
GitGub 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://help.github.com/en/categories/automating-your-workflow-with-github-actions)
 | 
			
		||||
@@ -1,17 +0,0 @@
 | 
			
		||||
workspace:
 | 
			
		||||
  base: /srv/drone-demo
 | 
			
		||||
  path: .
 | 
			
		||||
 | 
			
		||||
pipeline:
 | 
			
		||||
  build:
 | 
			
		||||
     image: golang:alpine
 | 
			
		||||
     # pull: true
 | 
			
		||||
     environment:
 | 
			
		||||
       - KEY=VALUE
 | 
			
		||||
     secrets: [key1, key2]
 | 
			
		||||
     commands:
 | 
			
		||||
       # - echo $$KEY
 | 
			
		||||
       - pwd
 | 
			
		||||
       - ls
 | 
			
		||||
       - CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
 | 
			
		||||
       - ./app
 | 
			
		||||
@@ -1,35 +0,0 @@
 | 
			
		||||
version: '3'
 | 
			
		||||
services:
 | 
			
		||||
 | 
			
		||||
  drone-server:
 | 
			
		||||
    image: drone/drone:0.8-alpine
 | 
			
		||||
    ports:
 | 
			
		||||
      - 443:443
 | 
			
		||||
      # - "${PRO_PUBLIC_IP}:8000:8000"
 | 
			
		||||
    volumes:
 | 
			
		||||
      - drone-data:/var/lib/drone/:rw
 | 
			
		||||
      # - ${SSL_PATH}:/etc/certs
 | 
			
		||||
    restart: always
 | 
			
		||||
    environment:
 | 
			
		||||
      - DRONE_SECRET=drone
 | 
			
		||||
      - DRONE_OPEN=false
 | 
			
		||||
      - DRONE_ADMIN=GITHUB_SERNAME
 | 
			
		||||
      - DRONE_HOST=https://drone.yeasy.com
 | 
			
		||||
      - DRONE_GITHUB=true
 | 
			
		||||
      - DRONE_GITHUB_CLIENT=${DRONE_GITHUB_CLIENT_PRO}
 | 
			
		||||
      - DRONE_GITHUB_SECRET=${DRONE_GITHUB_SECRET_PRO}
 | 
			
		||||
 | 
			
		||||
  drone-agent:
 | 
			
		||||
    image: drone/agent:0.8-alpine
 | 
			
		||||
    restart: always
 | 
			
		||||
    depends_on:
 | 
			
		||||
      - drone-server
 | 
			
		||||
    volumes:
 | 
			
		||||
      - /var/run/docker.sock:/var/run/docker.sock:rw
 | 
			
		||||
    environment:
 | 
			
		||||
      - DRONE_SECRET=drone
 | 
			
		||||
      - DRONE_SERVER=drone-server:9000
 | 
			
		||||
    dns: 114.114.114.114
 | 
			
		||||
 | 
			
		||||
volumes:
 | 
			
		||||
  drone-data:
 | 
			
		||||
@@ -1,183 +0,0 @@
 | 
			
		||||
## Drone
 | 
			
		||||
 | 
			
		||||
基于 `Docker` 的 `CI/CD` 工具 `Drone` 所有编译、测试的流程都在 `Docker` 容器中进行。
 | 
			
		||||
 | 
			
		||||
开发者只需在项目中包含 `.drone.yml` 文件,将代码推送到 git 仓库,`Drone` 就能够自动化的进行编译、测试、发布。
 | 
			
		||||
 | 
			
		||||
本小节以 `GitHub` + `Drone` 来演示 `Drone` 的工作流程。当然在实际开发过程中,你的代码也许不在 GitHub 托管,那么你可以尝试使用 `Gogs` + `Drone` 来进行 `CI/CD`。
 | 
			
		||||
 | 
			
		||||
### 要求
 | 
			
		||||
 | 
			
		||||
* 拥有公网 IP、域名 (如果你不满足要求,可以尝试在本地使用 Gogs + Drone)
 | 
			
		||||
 | 
			
		||||
* 域名 SSL 证书 (目前国内有很多云服务商提供免费证书)
 | 
			
		||||
 | 
			
		||||
* 熟悉 `Docker` 以及 `Docker Compose`
 | 
			
		||||
 | 
			
		||||
* 熟悉 `Git` 基本命令
 | 
			
		||||
 | 
			
		||||
* 对 `CI/CD` 有一定了解
 | 
			
		||||
 | 
			
		||||
### 新建 GitHub 应用
 | 
			
		||||
 | 
			
		||||
登录 GitHub,在 https://github.com/settings/applications/new 新建一个应用。
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
接下来查看这个应用的详情,记录 `Client ID` 和 `Client Secret`,之后配置 Drone 会用到。
 | 
			
		||||
 | 
			
		||||
### 配置 Drone
 | 
			
		||||
 | 
			
		||||
我们通过使用 `Docker Compose` 来启动 `Drone`,编写 `docker-compose.yml` 文件。
 | 
			
		||||
 | 
			
		||||
```yaml
 | 
			
		||||
version: '3'
 | 
			
		||||
services:
 | 
			
		||||
 | 
			
		||||
  drone-server:
 | 
			
		||||
    image: drone/drone:0.8-alpine
 | 
			
		||||
    ports:
 | 
			
		||||
      - 443:443
 | 
			
		||||
      # - "${PRO_PUBLIC_IP}:8000:8000"
 | 
			
		||||
    volumes:
 | 
			
		||||
      - drone-data:/var/lib/drone/:rw
 | 
			
		||||
      - ${SSL_PATH}:/etc/certs:rw
 | 
			
		||||
    restart: always
 | 
			
		||||
    environment:
 | 
			
		||||
      - DRONE_SECRET=drone
 | 
			
		||||
      - DRONE_OPEN=false
 | 
			
		||||
      - DRONE_ADMIN=${GITHUB_SERNAME}
 | 
			
		||||
      - DRONE_HOST=${DRONE_HOST}
 | 
			
		||||
      - DRONE_GITHUB=true
 | 
			
		||||
      - DRONE_GITHUB_CLIENT=${DRONE_GITHUB_CLIENT}
 | 
			
		||||
      - DRONE_GITHUB_SECRET=${DRONE_GITHUB_SECRET}
 | 
			
		||||
      - DRONE_SERVER_CERT=/etc/certs/drone.domain.com.crt
 | 
			
		||||
      - DRONE_SERVER_KEY=/etc/certs/drone.domain.com.key
 | 
			
		||||
 | 
			
		||||
  drone-agent:
 | 
			
		||||
    image: drone/agent:0.8-alpine
 | 
			
		||||
    restart: always
 | 
			
		||||
    depends_on:
 | 
			
		||||
      - drone-server
 | 
			
		||||
    volumes:
 | 
			
		||||
      - /var/run/docker.sock:/var/run/docker.sock:rw
 | 
			
		||||
    environment:
 | 
			
		||||
      - DRONE_SECRET=drone
 | 
			
		||||
      - DRONE_SERVER=drone-server:9000
 | 
			
		||||
    dns: 114.114.114.114
 | 
			
		||||
 | 
			
		||||
volumes:
 | 
			
		||||
  drone-data:
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
替换 `${SSL_PATH}` 为你网站的 SSL 证书路径。
 | 
			
		||||
 | 
			
		||||
替换 `${GITHUB_SERNAME}` 为你 GitHub 的用户名,该用户将成为 Drone 的管理员。
 | 
			
		||||
 | 
			
		||||
替换 `${DRONE_HOST}` 为你部署 Drone 的域名。
 | 
			
		||||
 | 
			
		||||
替换 `${DRONE_GITHUB_CLIENT}` 为你 GitHub 应用的 `Client ID`
 | 
			
		||||
 | 
			
		||||
替换 `${DRONE_GITHUB_SECRET}` 为你 GitHub 应用的 `Client Secret`
 | 
			
		||||
 | 
			
		||||
*注意:* 如果你的服务器占用了 `443` 端口,请配置 Nginx 代理,这里不再赘述。
 | 
			
		||||
 | 
			
		||||
#### 启动 Drone
 | 
			
		||||
 | 
			
		||||
```bash
 | 
			
		||||
$ docker-compose up -d
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### Drone 关联项目
 | 
			
		||||
 | 
			
		||||
在 Github 新建一个名为 `drone-demo` 的仓库。
 | 
			
		||||
 | 
			
		||||
打开我们已经部署好的 Drone 网站,使用 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!");
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
编写 `.drone.yml` 文件
 | 
			
		||||
 | 
			
		||||
```yaml
 | 
			
		||||
workspace:
 | 
			
		||||
  base: /srv/drone-demo
 | 
			
		||||
  path: .
 | 
			
		||||
 | 
			
		||||
pipeline:
 | 
			
		||||
  build:
 | 
			
		||||
     image: golang:alpine
 | 
			
		||||
     # pull: true
 | 
			
		||||
     environment:
 | 
			
		||||
       - KEY=VALUE
 | 
			
		||||
     secrets: [key1, key2]
 | 
			
		||||
     commands:
 | 
			
		||||
       - echo $$KEY
 | 
			
		||||
       - pwd
 | 
			
		||||
       - ls
 | 
			
		||||
       - CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
 | 
			
		||||
       - ./app
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
`workspace` 指明 git 源代码克隆的目标路径,本例中 git 源代码将被克隆到 golang 容器中的 `/srv/drone-demo` 目录中。
 | 
			
		||||
 | 
			
		||||
`pipeline` 指明构建所需的 Docker 镜像,环境变量,编译指令等。
 | 
			
		||||
 | 
			
		||||
现在目录结构如下
 | 
			
		||||
 | 
			
		||||
```bash
 | 
			
		||||
.
 | 
			
		||||
├── .drone.yml
 | 
			
		||||
└── app.go
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### 推送项目源代码到 GitHub
 | 
			
		||||
 | 
			
		||||
```bash
 | 
			
		||||
$ git add .
 | 
			
		||||
 | 
			
		||||
$ git commit -m "test drone ci"
 | 
			
		||||
 | 
			
		||||
$ git push origin master
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### 查看项目构建过程及结果
 | 
			
		||||
 | 
			
		||||
打开我们部署好的 `Drone` 网站,即可看到构建结果。
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
当然我们也可以把构建结果上传到 GitHub,Docker Registry,云服务商提供的对象存储,或者生产环境中。
 | 
			
		||||
 | 
			
		||||
本书 GitBook 也使用 Drone 进行 CI/CD,具体配置信息请查看本书根目录 [`.drone.yml`](https://github.com/yeasy/docker_practice/blob/master/.drone.yml) 文件。
 | 
			
		||||
 | 
			
		||||
## 参考链接
 | 
			
		||||
 | 
			
		||||
* [Drone Github](https://github.com/drone/drone)
 | 
			
		||||
 | 
			
		||||
* [Drone 文档](http://docs.drone.io/)
 | 
			
		||||
							
								
								
									
										6
									
								
								cases/ci/drone/.env.example
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								cases/ci/drone/.env.example
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
DRONE_SERVER_HOST=
 | 
			
		||||
DRONE_SERVER_PROTO=
 | 
			
		||||
DRONE_RPC_SECRET=
 | 
			
		||||
HOSTNAME=
 | 
			
		||||
DRONE_GITHUB_CLIENT_ID=
 | 
			
		||||
DRONE_GITHUB_CLIENT_SECRET=
 | 
			
		||||
							
								
								
									
										2
									
								
								cases/ci/drone/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								cases/ci/drone/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
.env
 | 
			
		||||
ssl/*
 | 
			
		||||
							
								
								
									
										99
									
								
								cases/ci/drone/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										99
									
								
								cases/ci/drone/README.md
									
									
									
									
									
										Normal 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,即可看到构建结果。
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
当然我们也可以把构建结果上传到 GitHub,Docker Registry,云服务商提供的对象存储,或者生产环境中。
 | 
			
		||||
 | 
			
		||||
本书 GitBook 也使用 Drone 进行 CI/CD,具体配置信息请查看本书根目录 [`.drone.yml`](https://github.com/yeasy/docker_practice/blob/master/.drone.yml) 文件。
 | 
			
		||||
 | 
			
		||||
## 参考链接
 | 
			
		||||
 | 
			
		||||
* [Drone Github](https://github.com/drone/drone)
 | 
			
		||||
* [Drone 文档](http://docs.drone.io/)
 | 
			
		||||
* [Drone 示例](https://github.com/docker-practice/drone-demo)
 | 
			
		||||
							
								
								
									
										19
									
								
								cases/ci/drone/demo/.drone.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								cases/ci/drone/demo/.drone.yml
									
									
									
									
									
										Normal 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
 | 
			
		||||
							
								
								
									
										1
									
								
								cases/ci/drone/demo/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								cases/ci/drone/demo/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
# Drone Demo
 | 
			
		||||
@@ -3,5 +3,5 @@ package main
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func main(){
 | 
			
		||||
    fmt.Printf("Hello World!");
 | 
			
		||||
    fmt.Printf("Hello World!\n");
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										39
									
								
								cases/ci/drone/docker-compose.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								cases/ci/drone/docker-compose.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
version: '3'
 | 
			
		||||
 | 
			
		||||
services:
 | 
			
		||||
 | 
			
		||||
  drone-server:
 | 
			
		||||
    image: drone/drone:1
 | 
			
		||||
    ports:
 | 
			
		||||
      - 443:443
 | 
			
		||||
      - 80:80
 | 
			
		||||
    volumes:
 | 
			
		||||
      - drone-data:/data:rw
 | 
			
		||||
      - ./ssl:/etc/certs
 | 
			
		||||
    restart: always
 | 
			
		||||
    environment:
 | 
			
		||||
      - DRONE_AGENTS_ENABLED=true
 | 
			
		||||
      - 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/agent: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:
 | 
			
		||||
							
								
								
									
										87
									
								
								cases/ci/drone/install.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								cases/ci/drone/install.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,87 @@
 | 
			
		||||
# 部署 Drone
 | 
			
		||||
 | 
			
		||||
## 要求
 | 
			
		||||
 | 
			
		||||
* 拥有公网 IP、域名 (如果你不满足要求,可以尝试在本地使用 Gogs + Drone)
 | 
			
		||||
 | 
			
		||||
* 域名 SSL 证书 (目前国内有很多云服务商提供免费证书)
 | 
			
		||||
 | 
			
		||||
* 熟悉 `Docker` 以及 `Docker Compose`
 | 
			
		||||
 | 
			
		||||
* 熟悉 `Git` 基本命令
 | 
			
		||||
 | 
			
		||||
* 对 `CI/CD` 有一定了解
 | 
			
		||||
 | 
			
		||||
## 新建 GitHub 应用
 | 
			
		||||
 | 
			
		||||
登录 GitHub,在 https://github.com/settings/applications/new 新建一个应用。
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
接下来查看这个应用的详情,记录 `Client ID` 和 `Client Secret`,之后配置 Drone 会用到。
 | 
			
		||||
 | 
			
		||||
## 配置 Drone
 | 
			
		||||
 | 
			
		||||
我们通过使用 `Docker Compose` 来启动 `Drone`,编写 `docker-compose.yml` 文件。
 | 
			
		||||
 | 
			
		||||
```yaml
 | 
			
		||||
version: '3'
 | 
			
		||||
 | 
			
		||||
services:
 | 
			
		||||
 | 
			
		||||
  drone-server:
 | 
			
		||||
    image: drone/drone:1
 | 
			
		||||
    ports:
 | 
			
		||||
      - 443:443
 | 
			
		||||
      - 80:80
 | 
			
		||||
    volumes:
 | 
			
		||||
      - drone-data:/data:rw
 | 
			
		||||
      - ./ssl:/etc/certs
 | 
			
		||||
    restart: always
 | 
			
		||||
    environment:
 | 
			
		||||
      - DRONE_AGENTS_ENABLED=true
 | 
			
		||||
      - 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/agent: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
 | 
			
		||||
```
 | 
			
		||||
		Reference in New Issue
	
	Block a user