Leon's Blogging

Coding blogging for hackers.

Drone

| Comments

透過 drone 可以很輕易的自己架設一個 CI CD server

Drone 是一套基於 Docker 容器技術的持續交付平台。

dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
version: '3'

services:
  drone-server:
    image: drone/drone:0.8.5
    container_name: drone-server
    ports:
      - 80:8000
      - 9000
    volumes:
      - ./db:/var/lib/drone/
    restart: always
    environment:
      - DRONE_OPEN=true
      - DRONE_HOST=${DRONE_HOST}
      - DRONE_GITHUB=true
      - DRONE_GITHUB_CLIENT=${DRONE_GITHUB_CLIENT}
      - DRONE_GITHUB_SECRET=${DRONE_GITHUB_SECRET}
      - DRONE_SECRET=${DRONE_SECRET}
      - DRONE_ADMIN=leon
      - DRONE_ORGS=test # Organization 名稱
      - DRONE_DEBUG=true

  drone-agent:
    image: drone/agent:0.8.5
    command: agent
    restart: always
    depends_on:
      - drone-server
    scale: 3
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - DRONE_SERVER=drone-server:9000
      - DRONE_SECRET=${DRONE_SECRET}
      - DRONE_MAX_PROCS=3
      - DRONE_DEBUG=true

.drone.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 所有流程共享一個 workspace
workspace:
  base: /rails # 所有步驟都可以存取此目錄資料
  path: src/github.com/mgleon08/project # 所有指令都在此目錄執行

# pull git 下來
clone:
  git:
    image: plugins/git:next
    pull: true
    depth: 50
    tags: true # 將 tag 一起拉下來

pipeline:
# 將一些不需要重複 build 的檔案 cache 起來 e.g. gem, node_module
  restore-cache:
    image: drillster/drone-volume-cache
    restore: true
    mount:
      - ./vendor
    # Mount the cache volume, needs "Trusted"
    volumes:
      - /tmp/cache:/cache
    when:
      local: false

  install:
    image: ruby:2.5
    pull: true
    environment:
      - RAILS_ENV=test
      - RAKE_ENV=test
    commands:
      - ruby -v
      - gem -v

      # bundle,檔案位置設定在 vendor/bundle
      - bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3

      # config
      - cp config/database.ci.yml config/database.yml

      # setup secrets and db migration
      - bundle exec rails -v
      - bundle exec rake generate:secrets --trace
      - bundle exec rake db:create db:migrate --trace
    when:
      local: false

  rubocop:
    image: ruby:2.5
    group: testing # 同一個 group 可同時執行,平行測試
    commands:
      - bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3
      - bundle exec rubocop -c .rubocop.yml || true
      - bundle exec rubocop -c .rubocop.yml --format=json --out=rubocop-result.json || true

  flay:
    image: ruby:2.5
    group: testing
    commands:
      - bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3
      - bundle exec flay app

  rspec:
    image: ruby:2.5
    group: testing
    environment:
      - RAILS_ENV=test
      - RAKE_ENV=test
    commands:
        - bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3
        - bundle exec rspec --color --tag ~@feature
    when:
      local: false

  rebuild-cache:
    image: drillster/drone-volume-cache
    group: cleanup
    rebuild: true
    mount:
      - ./vendor
    # Mount the cache volume, needs "Trusted"
    volumes:
      - /tmp/cache:/cache
    when:
      local: false

  notify:
    image: plugins/slack
    secrets: [ slack_webhook ] # 在 drone 介面上設定環境變數 secrets
    channel: channel_name
    username: Drone CI
    when:
      status: [ success, failure ] # 成功或失敗都通知訊息
      local: false

# database.yml 裡面的 database 要跟下面定義的名字一樣,這邊也是叫 database
services:
  database:
    image: mysql:5.7
    environment: # 全域變數
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes

Comments