Jul 26, 2024 3 min read

Drone CI: Self-Hosting Made Simple

Drone CI: Self-Hosting Made Simple
Table of Contents

Drone CI is a modern, lightweight continuous integration and delivery platform designed for self-hosting. It’s a popular choice among developers and system administrators because it offers full control over the CI/CD pipeline, seamless integration with version control systems, and a highly customizable environment. In this guide, we’ll walk through deploying, configuring, and managing Drone CI, focusing on Docker-based installation, reverse proxy setup with Nginx, logging, backups, updates, and leveraging its unique features.

Installing Drone CI

πŸ“¦ Docker/Docker Compose Setup

Drone CI is most commonly deployed using Docker. Below is a docker-compose.yml file tailored for Drone CI:


version: '3'

services:

drone-server:

image: drone/drone:latest

container_name: drone-server

ports:

- "80:80"  # Expose Drone on port 80

- "443:443"  # For HTTPS (later configured with Nginx)

volumes:

- /var/lib/drone:/data

restart: always

environment:

- DRONE_GITEA_SERVER=https://gitea.example.com  # Replace with your Gitea server URL

- DRONE_RPC_SECRET=supersecretkey  # Shared secret between server and runner

- DRONE_SERVER_HOST=drone.example.com

- DRONE_SERVER_PROTO=https

- DRONE_USER_CREATE=username:admin,admin:true

networks:

- drone-network

drone-runner:

image: drone/drone-runner-docker:latest

container_name: drone-runner

restart: always

depends_on:

- drone-server

environment:

- DRONE_RPC_PROTO=https

- DRONE_RPC_HOST=drone-server

- DRONE_RPC_SECRET=supersecretkey

- DRONE_RUNNER_CAPACITY=2

networks:

- drone-network

networks:

drone-network:

driver: bridge

To deploy Drone CI, execute the following commands:


mkdir -p /opt/drone && cd /opt/drone

nano docker-compose.yml  # Paste the above configuration

docker-compose up -d

This will spin up Drone CI and its runner. Replace placeholders (like the Gitea URL and domain) with your specifics.

πŸš€ Manual Installation

For those who prefer manual setup on a Linux server:

  1. Install Docker and Docker Compose:

sudo apt update && sudo apt install -y docker.io docker-compose

  1. Run Drone CI using Docker:

docker run -d \

--name drone-server \

-p 80:80 \

-p 443:443 \

-v /var/lib/drone:/data \

-e DRONE_GITEA_SERVER=https://gitea.example.com \

-e DRONE_RPC_SECRET=supersecretkey \

-e DRONE_SERVER_HOST=drone.example.com \

-e DRONE_SERVER_PROTO=https \

-e DRONE_USER_CREATE=username:admin,admin:true \

drone/drone:latest

Add a runner using similar Docker commands tailored to your requirements.

Configuring Nginx as a Reverse Proxy

🌐 Nginx Configuration

To expose Drone CI securely, configure an Nginx reverse proxy:


server {

server_name drone.example.com;

location / {

proxy_pass http://127.0.0.1:80;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-Proto $scheme;

}

listen 80;

}

Place this configuration in /etc/nginx/sites-available/drone and enable it:


sudo ln -s /etc/nginx/sites-available/drone /etc/nginx/sites-enabled/

sudo nginx -t

sudo systemctl reload nginx

πŸ”’ SSL/TLS Setup

Secure Drone CI with Let’s Encrypt:


sudo apt install certbot python3-certbot-nginx

sudo certbot --nginx -d drone.example.com

This automatically configures Nginx to serve traffic over HTTPS and sets up automatic renewal for the SSL certificate.

πŸ› οΈ Testing and Reloading Nginx

Validate the Nginx configuration and reload the service:


sudo nginx -t

sudo systemctl reload nginx

Access Drone CI at https://drone.example.com after Nginx is configured.

Logging and Debugging Drone CI

πŸ—ƒοΈ Enabling Debug Logs

To enable debug-level logging, set the appropriate environment variable in your docker-compose.yml:


environment:

- DRONE_LOGS_DEBUG=true

Restart the container to apply changes:


docker-compose restart drone-server

πŸ“„ Viewing Logs

View logs for the Drone server and runner:


docker logs -f drone-server

docker logs -f drone-runner

πŸ› οΈ Troubleshooting Common Issues

Check for:

  • Authentication errors: Ensure DRONE_RPC_SECRET matches between server and runner.

  • Connectivity issues: Verify DNS and proxy configurations.

πŸ“€ Exporting Logs

Forward logs to an ELK stack using Docker’s logging driver:


logging:

driver: "gelf"

options:

gelf-address: "udp://elk.example.com:12201"

Backup and Restore

πŸ—‚οΈ File-Based Backups

Backup the /var/lib/drone directory, which contains all Drone data:


tar -czvf drone-backup.tar.gz /var/lib/drone

πŸ”„ Database Backups

If using an external database, back it up with a command like:


mysqldump -u root -p drone_db > drone_db_backup.sql

πŸ“… Automated Backup Scripts

Automate backups with a cron job:


echo "0 2 * * * tar -czvf /backup/drone-$(date +\%F).tar.gz /var/lib/drone" | crontab -

Updating and Upgrading Drone CI

⬆️ Updating Docker Images

Pull the latest Docker images and redeploy:


docker-compose pull

docker-compose up -d

πŸ› οΈ Manual Updates

For manual installations, re-download the binary:


wget https://github.com/drone/drone/releases/latest/download/drone-server-linux-amd64

chmod +x drone-server-linux-amd64

mv drone-server-linux-amd64 /usr/local/bin/drone

Restart the application to apply updates.

πŸ” Checking for Updates

Verify the current version:


curl -s http://localhost/api/version

Leveraging Drone CI’s Unique Features

πŸ”§ Enabling APIs

Activate Drone’s API to interact programmatically:

  • Add DRONE_SERVER_ENABLE_API=true to your environment variables.

  • Test it with curl:


curl -H "Authorization: Bearer <token>" https://drone.example.com/api/repos

🌟 Advanced Configurations

Integrate with third-party tools like Slack by adding a notification plugin to your .drone.yml file:


pipeline:

notify:

image: plugins/slack

webhook: https://hooks.slack.com/services/XXX/YYY/ZZZ

channel: '#ci-cd'

username: 'drone'

Wrapping Up

Self-hosting Drone CI provides unparalleled control over your CI/CD pipelines. From deployment to advanced configurations, this guide covered all the essential steps to help you get started. With the provided examples, you can now fully leverage Drone CI to streamline your development workflows while maintaining full data ownership. Start exploring its flexibility and power today!

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Selfhosted Ninja.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.