[Day 11] Oops!Drone - 快速上手

想要讓Drone設定的好,就要把基礎先打好!

認識設定值

看完上述描述可以看下方的 exmaple yaml檔來比對

kind: pipeline
type: docker
name: backend

steps:
- name: golang test
  image: golang:1.14
  commands:
    - go test

基礎練習

看完上述的簡介,是不是對 Drone還有些陌生呢?

事不宜遲,今天要來讓大家練習寫 Drone設定yaml!加深印象!

以下是我列出的練習題目及示範給各位

1. echo hello drone

kind: pipeline
type: docker
name: backend

steps:
- name: practice
  image: golang
  commands:
    - echo hello drone

2. 只讓test分支,跑golang test

kind: pipeline
type: docker
name: backend

steps:
- name: practice
  image: golang
  when:
    branch: 
    - test
  commands:
    - go test

3. 只在 maser,test分支,跑 golang test

kind: pipeline
type: docker
name: backend

steps:
- name: practice
  image: golang
  when:
    branch: 
    - test
    - master
  commands:
    - go test

4. golang repo - docker build image

先建立一個 go.dockerfile 檔案

FROM golang

WORKDIR /ithome
ADD . /ithome
RUN cd /ithome && go build

CMD ["./ithome"]

.drone.yml

kind: pipeline
type: docker
name: backend

steps:
- name: test
  image: docker:dind
  volumes:
  - name: dockersock
    path: /var/run/docker.sock
  commands:
  - docker build --no-cache --pull --force-rm -t rain/ithome:latest -f go.dockerfile .

volumes:
- name: dockersock
  host:
    path: /var/run/docker.sock

5. 只在 master分支跑 golang test + build成image

kind: pipeline
type: docker
name: backend

steps:
- name: practice
  when:
    branch: 
    - master
  image: docker:dind
  volumes:
  - name: dockersock
    path: /var/run/docker.sock
  commands:
  - docker build --no-cache --pull --force-rm -t rain/ithome:latest -f go.dockerfile .

volumes:
- name: dockersock
  host:
    path: /var/run/docker.sock

6. 當觸發 tag事件時,跑 golang test + build 成 image

kind: pipeline
type: docker
name: backend

steps:
- name: practice
  image: docker:dind
  volumes:
  - name: dockersock
    path: /var/run/docker.sock
  commands:
  - docker build --no-cache --pull --force-rm -t rain/ithome:latest -f go.dockerfile .

trigger:
  event:
  - tag

volumes:
- name: dockersock
  host:
    path: /var/run/docker.sock

今天練習到這邊,各位讀者們有沒有一點fu了呀?

明天會帶各位進入進階的練習唷~敬請期待!