Docker 技術鼻祖系列

原文鏈接:https://muzim.cn/post/BVx30SK1M/

前言

本文會詳細介紹如何編寫一個 Drone 的 pipeline 文件。

pipeline 的意思就是流水線,持續集成其實就是一個流水線工作,指定一系列的動作,然後讓程序按順序和條件去執行。所以寫好一個 pipeline 就很重要。本文會介紹比較常用的 pipeline 流水線模式。

1. Drone 的 Pipeline 語法

Drone 的 pipeline 語法和 k8s 非常的相似,也是 yaml 文件。這裏貼上 nodejs 的構建示例來作爲展示 (附帶註釋講解):

#分隔符
---
#定義類型
kind: pipeline
#定義pipeline名字
name: default
#定義步驟,固定語法
steps:
#定義步驟的名字
- name: restore-cache
#定義該步驟用到的鏡像
image: drillster/drone-volume-cache
#掛載緩存
volumes:
#掛載的名字
- name: cache
#掛載路徑
path: /cache
#該步驟的設置(就是容器的環境變量)
settings:
restore: true
mount:
- ./node_modules
#定義下一個步驟
- name: build
#所用到的鏡像
image: node:8.15
#鏡像運行時候的命令(就是docker裏面的command)
commands:
- yarn config set registry https:
- yarn install
- yarn run build
#緩存相關的
- name: rebuild-cache
image: drillster/drone-volume-cache
volumes:
- name: cache
path: /cache
settings:
rebuild: true
mount:
- ./node_modules
#當對應條件的時候纔會執行
when:
#pipeline狀態爲成功和失敗的時候。
status:
- success
- failure

#設定掛載的路徑
volumes:
- name: cache
host:
path: /tmp/cache
- name: docker
host:
path: /var/run/docker.sock

通過上面的 yaml 文件,我們定義了三個步驟:

  1. 取出緩存中的內容

  2. 使用 yarn 構建

  3. 將新的緩存放回去

當 pipeline 被觸發的時候,就會安裝上面的步驟,依序執行。

2. 多個 Pipeline 的執行

通常我們不止一個流水線,可能需要並行執行,執行完一個流水線後再執行其他的流水線。Drone 一樣可以做到,只需要加個分隔符即可。這和 k8s 裏面是一樣的,這裏用 nodejs 項目構建同時執行代碼掃描來做個例子:

kind: pipeline
name: build

steps:
- name: build
image: node:8.15
commands:
- yarn config set registry https://registry.npm.taobao.org
- yarn install
- yarn run build


kind: pipeline
name: scan

steps:
- name: code-analysis
image: aosapps/drone-sonar-plugin
settings:
sonar_host: http://sonar.asoco.com.cn
sonar_token:
#從密文獲取(drone的secret設置)
from_secret: sonar_token

上面的例子就是同時執行,只需要寫 2 個 pipeline,然後用 --- 分割開即可。

但是我們很多時候是需要爲後一個 pipeline 設定條件的,比如我們規定構建成功後再做代碼掃描,因爲構建不成功,也就沒有做代碼掃描的必要了對吧。這裏用上面的例子:

kind: pipeline
name: build

steps:
- name: build
image: node:8.15
commands:
- yarn config set registry https://registry.npm.taobao.org
- yarn install
- yarn run build


kind: pipeline
name: scan

steps:
- name: code-analysis
image: aosapps/drone-sonar-plugin
settings:
sonar_host: http://sonar.asoco.com.cn
sonar_token:
from_secret: sonar_token

#只有上一個pipeline成功纔會觸發
trigger:
status:
- success

#在指定的pipeline完成之後進行驗證
depends_on:
- build

可以看到,再上面,我們只需要再需要加入條件的 pipeline 裏面加上限制即可。也就是註釋了的那幾段。

3. 構建 docker 鏡像

由於 drone 是基於原生的 docker 來做持續集成的。所以構建 docker 鏡像實際上就是用了 docker in docker。官方也直接給了個示例。這裏我依然用 nodejs 的項目作爲例子

kind: pipeline
name: default

steps:
- name: build
image: node:8.15
commands:
- yarn config set registry https://registry.npm.taobao.org
- yarn install
- yarn run build

- name: publish
image: plugins/docker
settings:
mirror: https://docker.mirrors.ustc.edu.cn
# 註冊倉庫(可以是harbor之類的地址)
registry: registry.cn-hangzhou.aliyuncs.com
# docker倉庫地址
repo: registry.cn-hangzhou.aliyuncs.com/lm93129/drone-test
#自動給鏡像打tag,這裏是根據推送上來的git信息來自動打tag的
auto_tag: true
#鏡像倉庫的賬戶密碼
password:
from_secret: docker_password
username:
from_secret: docker_username
purge: true
#運行該步驟的條件
when:
branch:
- master

這裏 publish 就是構建鏡像並且推送鏡像到鏡像倉庫了。plugins/docker 這個鏡像是固定的,drone 官方給出的,有興趣的,可以自己研究下這個鏡像。主要的參數都有註釋說明,還有其他問題可以看看官方的這個插件文檔。

4. 通知推送

構建成功還是失敗,肯定是需要來個釘釘、郵件、短信、微信之類的推送下對吧。

這裏以釘釘爲例:

---
kind: pipeline
name: default

steps:
- name: dingtalk
image: lddsb/drone-dingtalk-message
settings:

token:
from_secret: dingding_token

type: markdown

message_color: true

sha_link: true
when:
status:
- failure

這個釘釘推送是 lddsb 製作的插件,當然如果想自己自定短語之類的,可以自己做個,比如你有自己的企業 IM,需要做個推送,就可以自己寫腳本,然後做成一個插件,然後就可以吧鏡像換成自己的插件,來做消息推送了。

上面大部分的 pipeline 配置,裏面用到的鏡像都是官方收錄的鏡像,以及一些系統環境鏡像。Drone 的靈活在於可以自己輕鬆的定製鏡像來實現各種各樣的需求。以後有空,可能還會出一個 Drone 的鏡像定製教程,實現自己寫插件。

你可能還喜歡

點擊下方圖片即可閱讀

只有 4000 行代碼的 WireGuard 不權威指南:理論篇

雲原生是一種信仰 

碼關注公衆號

後臺回覆◉k8s◉獲取史上最方便快捷的 Kubernetes 高可用部署工具,只需一條命令,連 ssh 都不需要!

點擊  "閱讀原文"  獲取 更好的閱讀體驗!

:heart: 給個 「在看」 ,是對我最大的支持:heart:

相關文章