Building Automated CI server with Drone and Docker
Docker is great tool to management linux containers. It brings DevOps to next level, from development to production environment. And of course, before deploy anything to production, software should be tested carefully and automatically.
That’s why Drone, a new lightweight CI server built-on top Go lang and Docker, will help us to resolve the testing problems in simple and fast way.
This guide will assume you already have Docker and Docker Compose tool. And of course, root permission ;)
Step 1 : Clone my example docker-compose here : https://github.com/khanhicetea/drone-ci
$ git clone https://github.com/khanhicetea/drone-ci $ cd drone-ci $ cp .env.example .env
Step 2 : Update your setting in
Step 3 : Run drone via docker-compose
$ source .env $ sudo docker-compose up -d
Step 4 : Go to your Drone url (remember use https url), then authorize with Github provider.
In example repo, I created a sample
.drone.sample.yml file so you can follow the structure to create own file.
I will explain some basics here
clone: git: image: plugins/git depth: 5 pipeline: phpunit: image: php:7 commands: - /bin/sh conflict_detector.sh - /bin/sh phplinter.sh app lib test - composer install -q --prefer-dist - test/db/import testdatabase root passwd testdb test.sql - php -d memory_limit=256M vendor/bin/phpunit --no-coverage --colors=never notify: image: plugins/slack webhook: [your_slack_webhook_url] channel: deployment username: DroneCI when: status: success notify-bug: image: plugins/slack webhook: [your_slack_webhook_url] channel: bugs username: DroneCI when: status: failure branch: production services: testdatabase: image: mysql:5.7 detach: true environment: - MYSQL_DATABASE=testdb - MYSQL_ROOT_PASSWORD=passwd
This file consists 3 sections :
- clone : To clone the source code and prepare for
pipelinestep. This section will be run first
- services : Declare your docker services (databases, ip server) which source code connect to. This section will be run at sametime with
- pipeline : Testing pipe, where you put testing logic here.
pipeline, I made a example PHP testing through these steps :
- Check conflicts in code (grep for
- Run PHP linter in application codes
- Run Composer to install all dependencies
- Import testing database to mysql services (using
testdatabasehostname to connect service)
- Run testing script via
Then, notify testing result via Slack channel ! ;)
A picture is worth a thousand words
Lets automate all the things !