Continuous Integration and Delivery using Drone-CI for Docker Swarm Orchestrator - The Easy Way

Tech Stuff Nov 9, 2020

Well, here's the fact; i was looking for the best CI/CD tool for my little cloud cluster which is hosted in digital ocean. And let me say this; I tried every tool from Java based tools i.e. Jenkins, GoCd and cloud hosted tools CircleCI and Travis. All of them does the job done after all; well some have drawbacks but majority speaking they're all good but my preference goes to Mr. Drone after all. I'll try to explain why I've preferred drone-ci to those other CI/CD tools.

Why Drone CI

My CI/CD journey began with Jenkins. Jenkins is an open source ci/cd or automation tool which is wildly used out there. But that comes with a price if you really need speed on your automation process. Here's what i found out when working with Jenkins and GoCD tools; First one is, they're both Java based tools; which makes them a bit slow when doing their job. Also, when we say Java based tools, it runs on top of a Java Virtual Machine (JVM), thus it hits up CPU and RAM pretty good when running. When i say it hits up CPU and RAM; it actually generate a high bill depending on which cloud platform you're using. I've carefully analyzed when using those two tools in my small cluster found in digital ocean and it really has a difference when migrated from java based CI/CD tools to others (will shortly talk about this). Second one is their sizes of docker image files. well, it might not be a key factor to you; but for me, the less the size of the docker image, the more i fell in love with the service as it'll be super easy for the orchestrator tool to manage the service smoothly. Their size is 333.88MB (jenkins/jenkins:lts) and 295.46MB (gocd:v20.8.0) respectively. Now; Let us come to Mr. Drone, it's docker image size is almost 27MB (as of writing). Small and beautiful; right! Second one is; it's based on GoLang programming language, super fast and much more preferable and suitable for cloud infrastructure environments. Also in advance, it has simple and easy to use UI where anyone can understand what's going on in real time of automation. Documentation wise also; easy-peasy to understand. I don't want to bore you with a lot of non sense. Let's see how I set up my easy .drone.yaml file for my automation.

My Drone-CI (YAML) Automation File

kind: pipeline
type: docker
name: master

platform:
  os: linux
  arch: amd64

steps:
  - name: build
    image: plugins/docker
    settings:
      repo: image/repo
      tag: stack
      dockerfile: Dockerfile-stack
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password

  - name: refresh-site
    image: appleboy/drone-ssh
    settings:
      host: xxx.xxx.xxx.xxx
      username: username
      port: xxxx
      envs:
        - docker_username
        - docker_password
      key:
        from_secret: ssh_key
      script:
        - docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
        - docker pull image/repo:stack
        - docker service update --force serviceToUpdate

trigger:
  branch:
    - master
  event:
    - push
    - pull_request

---
kind: pipeline
type: docker
name: staging

platform:
  os: linux
  arch: amd64

steps:
  - name: build
    image: plugins/docker
    settings:
      repo: image/repo
      tag: staging
      dockerfile: Dockerfile-staging
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password

  - name: refresh-site
    image: appleboy/drone-ssh
    environment:
      DOCKER_USERNAME:
        from_secret: docker_username
      DOCKER_PASSWORD:
        from_secret: docker_password
    settings:
      host: xxx.xxx.xxx.xxx
      username: username
      port: xxxx
      envs:
        - docker_username
        - docker_password
      key:
        from_secret: ssh_key
      script:
        - docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
        - docker pull image/repo:staging
        - docker service update --force stagingServiceToUpdate
trigger:
  branch:
    - develop
  event:
    - push
    - pull_request
.drone.yml

In that file, I'm working with multiple pipelines which is one for the production environment and the other one for staging. Each of them relies on two plugins; one for building and pushing the image (i.e. plugins/docker) and the other one for applying the changes on the current running environment (i.e. appleboy/drone-ssh). The second one actually connects to the cluster through SSH and then executes some commands to refresh/update the service. All sensitive credential values can be added as a secret to Drone.IO so that we just call them by their variable when ever we want to use them.

Tags

Meron Hayle

Hi there, I'm Meron, a software engineer, an entrepreneur, and an artist, also known as a ninja in the art world.