.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 |
|
01
02
|
curl -L https://github.com/drone/drone-cli/releases/download/v0.8.0/drone_linux_amd64.tar.gz | tar zxsudo 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 |
使用 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 |
忽略特定步驟
已經導入 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 方式。