輕量級CI/CD工具Drone


Drone

基於 DockerCI/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 IDClient Secret,之后配置 Drone 會用到。

配置 Drone

我們通過使用 Docker Compose 來啟動 Drone,編寫 docker-compose.yml 文件。

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

$ docker-compose up -d

Drone 關聯項目

在 Github 新建一個名為 drone-demo 的倉庫。

打開我們已經部署好的 Drone 網站,使用 GitHub 賬號登錄,在界面中關聯剛剛新建的 drone-demo 倉庫。

編寫項目源代碼

在本機初始化一個 git 倉庫

$ mkdir drone-demo

$ cd drone-demo $ git init $ git remote add origin git@github.com:username/drone-demo.git 

這里以一個簡單的 Go 程序為例,該程序輸出 Hello World!

編寫 app.go 文件

package main

import "fmt"

func main(){  
    fmt.Printf("Hello World!");
}

編寫 .drone.yml 文件

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 鏡像,環境變量,編譯指令等。

現在目錄結構如下

.
├── .drone.yml
└── app.go

推送項目源代碼到 GitHub

$ git add .

$ git commit -m "test drone ci" $ git push origin master 

查看項目構建過程及結果

打開我們部署好的 Drone 網站,即可看到構建結果。

當然我們也可以把構建結果上傳到 GitHub,Docker Registry,雲服務商提供的對象存儲,或者生產環境中。

本書 GitBook 也使用 Drone 進行 CI/CD,具體配置信息請查看本書根目錄 .drone.yml 文件。

參考鏈接

  • Drone Github

  • Drone 文檔

  • 轉載鏈接:https://www.cntofu.com/book/139/cases/ci/drone.md
  • 更多參考:http://www.found5.com/view/288.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM