在CI/CD的流程中跑testing是很重要的,但有些test case需要使用DB或其他服務時就可以用Drone Pipelines Service設定把你需要的服務掛進來,例如:redis, mysql, nsq, cassandra or scylla ...。 只要相關服務支援使用image讓docker啟動,就能在Pipelines單獨起服務來使用唷!是不是非常非常方便~
以下是範例:
kind: pipeline
type: docker
name: default
steps:
- name: ping
image: redis
commands:
- sleep 5
- redis-cli -h cachehost ping
services:
- name: cachehost
image: redis
kind: pipeline
type: docker
name: default
steps:
- name: ping mysql
image: mysql:5.5
commands:
- sleep 10
- mysql -hmysqlhost -uroot -proot
services:
- name: mysqlhost
image: mysql:5.5
environment:
MYSQL_DATABASE: "test"
MYSQL_ROOT_PASSWORD: "root"
kind: pipeline
type: docker
name: default
steps:
- name: ping scylla
image: scylladb/scylla:3.3.4
commands:
- sleep 50
- cqlsh -e "SHOW VERSION" scylladb
services:
- name: scylladb
image: scylladb/scylla:3.3.4
- name: scylladb2
image: scylladb/scylla:3.3.4
command:
- --seeds=scylladb
- name: scylladb3
image: scylladb/scylla:3.3.4
command:
- --seeds=scylladb
kind: pipeline
type: docker
name: default
steps:
- name: ping nsq
image: curlimages/curl:7.71.1
commands:
- sleep 5
- curl nsqd:4151/ping
- curl nsqlookupd:4161/ping
- curl nsqadmin:4171/ping
services:
- name: nsqlookupd
image: nsqio/nsq
command: ["/nsqlookupd"]
ports: [4160,4161]
- name: nsqd
image: nsqio/nsq
command: ["/nsqd", "--lookupd-tcp-address=nsqlookupd:4160"]
depends_on:
- nsqlookupd
ports: [4150,4151]
- name: nsqadmin
image: nsqio/nsq
command: ["/nsqadmin", "--lookupd-http-address=nsqlookupd:4161"]
depends_on:
- nsqlookupd
- nsqd
ports: [4171]
別著急!啟動容器後,在容器內運行的程式需要花一些時間來初始化並啟動服務。
在連接Services之前,請確認服務有足夠的時間進行初始化的動作。
而其中一個最快的解決方案是使用sleep命令,讓我們的Pipeline多等Services一會兒。
Service的Hostname就是你在service下一層定義的name,以上面我寫的redis範例就是cachehost
來源:
官網說明文件
以及感謝weiweiwesley 提供NSQ的Drone Services 設定