LoginSignup
0
0

More than 3 years have passed since last update.

docker-compose版drone 1.10.1でRSpec + docker build & push + slack

Posted at

概要と流れ

droneはクラウドではなく、自前でホストします。

※droneは情報も少なく、バージョンが新旧あって設定が混乱しますので、動いたやつを保存しときます。

まずは、droneの1.10.1のdocker-compose.yamlを作成します。これはどこかでホストし、ドメインを振って立ち上げとく。そして、githubのほうの設定をしとく(省略)。

この記事の.drone.ymlでdroneが動く流れとしては、githubのmaster( or main)にpushされたら(pull request mergeとか)、droneが起動してRSpecが動いて、NGならばslackされ終了。OKならば、docker buildの上dockerhubにpush、その結果OK/NG結果はslackされます。

それと、bundle installの結果を、ホストボリュームにキャッシュし、2回目からは速いという技を入れています。

本来なら、このあと、デプロイサーバのwebhookを叩いて、対象のインスタンスをdockerhubから更新して再起動すればいいのですが省略。

設定

docker-compose.yaml
version: '3.8'

services:
  drone-server:
    image: drone/drone:1.10.1
    ports:
      - 4080:80
    volumes:
      - drone_data:/data
      - /var/run/docker.sock:/var/run/docker.sock    
    restart: always
    environment:
      - DRONE_GITHUB_CLIENT_ID=xxxxxxxxxxxxxxxxxxxx
      - DRONE_GITHUB_CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
      - DRONE_RPC_SECRET=honi
      - DRONE_SERVER_PROTO=http
      - DRONE_SERVER_HOST=drone.domain.com
      - DRONE_USER_CREATE=username:yuknak,admin:true

  drone-agent:
    image: drone/drone-runner-docker:1.6.3
    command: agent
    restart: always
    depends_on:
      - drone-server
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - DRONE_RPC_HOST=drone-server:80
      - DRONE_RPC_PROTO=http
      - DRONE_RPC_SECRET=honi

volumes:
  drone_data:
    driver_opts:
      type: none
      device: "/home/user/docker_volumes/drone_data"
      o: bind

DRONE_GITHUB_CLIENT_ID, DRONE_GITHUB_CLIENT_SECRETはgithubから文字列を取得してきて貼る。volumesは新しく/dataになっているので注意。間違えるとデータがコンテナに入るので注意。DRONE_RPC_SECRETはなんでもいいけど、合わせとく。DRONE_USER_CREATEは、githubユーザを指定すればOK。これをやらないと、Trustedなどの設定が管理画面に出てこないので必須。DRONE_SERVER_PROTOは、私はcloudflareを使うとかの都合で、httpでいいです。DRONE_SERVER_HOSTは立ち上げるサーバ名です。portsは私は都合で上記のようになっています。volumesは上記の都合で指定したほうがいいでしょう。

.drone.yml
---
kind: pipeline
type: docker
name: test

services:
- name: database
  image: mysql:5.7.32
  ports:
    - 3306
  environment:
    MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'

steps:
- name: wait-for-services
  image: jwilder/dockerize
  commands:
  - dockerize -wait tcp://database:3306 -timeout 1m

# Copy bundler cache from the host machine to your build environment.
# Plugin description: http://plugins.drone.io/drillster/drone-volume-cache
- name: restore-bundle-cache
  pull: if-not-exists
  image: drillster/drone-volume-cache
  settings:
    restore: true
    mount:
    - vendor/bundle
  volumes:
  - name: bundle-cache
    path: /cache

- name: run-rspec
  image: ruby:2.7.2
  environment:
    DATABASE_HOST: database
  commands:
  - gem install bundler:2.2.4
  - bundle install --path vendor/bundle
  - bundle exec rake db:create db:migrate
  - LOG_LEVEL=warn bundle exec rspec

- name: rebuild-bundle-cache
  pull: if-not-exists
  image: drillster/drone-volume-cache
  settings:
    rebuild: true
    mount:
    - vendor/bundle
  volumes:
  - name: bundle-cache
    path: /cache

#If you want to use slack-blame,
#https://blog.kerkerj.in/2020/11/how-to-setup-slack-and-drone-slack-blame/
- name: slack
  image: plugins/slack
  settings:
    webhook:
      from_secret: slack_webhook_url
    channel: tetraserve
  when:
    status:
    - failure

volumes:
- name: bundle-cache
  host:
    path: /tmp/bundle

trigger:
  event:
  - push
  branch:
  - master
  - main

---
kind: pipeline
type: docker
name: build

steps:
- name: docker-build-and-push
  image: plugins/docker
  settings:
    username:
      from_secret: docker_username
    password:
      from_secret: docker_password
    repo: yuknak/snn
    tags: 1.0.4

- name: slack
  image: plugins/slack
  settings:
    webhook:
      from_secret: slack_webhook_url
    channel: tetraserve
  when:
    status: [ success, failure ]

trigger:
  event:
  - push
  branch:
  - master
  - main

depends_on:
- test

slack_webhook_urlと、docker_usernameと、docker_passwordは、droneの管理画面から指定してください。管理画面では、Trustedを指定してください。そうでないとvolume関連でエラーになります。

あと適当に自分の環境に合わせてください。

キャッシュ関連は技です。不要な場合は全部削除したらいけます。

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0