安裝 drone CLI和使用drone

 Drone 是一套用 Go 語言所撰寫的 CI/CD 開源專案,透過 .drone.yml 檔案方式讓開發者可以自行撰寫測試及部署流程。大家一定會認為要先架設好 Drone 伺服器,才能透過 Git Push 方式來達到自動化測試及部署專案。現在跟大家介紹,如果你的團隊尚未架設 Drone 服務,但是又想要使用 Drone 透過 Yaml 方式所帶來的好處,很簡單,你只需要透過 Drone CLI 工具就可以完成,不需要架設任何一台 Drone 服務,只要學會 Yaml 方式如何撰寫,就可以透過 drone exec 指令來完成。好處是寫完 .drone.yml 檔案,未來圖隊如果正式架設了 Drone 服務,就可以無痛升級,沒有的話,也可以透過 CLI 工具在公司專案內單獨使用,這比寫 docker-compose.yml 方式還要快很多。本篇會介紹使用 drone exec 的小技巧。

安裝 drone cli

請直到官方下載頁面下載相對應檔案,完成後請放到 /usr/local/bin 底下,目前支援 Windows, Linnx 及 MacOS。如果開發環境有 Go 語言,可以直接透過底下指令安裝
01
$ go get -u github.com/drone/drone-cli/drone
或是透過 tarbal 方式安裝
01
02
curl -L https://github.com/drone/drone-cli/releases/download/v0.8.0/drone_linux_amd64.tar.gz | tar zx
sudo install -t /usr/local/bin drone

撰寫 Yaml 檔案

用編輯器打開專案,並且初始化 .drone.yml 檔案
01
02
03
04
05
06
07
08
09
10
pipeline:
  backend:
    image: golang
    commands:
      - echo "backend testing"
 
  frontend:
    image: golang
    commands:
      - echo "frontend testing"
在命令列直接下 drone exec 畫面如下
01
02
03
04
[backend:L0:0s] + echo "backend testing"
[backend:L1:0s] backend testing
[frontend:L0:0s] + echo "frontend testing"
[frontend:L1:0s] frontend testing
可以發現今天就算沒有 drone server 團隊依然可以透過 drone exec 來完成測試。

使用 secret

在 drone 測試會需要使用 secret 來保存類似像 AWS API Key 隱秘資訊,但是這只能在 Drone server 上面跑才會自動帶入 secret。
01
02
03
04
05
06
07
08
09
10
11
12
pipeline:
  backend:
    image: golang
    secrets: [ test ]
    commands:
      - echo "backend testing"
      - echo $TEST
 
  frontend:
    image: golang
    commands:
      - echo "frontend testing"
執行 drone exec 後會發現結果如下
01
02
03
04
05
06
07
$ drone exec
[backend:L0:0s] + echo "backend testing"
[backend:L1:0s] backend testing
[backend:L2:0s] + echo $TEST
[backend:L3:0s]
[frontend:L0:0s] + echo "frontend testing"
[frontend:L1:0s] frontend testing
可以得知 $TEST 輸出是沒有任何資料,但是如果在 Drone server 上面跑是有資料的。那該如何在個人電腦也拿到此資料呢?其實很簡單,透過環境變數即可
01
02
03
04
05
06
07
$ TEST=appleboy drone exec
[backend:L0:0s] + echo "backend testing"
[backend:L1:0s] backend testing
[backend:L2:0s] + echo $TEST
[backend:L3:0s] appleboy
[frontend:L0:0s] + echo "frontend testing"
[frontend:L1:0s] frontend testing
這樣我們就可以正確拿到 secret 資料了。

忽略特定步驟

已經導入 drone 的團隊,一定會把很多部署的步驟都放在 .drone.yml 檔案內,但是在本機端只想跑前後端測試,後面的像是 Notification,或者是 SCP 及 SSH 步驟都需要忽略,這樣可以單純只跑測試,這時候該透過什麼方式才可以避免呢?很簡單只要在 when 條件子句加上 local: false 即可。假設原本 Yaml 寫法如下:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
pipeline:
  backend:
    image: golang
    commands:
      - echo "backend testing"
 
  frontend:
    image: golang
    commands:
      - echo "frontend testing"
 
  deploy:
    image: golang
    commands:
      - echo "deploy"
這次我們想忽略掉 deploy 步驟,請改寫如下
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
pipeline:
  backend:
    image: golang
    commands:
      - echo "backend testing"
 
  frontend:
    image: golang
    commands:
      - echo "frontend testing"
 
  deploy:
    image: golang
    commands:
      - echo "deploy"
    when:
      local: false
再執行 drone exec,大家可以發現,最後一個步驟 deploy 就被忽略不執行了,這在本機端測試非常有用,也不會影響到 drone server 上的執行。大家可以參考此 Yaml 檔案範例,大量使用了 local: false 方式。
 

撰寫 .drone.jsonnet 檔案

在專案目錄內放置 .drone.jsonnet 檔案,拿 Go 專案當範例:

  1. 驗證程式碼品質
  2. 編譯執行檔
  3. 包成 Docker 容器
  4. 上傳到 Docker Hub
  5. 消息通知
local PipelineTesting = {
  kind: "pipeline",
  name: "testing",
  platform: {
    os: "linux",
    arch: "amd64",
  },
  steps: [
    {
      name: "vet",
      image: "golang:1.11",
      pull: "always",
      environment: {
        GO111MODULE: "on",
      },
      commands: [
        "make vet",
      ],
    },
    {
      name: "lint",
      image: "golang:1.11",
      pull: "always",
      environment: {
        GO111MODULE: "on",
      },
      commands: [
        "make lint",
      ],
    },
    {
      name: "misspell",
      image: "golang:1.11",
      pull: "always",
      environment: {
        GO111MODULE: "on",
      },
      commands: [
        "make misspell-check",
      ],
    },
    {
      name: "test",
      image: "golang:1.11",
      pull: "always",
      environment: {
        GO111MODULE: "on",
        WEBHOOK_ID: { "from_secret": "webhook_id" },
        WEBHOOK_TOKEN: { "from_secret": "webhook_token" },
      },
      commands: [
        "make test",
        "make coverage",
      ],
    },
    {
      name: "codecov",
      image: "robertstettner/drone-codecov",
      pull: "always",
      settings: {
        token: { "from_secret": "codecov_token" },
      },
    },
  ],
  trigger: {
    branch: [ "master" ],
  },
};

local PipelineBuild(os="linux", arch="amd64") = {
  kind: "pipeline",
  name: os + "-" + arch,
  platform: {
    os: os,
    arch: arch,
  },
  steps: [
    {
      name: "build-push",
      image: "golang:1.11",
      pull: "always",
      environment: {
        CGO_ENABLED: "0",
        GO111MODULE: "on",
      },
      commands: [
        "go build -v -ldflags \"-X main.build=${DRONE_BUILD_NUMBER}\" -a -o release/" + os + "/" + arch + "/drone-discord",
      ],
      when: {
        event: [ "push", "pull_request" ],
      },
    },
    {
      name: "build-tag",
      image: "golang:1.11",
      pull: "always",
      environment: {
        CGO_ENABLED: "0",
        GO111MODULE: "on",
      },
      commands: [
        "go build -v -ldflags \"-X main.version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}\" -a -o release/" + os + "/" + arch + "/drone-discord",
      ],
      when: {
        event: [ "tag" ],
      },
    },
    {
      name: "executable",
      image: "golang:1.11",
      pull: "always",
      commands: [
        "./release/" + os + "/" + arch + "/drone-discord --help",
      ],
    },{
      name:"dryrun",
      image:"plugins/docker:"+ os +"-"+ arch,
      pull:"always",
      settings:{
        dry_run:true,
        tags: os +"-"+ arch,
        dockerfile:"docker/Dockerfile."+ os +"."+ arch,
        repo:"appleboy/drone-discord",
        username:{"from_secret":"docker_username"},
        password:{"from_secret":"docker_password"},},when:{event:["pull_request"],},},{
      name:"publish",
      image:"plugins/docker:"+ os +"-"+ arch,
      pull:"always",
      settings:{
        auto_tag:true,
        auto_tag_suffix: os +"-"+ arch,
        dockerfile:"docker/Dockerfile."+ os +"."+ arch,
        repo:"appleboy/drone-discord",
        username:{"from_secret":"docker_username"},
        password:{"from_secret":"docker_password"},},when:{event:["push","tag"],},},],
  depends_on:["testing",],
  trigger:{
    branch:["master"],},};localPipelineNotifications={
  kind:"pipeline",
  name:"notifications",
  platform:{
    os:"linux",
    arch:"amd64",},
  clone:{
    disable:true,},
  steps:[{
      name:"microbadger",
      image:"plugins/webhook:1",
      pull:"always",
      settings:{
        url:{"from_secret":"microbadger_url"},},},],
  depends_on:["linux-amd64","linux-arm64","linux-arm",],
  trigger:{
    branch:["master"],event:["push","tag"],},};[PipelineTesting,PipelineBuild("linux","amd64"),PipelineBuild("linux","arm64"),PipelineBuild("linux","arm"),PipelineNotifications,]

大家可以看到 local PipelineBuild 就是一個 func 函數,可以用來產生不同的環境代碼

PipelineBuild("linux", "amd64"),
  PipelineBuild("linux", "arm64"),
  PipelineBuild("linux", "arm"),

完成後,直接在專案目錄下執行

$ drone jsonnet --stream

您會發現專案下的 .drone.yml 已經成功修正,未來只要將變動部分抽成變數,就可以產生不同專案的環境,開發者就不需要每次手動修改很多變動的地方。至於要不要把 .drone.jsonnet 放入專案內進行版本控制就看情境了。其實可以另外開一個新的 Repo 放置 .drone.jsonnet ,未來新專案開案,就可以快速 clone 下來,並且產生新專案的 .drone.yml 設定檔。底下是 Drone 執行結果:

有效率的用 jsonnet 撰寫 Drone CI/CD 設定檔

 

https://www.codercto.com/a/55576.html

https://blog.wu-boy.com/2017/12/drone-cli-local-testing/

posted @ 2020-04-29 10:54  学习者_3988  阅读(1235)  评论(0编辑  收藏  举报