Continuous Integration and Continuous Deployment (CI/CD) using Drone CI

Mirdan Syahid
4 min readJun 17, 2023

--

Continuous Integration and Continuous Deployment (CI/CD) pipelines have become essential for modern software development workflows. In this article, we will explore how to set up a robust CI/CD pipeline in Drone CI using a YAML script.

When considering a CI/CD solution, it’s essential to evaluate various factors such as flexibility, scalability, ease of use, and cost. Here are some compelling reasons why Drone CI might be considered among other CI/CD tools:

  1. Simplicity and Minimal Configuration: Drone CI embraces simplicity by utilizing a lightweight YAML-based configuration file. Its intuitive design allows developers to define pipeline stages, steps, and dependencies easily.
  2. Extensive Plugin Ecosystem: Drone CI boasts a rich ecosystem of plugins, enabling seamless integration with popular tools and services. Whether you need to deploy to cloud platforms like AWS, GCP or interact with container registries such as Docker Hub.
  3. Drone CI offers a free: Compare to GitLab with the similar type of the CICD tool, that offers a free tier, it often imposes limitations on usage, such as concurrent jobs or pipeline minutes. In contrast, Drone CI offers a free, open-source version that provides all the essential CI/CD features needed for most projects.

Anyway, this article will cover various stages, including build automation, continuous deployment using GitOps, and Slack notifications. By the end of this guide, you’ll have a clear basic understanding of how to leverage Drone CI to streamline your development process and automate deployments.

Prerequisites

Before we dive into the setup process, ensure that you have the following prerequisites in place:

  1. A Drone CI server: Set up a Drone CI server either locally or on a cloud provider of your choice.
  2. GitOps repository: Create a repository to store your deployment manifests, which will be used for continuous deployment, the deployment method will be using helm.
  3. Dockerfile: Create a dockerfile file at the root of your respository source code.
  4. Activate your repository source code at Drone CI Dashboard: this action will enable and add webhook setup on source code repository seamlessly.
  5. Access to Slack: Obtain a Slack workspace and the necessary permissions to send notifications.

Step 1: Configuring the Drone CI Pipeline

To get started, create a .drone.yml file at the root of your project repository. This file will define the CI/CD pipeline stages and their configurations. Let's outline the basic structure of the YAML script:

kind: pipeline
name: ci-cd-pipeline

steps:
- name: build
# Build configuration
- name: deploy
# Deployment configuration
- name: slack-notify
# Slack notification configuration

Step 2: Building your Application

In the build stage, you can define the necessary build steps for your application. This may include compiling code, running tests, and generating build artifacts. This steps is using plugin that drone provide specific for GCP. Customize the following example build configuration to suit your project:

steps:
- name: build and push image
image: plugins/gcr
settings:
repo: #path/to/registry
tags: ${DRONE_TAG}
json_key:
from_secret: GCR_TOKEN
dockerfile: Dockerfile

Step 5: Continuous Deployment with GitOps

Next, we’ll configure the deploy stage to achieve continuous deployment using GitOps. This stage will update the deployment repository with the image tag generated from the Git commit using Helm based. Modify the following template to suit your deployment setup:

steps:
- name: Commit New Tag Version
image: alpine/git
commands:
- git clone https://BITBUCKET_USERNAME:BITBUCKET_TOKEN@bitbucket.org/WORKSPACE_NAME/DEPLOYMENT_REPO
- cd #HELM_PATH
- git remote set-url origin https://BITBUCKET_USERNAME:BITBUCKET_TOKEN@bitbucket.org/WORKSPACE/DEPLOYMENT_REPO
- apk add yq jq
- yq eval -j values.yaml > values.json
- jq --arg tag ${DRONE_TAG} '(.image.tag) |= $tag' values.json > tmp.json
- yq eval -P tmp.json > values.yaml
- rm values.json tmp.json
- git add -A
- git commit -m "Automatically promoting new image version ${DRONE_TAG}" --author "DroneCI"
- git push

Make sure to replace the capitalized words above with your information that contains the necessary to interact with your deployment repository.

Step 4: Sending Slack Notifications

Keeping your team informed about the status of your CI/CD pipeline is crucial. In the slack-notify stage, we can integrate Slack notifications to receive updates on pipeline execution. Add the following configuration, replacing your-slack-webhook with the webhook URL provided by Slack:

steps:
- name: slack-notify
image: plugins/slack
settings:
webhook: your-slack-webhook
channel: #channel-name
template: |
{{#success build.status}}
Build succeeded for commit {{commit.sha}}
{{else}}
Build failed for commit {{commit.sha}}
{{/success}}

Step 5: Pipeline Execution and Results

Save the .drone.yml file and commit it to your project repository. Drone CI will automatically detect the changes and start executing the pipeline.

Throughout the pipeline execution, you will receive notifications in your configured Slack channel. Once the build and deployment stages are complete.

--

--

Mirdan Syahid

Delving into the intricate weave of cloud native technologies