# Drone CI with Gitea

> Lightweight Drone CI integrated with Gitea for self-hosted Git and continuous integration

- Category: DevOps & CI/CD
- Difficulty: intermediate
- Components: gitea, drone, drone-runner, postgres
- Tags: drone, ci-cd, gitea, git, lightweight, devops
- Source: https://docker.recipes/devops/drone-ci-gitea

## Overview

Gitea is a lightweight, self-hosted Git service that provides GitHub-like functionality with minimal resource requirements. Written in Go, Gitea offers repository hosting, pull requests, issue tracking, and built-in CI/CD capabilities while consuming significantly fewer resources than alternatives like GitLab. Its painless setup and maintenance make it ideal for small teams and self-hosted environments. This stack combines Gitea with Drone CI to create a complete DevOps pipeline for continuous integration and deployment. Drone CI acts as the automation engine, executing builds, tests, and deployments triggered by Git events from Gitea repositories. The PostgreSQL database ensures reliable data persistence for both Git repositories and CI/CD metadata, while the Docker-based runner provides isolated execution environments for pipeline tasks. This combination delivers enterprise-grade CI/CD capabilities without the overhead of larger platforms like Jenkins or GitLab CI. Teams seeking full control over their development workflow will find this stack particularly valuable. The Gitea-Drone integration provides immediate feedback on code changes, automated testing, and deployment pipelines while maintaining complete data sovereignty. Small development teams, startups, and organizations with strict data privacy requirements can establish professional development practices without relying on external services or complex infrastructure.

## Features

- GitHub-like web interface with repository browsing, pull requests, and issue tracking
- Drone CI pipeline execution with Docker-based isolation for build environments
- PostgreSQL backend providing ACID compliance and reliable data persistence
- OAuth2 authentication integration between Gitea and Drone for single sign-on
- Scalable runner architecture supporting multiple concurrent build jobs
- Webhook-driven automation triggering builds on push, pull request, and tag events
- YAML-based pipeline configuration with .drone.yml files in repositories
- Built-in Docker registry support for storing and deploying container images

## Use cases

- Small development teams requiring private Git hosting with automated testing
- Startups building CI/CD pipelines without external service dependencies
- Organizations with data sovereignty requirements needing on-premises DevOps tools
- Open source projects wanting self-hosted infrastructure for community contributions
- Homelab enthusiasts learning DevOps practices with production-like tooling
- Educational institutions teaching version control and continuous integration
- Companies migrating from GitHub/GitLab to reduce licensing costs while maintaining functionality

## Prerequisites

- Docker and Docker Compose installed with at least 2GB available RAM
- Minimum 1GB RAM recommended for PostgreSQL database performance
- Ports 3000 (Gitea), 8080 (Drone), and 222 (SSH) available on the host system
- Basic understanding of Git workflows and CI/CD pipeline concepts
- Docker socket access for Drone runner to execute containerized builds
- Environment variables configured for database passwords and OAuth credentials

## docker-compose.yml

```yaml
services:
  gitea:
    image: gitea/gitea:latest
    container_name: gitea
    restart: unless-stopped
    ports:
      - "${GITEA_HTTP:-3000}:3000"
      - "${GITEA_SSH:-222}:22"
    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=gitea
      - GITEA__database__PASSWD=${DB_PASSWORD}
    volumes:
      - gitea_data:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    depends_on:
      - gitea-db

  gitea-db:
    image: postgres:15-alpine
    container_name: gitea-db
    restart: unless-stopped
    environment:
      - POSTGRES_USER=gitea
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=gitea
    volumes:
      - gitea_db_data:/var/lib/postgresql/data

  drone:
    image: drone/drone:latest
    container_name: drone
    restart: unless-stopped
    ports:
      - "${DRONE_PORT:-8080}:80"
    environment:
      - DRONE_GITEA_SERVER=http://gitea:3000
      - DRONE_GITEA_CLIENT_ID=${GITEA_CLIENT_ID}
      - DRONE_GITEA_CLIENT_SECRET=${GITEA_CLIENT_SECRET}
      - DRONE_RPC_SECRET=${DRONE_RPC_SECRET}
      - DRONE_SERVER_HOST=${DRONE_HOST:-localhost:8080}
      - DRONE_SERVER_PROTO=http
    volumes:
      - drone_data:/data
    depends_on:
      - gitea

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

volumes:
  gitea_data:
  gitea_db_data:
  drone_data:
```

## .env

```bash
# Drone CI with Gitea
GITEA_HTTP=3000
GITEA_SSH=222
DRONE_PORT=8080

# Database
DB_PASSWORD=gitea_password

# Gitea OAuth App (create in Gitea settings)
GITEA_CLIENT_ID=your-gitea-client-id
GITEA_CLIENT_SECRET=your-gitea-client-secret

# Drone RPC Secret (generate with: openssl rand -hex 16)
DRONE_RPC_SECRET=your-rpc-secret

# Drone host (your server hostname)
DRONE_HOST=localhost:8080
```

## Usage notes

- Gitea at http://localhost:3000 - create admin account
- Create OAuth app in Gitea: Settings > Applications
- Drone at http://localhost:8080 - login via Gitea
- Create .drone.yml in repos for CI pipelines
- Runner auto-registers with Drone server
- Scale runners: docker compose up -d --scale drone-runner=3

## Troubleshooting

- Drone shows 'OAuth application not found': Verify GITEA_CLIENT_ID and GITEA_CLIENT_SECRET match the OAuth app created in Gitea settings
- Runner not connecting to Drone server: Ensure DRONE_RPC_SECRET matches between drone and drone-runner services
- Gitea SSH clone fails on port 22: Change GITEA_SSH environment variable to use port 222 or configure SSH client with custom port
- Pipeline builds failing with permission errors: Check that /var/run/docker.sock is properly mounted and accessible to the runner container
- PostgreSQL connection refused during startup: Wait for gitea-db container to fully initialize before Gitea attempts database connection
- Drone webhooks not triggering builds: Verify webhook URLs in Gitea repository settings point to the correct Drone server address

