iT邦幫忙

第 12 屆 iT 邦幫忙鐵人賽

DAY 12
0
DevOps

遠離 DevOops系列 第 12

[Day 12] Oops!Drone - 進階應用

大家早安

相信大家看完昨日的 [Day 11] Oops!Drone - 快速上手 ,並且練習完昨天的題目後,已經奠定了一些基礎,那麼我們今天來進階練習ㄧ下~
這些練習或許在未來的某一天,你們可以在某個地方應用的到!/images/emoticon/emoticon37.gif

進階練習

1.volume

第一個步驟就是定義主機的路徑。主機的路徑必須是絕對路徑。

volumes:
- name: mylog
  host:
    path: /tmp

而下一步驟是配置 Pipeline步驟,將命名的主機路徑掛載到容器中。容器的路徑也必須是絕對路徑。

steps:
- name: step_name
  volumes:
  - name: mylog
    path: /tmp

以下是完整的示範

kind: pipeline
type: docker
name: backend

steps:
- name: practice make file
  image: golang
  commands:
    - ls /tmp
    - echo hello drone > /tmp/hi.txt

  volumes:
  - name: mylog
    path: /tmp

- name: check volumes file
  image: golang
  commands:
    - ls /tmp
  volumes:
  - name: mylog
    path: /tmp

volumes:
- name: mylog
  host:
    path: /tmp

2.Environment 使用

設定參數在官網有文件說明

以下是我把全部的參數直接印出來的做法

kind: pipeline
type: docker
name: backend

steps:
- name: show environment
  image: golang
  commands:
    - echo "DRONE_BUILD_ACTION = ${DRONE_BUILD_ACTION}"
    - echo "DRONE_BUILD_CREATED = ${DRONE_BUILD_CREATED}"
    - echo "DRONE_BUILD_EVENT = ${DRONE_BUILD_EVENT}"
    - echo "DRONE_BUILD_FINISHED = ${DRONE_BUILD_FINISHED}"
    - echo "DRONE_BUILD_NUMBER = ${DRONE_BUILD_NUMBER}"
    - echo "DRONE_BUILD_PARENT = ${DRONE_BUILD_PARENT}"
    - echo "DRONE_BUILD_STARTED = ${DRONE_BUILD_STARTED}"
    - echo "DRONE_BUILD_STATUS = ${DRONE_BUILD_STATUS}"
    - echo "DRONE_COMMIT = ${DRONE_COMMIT}"
    - echo "DRONE_COMMIT_AFTER = ${DRONE_COMMIT_AFTER}"
    - echo "DRONE_COMMIT_AUTHOR = ${DRONE_COMMIT_AUTHOR}"
    - echo "DRONE_COMMIT_AUTHOR_AVATAR = ${DRONE_COMMIT_AUTHOR_AVATAR}"
    - echo "DRONE_COMMIT_AUTHOR_EMAIL = ${DRONE_COMMIT_AUTHOR_EMAIL}"
    - echo "DRONE_COMMIT_AUTHOR_NAME = ${DRONE_COMMIT_AUTHOR_NAME}"
    - echo "DRONE_COMMIT_BEFORE = ${DRONE_COMMIT_BEFORE}"
    - echo "DRONE_COMMIT_BRANCH = ${DRONE_COMMIT_BRANCH}"
    - echo "DRONE_COMMIT_LINK = ${DRONE_COMMIT_LINK}"
    - echo "DRONE_COMMIT_MESSAGE = ${DRONE_COMMIT_MESSAGE}"
    - echo "DRONE_COMMIT_REF = ${DRONE_COMMIT_REF}"
    - echo "DRONE_COMMIT_SHA = ${DRONE_COMMIT_SHA}"
    - echo "DRONE_DEPLOY_TO = ${DRONE_DEPLOY_TO}"
    - echo "DRONE_FAILED_STAGES = ${DRONE_FAILED_STAGES}"
    - echo "DRONE_FAILED_STEPS = ${DRONE_FAILED_STEPS}"
    - echo "DRONE_GIT_HTTP_URL = ${DRONE_GIT_HTTP_URL}"
    - echo "DRONE_GIT_SSH_URL = ${DRONE_GIT_SSH_URL}"
    - echo "DRONE_PULL_REQUEST = ${DRONE_PULL_REQUEST}"
    - echo "DRONE_REMOTE_URL = ${DRONE_REMOTE_URL}"
    - echo "DRONE_REPO = ${DRONE_REPO}"
    - echo "DRONE_REPO_BRANCH = ${DRONE_REPO_BRANCH}"
    - echo "DRONE_REPO_LINK = ${DRONE_REPO_LINK}"
    - echo "DRONE_REPO_NAME = ${DRONE_REPO_NAME}"
    - echo "DRONE_REPO_NAMESPACE = ${DRONE_REPO_NAMESPACE}"
    - echo "DRONE_REPO_OWNER = ${DRONE_REPO_OWNER}"
    - echo "DRONE_REPO_PRIVATE = ${DRONE_REPO_PRIVATE}"
    - echo "DRONE_REPO_SCM = ${DRONE_REPO_SCM}"
    - echo "DRONE_REPO_VISIBILITY = ${DRONE_REPO_VISIBILITY}"
    - echo "DRONE_SEMVER = ${DRONE_SEMVER}"
    - echo "DRONE_SEMVER_BUILD = {DRONE_SEMVER_BUILD}"
    - echo "DRONE_SEMVER_ERROR = ${DRONE_SEMVER_ERROR}"
    - echo "DRONE_SEMVER_MAJOR = ${DRONE_SEMVER_MAJOR}"
    - echo "DRONE_SEMVER_MINOR = ${DRONE_SEMVER_MINOR}"
    - echo "DRONE_SEMVER_PATCH = ${DRONE_SEMVER_PATCH}"
    - echo "DRONE_SEMVER_PRERELEASE = ${DRONE_SEMVER_PRERELEASE}"
    - echo "DRONE_SEMVER_SHORT = ${DRONE_SEMVER_SHORT}"
    - echo "DRONE_SOURCE_BRANCH = ${DRONE_SOURCE_BRANCH}"
    - echo "DRONE_STAGE_ARCH = ${DRONE_STAGE_ARCH}"
    - echo "DRONE_STAGE_DEPENDS_ON = ${DRONE_STAGE_DEPENDS_ON}"
    - echo "DRONE_STAGE_FINISHED = ${DRONE_STAGE_FINISHED}"
    - echo "DRONE_STAGE_KIND = ${DRONE_STAGE_KIND}"
    - echo "DRONE_STAGE_MACHINE = ${DRONE_STAGE_MACHINE}"
    - echo "DRONE_STAGE_NAME = ${DRONE_STAGE_NAME}"
    - echo "DRONE_STAGE_NUMBER = ${DRONE_STAGE_NUMBER}"
    - echo "DRONE_STAGE_OS = ${DRONE_STAGE_OS}"
    - echo "DRONE_STAGE_STARTED = ${DRONE_STAGE_STARTED}"
    - echo "DRONE_STAGE_STATUS = ${DRONE_STAGE_STATUS}"
    - echo "DRONE_STAGE_TYPE = ${DRONE_STAGE_TYPE}"
    - echo "DRONE_STAGE_VARIANT = ${DRONE_STAGE_VARIANT}"
    - echo "DRONE_STEP_NAME = ${DRONE_STEP_NAME}"
    - echo "DRONE_STEP_NUMBER = ${DRONE_STEP_NUMBER}"
    - echo "DRONE_SYSTEM_HOST = ${DRONE_SYSTEM_HOST}"
    - echo "DRONE_SYSTEM_HOSTNAME = ${DRONE_SYSTEM_HOSTNAME}"
    - echo "DRONE_SYSTEM_PROTO = ${DRONE_SYSTEM_PROTO}"
    - echo "DRONE_SYSTEM_VERSION = ${DRONE_SYSTEM_VERSION}"
    - echo "DRONE_TAG = ${DRONE_TAG}"
    - echo "DRONE_TARGET_BRANCH = ${DRONE_TARGET_BRANCH}"

3.使用多個Pipeline


kind: pipeline
type: docker
name: pipeline-01

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

---

kind: pipeline
type: docker
name: pipeline-02

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

---

kind: pipeline
type: docker
name: pipeline-03

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

https://ithelp.ithome.com.tw/upload/images/20200919/20115289HGxxOAj56p.png

https://ithelp.ithome.com.tw/upload/images/20200919/20115289jmMtGvIkDd.png

https://ithelp.ithome.com.tw/upload/images/20200919/20115289ao5pf6ZzPm.png

4.depends_on

kind: pipeline
type: docker
name: pipeline-01

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

---

kind: pipeline
type: docker
name: pipeline-02

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

depends_on:
- pipeline-01

---

kind: pipeline
type: docker
name: pipeline-03

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

depends_on:
- pipeline-02

https://ithelp.ithome.com.tw/upload/images/20200919/20115289Ls1DXBrnw5.png

https://ithelp.ithome.com.tw/upload/images/20200919/20115289Q12nd5E44I.png

https://ithelp.ithome.com.tw/upload/images/20200919/20115289NEh63HGKoq.png

5. clone disable

kind: pipeline
type: docker
name: pipeline-01

clone:
  disable: true

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

---

kind: pipeline
type: docker
name: pipeline-02

clone:
  disable: true

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

depends_on:
- pipeline-01

---

kind: pipeline
type: docker
name: pipeline-03


clone:
  disable: true

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

depends_on:
- pipeline-02

https://ithelp.ithome.com.tw/upload/images/20200919/20115289e0LSBjGAxw.png

https://ithelp.ithome.com.tw/upload/images/20200919/201152898hvcYrDJKI.png

https://ithelp.ithome.com.tw/upload/images/20200919/20115289wWUpCdMYYV.png

6.ignore

如果要允許某個Step失敗而不使整個Pipeline失敗的話,可以在你需要允許可接受失敗的Step上面加上此設定。

https://ithelp.ithome.com.tw/upload/images/20200919/20115289XsG76PykMs.png

https://ithelp.ithome.com.tw/upload/images/20200919/20115289A4YIUtFevb.png


kind: pipeline
type: docker
name: pipeline-01

clone:
  disable: true

steps:
- name: steps 01
  image: golang
  failure: ignore
  commands:
    - echo hello drone steps 01
    - ls abcdefg

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

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

效果就會如圖所示!
https://ithelp.ithome.com.tw/upload/images/20200919/20115289GQU1859zmh.png

Oops Environment

有些 Environment 可能不是如你預期的!
有些 Environment 可能是空值!
所以大家務必先行run看看,在你寫的 Pipeline內 Environment是否真的如你預期的取得,再導入到CI/CD流程內唷!

今日小結

這兩日的練習相信大家對Drone的CI/CD設定已經有更深的了解,希望大家未來可以運用到往後的CI/CD的戰場上囉!/images/emoticon/emoticon58.gif


上一篇
[Day 11] Oops!Drone - 快速上手
下一篇
[Day 13] Oops!Drone - Cron Jobs
系列文
遠離 DevOops30

尚未有邦友留言

立即登入留言