iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 11
0
DevOps

遠離 DevOops系列 第 11

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

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

認識設定值

  • kind - 定義對象屬性的類別
    目前有

    • Pipeline -> 流程定義 (最常用的)
    • Secret -> 儲存敏感資訊,可以在drone server上操作
    • Signature -> 是透過drone cli產生,主要是防止被串改使用
  • type - 此屬性定義 Pipeline的類型
    目前下列有六種

    • docker
    • kubernetes
    • exec
    • ssh
    • digitalocean
    • macstadium
  • name - 此屬性定義 Pipeline的名稱

  • steps - 定義了一連串執行 Pipeline的步驟。 如果 Pipeline中的任何步驟失敗,將會立即退出

    • name - 定義 Pipeline 步驟的名稱
    • image - 定義執行腳本的Docker映像檔案
    • commands - 定義了在 Docker容器內作為容器入口點執行的 Shell命令列表。 如果有任何命令返回非零退出,則 Pipeline步驟會失敗。

看完上述描述可以看下方的 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了呀?/images/emoticon/emoticon69.gif

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


上一篇
[Day 10] Oops!Drone - 建置與安裝
下一篇
[Day 12] Oops!Drone - 進階應用
系列文
遠離 DevOops30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言