Update CI

* Update drone to v1.x
* Add GitHub Actions
This commit is contained in:
khs1994 2019-08-31 18:36:00 +08:00
parent 8a3be4634e
commit 5a00a6b32f
20 changed files with 324 additions and 258 deletions

View File

@ -1,17 +1,15 @@
workspace:
base: /srv/gitbook-src
path: .
pipeline:
build:
kind: pipeline
type: docker
name: build
steps:
- name: build
image: yeasy/docker_practice:latest
# pull: true
pull: if-not-exists # always never
environment:
- TZ=Asia/Shanghai
secrets: [key1, key2]
TZ: Asia/Shanghai
commands:
# - echo $${key1}
# - echo $KEY2
- docker-entrypoint.sh build
when:
event: [push, pull_request, tag, deployment]
branch: master
trigger:
branch:
- master

18
.github/workflows/ci.yaml vendored Normal file
View 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

View File

@ -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
}

View File

@ -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

View 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)

View File

@ -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

View File

@ -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:

View File

@ -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 新建一个应用。
![](../.image/drone-github.png)
接下来查看这个应用的详情记录 `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` 网站即可看到构建结果
![](../.image/drone-build.png)
当然我们也可以把构建结果上传到 GitHubDocker 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/)

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
cases/ci/drone/.gitignore vendored Normal file
View File

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

99
cases/ci/drone/README.md Normal file
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即可看到构建结果
![](../.image/drone-build.png)
当然我们也可以把构建结果上传到 GitHubDocker 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)

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 @@
# Drone Demo

View File

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

View 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
View File

@ -0,0 +1,87 @@
# 部署 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
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
```