# Drone CI with Gitea

> Container-native CI/CD with Drone, Gitea integration, Docker runner, and secrets management.

- Category: DevOps & CI/CD
- Difficulty: advanced
- Components: drone-server, drone-runner, gitea, postgresql
- Tags: drone, ci-cd, gitea, docker, pipelines
- Source: https://docker.recipes/devops/drone-ci-stack

## Overview

Drone is a container-native continuous integration platform built on Docker that transforms Git commits into automated testing and deployment pipelines. Originally created by Brad Rydzewski in 2014, Drone has evolved into a powerful CI/CD system that executes pipeline steps inside ephemeral Docker containers, providing true isolation and reproducibility. Unlike traditional CI systems that require agents with pre-installed tools, Drone's container-first approach means every build runs in a clean environment with exactly the dependencies specified in your pipeline configuration. This stack combines Drone with Gitea, a lightweight self-hosted Git service that provides GitHub-like functionality without the resource overhead of GitLab or enterprise solutions. Together, they create a complete DevOps platform where code repositories, issue tracking, and automated pipelines operate within your own infrastructure. The integration between Gitea and Drone enables automatic webhook-triggered builds, pull request validation, and deployment automation while maintaining complete control over your source code and build artifacts. This combination is particularly valuable for organizations requiring air-gapped development environments, teams with strict data sovereignty requirements, or developers who want enterprise-grade CI/CD capabilities without recurring SaaS costs.

## Features

- Container-native pipeline execution with Docker-in-Docker support for isolated build environments
- Native Gitea integration with OAuth2 authentication and automatic webhook configuration
- YAML-based pipeline definitions with multi-stage builds, parallel execution, and conditional steps
- Docker registry integration for automated image building, tagging, and deployment
- Encrypted secrets management with per-repository and organization-level secret storage
- Real-time build logs with terminal output streaming and downloadable artifacts
- Pull request automation with status checks, approval workflows, and merge conflict detection
- Multi-architecture build support for ARM, AMD64, and custom runner configurations

## Use cases

- Self-hosted development teams requiring private Git hosting with integrated CI/CD pipelines
- Organizations with compliance requirements needing air-gapped CI/CD infrastructure
- Microservices architectures requiring automated Docker image builds and registry management
- Open source projects needing cost-effective CI/CD without GitHub Actions minute limitations
- Development teams practicing GitOps with automated testing and deployment to Kubernetes clusters
- Startups and small companies wanting GitHub Enterprise features at zero recurring cost
- Educational institutions teaching DevOps practices with self-contained development environments

## Prerequisites

- Minimum 2GB RAM recommended (512MB for Gitea, 1GB+ for PostgreSQL instances, plus runner overhead)
- Docker Engine with BuildKit support and docker.sock access for the Drone runner
- Available ports 3000 (Gitea web), 222 (Gitea SSH), and 8080 (Drone web interface)
- Domain names or static IPs for DRONE_SERVER_HOST configuration and webhook callbacks
- Understanding of YAML pipeline syntax and Docker container concepts for build configuration
- SSL certificates for production deployments to enable secure webhook delivery

## docker-compose.yml

```yaml
services:
  gitea:
    image: gitea/gitea:latest
    environment:
      USER_UID: 1000
      USER_GID: 1000
      GITEA__database__DB_TYPE: postgres
      GITEA__database__HOST: gitea-db:5432
      GITEA__database__NAME: gitea
      GITEA__database__USER: ${POSTGRES_USER}
      GITEA__database__PASSWD: ${POSTGRES_PASSWORD}
    ports:
      - "3000:3000"
      - "222:22"
    volumes:
      - gitea_data:/data
    depends_on:
      gitea-db:
        condition: service_healthy
    networks:
      - drone-net
    restart: unless-stopped

  gitea-db:
    image: postgres:15-alpine
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: gitea
    volumes:
      - gitea_postgres:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - drone-net
    restart: unless-stopped

  drone:
    image: drone/drone:latest
    environment:
      DRONE_GITEA_SERVER: http://gitea:3000
      DRONE_GITEA_CLIENT_ID: ${DRONE_GITEA_CLIENT_ID}
      DRONE_GITEA_CLIENT_SECRET: ${DRONE_GITEA_CLIENT_SECRET}
      DRONE_RPC_SECRET: ${DRONE_RPC_SECRET}
      DRONE_SERVER_HOST: ${DRONE_SERVER_HOST}
      DRONE_SERVER_PROTO: http
      DRONE_DATABASE_DRIVER: postgres
      DRONE_DATABASE_DATASOURCE: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@drone-db:5432/drone?sslmode=disable
    ports:
      - "8080:80"
    volumes:
      - drone_data:/data
    depends_on:
      drone-db:
        condition: service_healthy
      gitea:
        condition: service_started
    networks:
      - drone-net
    restart: unless-stopped

  drone-db:
    image: postgres:15-alpine
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: drone
    volumes:
      - drone_postgres:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - drone-net
    restart: unless-stopped

  drone-runner:
    image: drone/drone-runner-docker:latest
    environment:
      DRONE_RPC_PROTO: http
      DRONE_RPC_HOST: drone
      DRONE_RPC_SECRET: ${DRONE_RPC_SECRET}
      DRONE_RUNNER_CAPACITY: 2
      DRONE_RUNNER_NAME: docker-runner
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    depends_on:
      - drone
    networks:
      - drone-net
    restart: unless-stopped

volumes:
  gitea_data:
  gitea_postgres:
  drone_data:
  drone_postgres:

networks:
  drone-net:
    driver: bridge
```

## .env

```bash
# PostgreSQL (shared)
POSTGRES_USER=drone
POSTGRES_PASSWORD=secure_postgres_password

# Drone Server
DRONE_SERVER_HOST=localhost:8080
DRONE_RPC_SECRET=your_rpc_secret_here

# Gitea OAuth2 (create in Gitea Settings > Applications)
DRONE_GITEA_CLIENT_ID=your_client_id
DRONE_GITEA_CLIENT_SECRET=your_client_secret
```

## Usage notes

- Gitea at http://localhost:3000
- Drone at http://localhost:8080
- Create OAuth2 app in Gitea first
- Add .drone.yml to repos for pipelines

## Troubleshooting

- OAuth2 application not found error: Create OAuth2 application in Gitea admin panel with correct redirect URI http://your-drone-host/login
- Pipeline fails with 'Cannot connect to Docker daemon': Ensure drone-runner container has access to /var/run/docker.sock and Docker socket permissions
- Gitea webhooks failing with connection refused: Verify DRONE_SERVER_HOST matches accessible hostname from Gitea container perspective
- Database connection errors on startup: Check PostgreSQL containers are healthy and POSTGRES_USER/POSTGRES_PASSWORD environment variables match across services
- Build secrets not accessible in pipeline: Verify secrets are added in Drone web UI at repository level and referenced correctly in .drone.yml with from_secret syntax
- Runner capacity exceeded errors: Increase DRONE_RUNNER_CAPACITY value or deploy additional drone-runner instances with unique DRONE_RUNNER_NAME values

