🚁

Máy bay không người lái

CI/CD và DevOps

Nền tảng tích hợp liên tục dựa trên container với cấu hình YAML đơn giản.

Thông tin triển khai

Triển khai: 2-5 phút
loại: CI/CD và DevOps
RAM tối thiểu: 1024MB
Ủng hộ: Tiền vé và thư điện tử

Chia sẻ hướng dẫn này

Khám phá

Drone là nền tảng CI/CD (tích hợp liên tục và phân phối liên tục) hiện đại, hướng container và được xây dựng trên Docker. Nền tảng này nổi bật nhờ sự đơn giản, kiến trúc nhẹ và thiết kế cloud-native giúp dễ triển khai, mở rộng và bảo trì. Khác với các hệ thống CI/CD truyền thống đòi hỏi hạ tầng và cấu hình phức tạp, Drone chạy dưới dạng một binary nhẹ duy nhất với rất ít phụ thuộc, nên phù hợp với nhóm ở mọi quy mô.

Điều làm Drone khác biệt là cách tiếp cận pipeline dựa trên container: mỗi bản build chạy trong các container Docker biệt lập, đảm bảo bản build sạch, có thể tái tạo và không xung đột. Cấu hình pipeline là YAML đơn giản lưu trong kho mã, giúp dễ quản lý phiên bản và sao chép giữa các dự án. Drone tự động nhận biết khi bạn push mã, mở pull request hoặc gắn tag phát hành, và kích hoạt pipeline phù hợp ngay lập tức.

Hệ sinh thái plugin của Drone rất phong phú, với hàng trăm plugin do cộng đồng xây dựng để triển khai lên AWS, Kubernetes, registry Docker, thông báo Slack và nhiều hơn nữa. Vì Drone hoạt động gốc với Docker, bạn có thể chạy bất kỳ Docker image nào làm bước trong pipeline, mang lại tính linh hoạt không giới hạn. Cần chạy kiểm thử bằng Python 3.11? Triển khai lên AWS? Gửi thông báo Slack? Chỉ cần tham chiếu Docker image phù hợp trong pipeline.

Nền tảng hỗ trợ mọi hệ thống quản lý phiên bản lớn, gồm GitHub, GitLab, Bitbucket và Gitea, với tích hợp OAuth gốc để xác thực liền mạch. Drone có thể mở rộng theo chiều ngang bằng cách thêm các runner node, xử lý hàng trăm bản build đồng thời trên nhiều máy chủ. Kiến trúc không lưu trạng thái cho phép bạn tăng hoặc giảm quy mô theo nhu cầu mà không cần cấu hình cluster phức tạp.

Drone cung cấp các tính năng mạnh mẽ như matrix build để kiểm thử trên nhiều phiên bản, thực thi có điều kiện dựa trên mẫu branch hoặc tag, quản lý secret để lưu thông tin xác thực an toàn, và build caching để tăng tốc các bản build lặp lại. Giao diện web gọn gàng và trực quan, hiển thị log thời gian thực, lịch sử build và chi tiết từng bước trong pipeline.

Với đội ngũ doanh nghiệp, Drone cung cấp các tính năng nâng cao như RBAC (kiểm soát truy cập dựa trên vai trò), audit log, và khả năng giới hạn kho mã nào được dùng plugin nào để tăng bảo mật. Tính năng autoscaling giúp tiết kiệm chi phí: runner chỉ được khởi động khi có build đang xếp hàng và tắt khi nhàn rỗi, rất phù hợp với môi trường đám mây nơi bạn trả tiền theo phút.

Drone là phần mềm mã nguồn mở, có cả phiên bản cộng đồng và phiên bản doanh nghiệp. Phiên bản cộng đồng có đầy đủ tính năng và phù hợp với nhóm nhỏ đến vừa, trong khi phiên bản doanh nghiệp bổ sung hợp đồng hỗ trợ, tính năng bảo mật nâng cao và hỗ trợ ưu tiên. Dù bạn là startup hay doanh nghiệp lớn, Drone đều mở rộng theo nhu cầu của bạn.

Tính năng chính

Pipeline hướng container

Mỗi bước build chạy trong container Docker biệt lập, cho bản build sạch, có thể tái tạo và không phụ thuộc

Cấu hình YAML đơn giản

Pipeline như mã với cú pháp YAML dễ hiểu, được lưu trong kho mã

Hệ sinh thái plugin phong phú

Hàng trăm plugin để triển khai, thông báo, kiểm thử và tích hợp với mọi dịch vụ

Runner tự động mở rộng

Mở rộng theo chiều ngang với nhiều runner node, tự động cấp phát và thu hồi dựa trên số lượng build đang chờ

Tích hợp VCS gốc

Hỗ trợ đầy đủ GitHub, GitLab, Bitbucket, Gitea với OAuth và tự động hóa qua webhook

Quản lý secret

Lưu trữ secret được mã hóa với kiểm soát truy cập chi tiết theo từng kho mã hoặc tổ chức

Ví dụ sử dụng

• CI/CD cho microservices: Build và triển khai nhiều microservice với các pipeline biệt lập, dựa trên container cho từng dịch vụ
• Dự án đa ngôn ngữ: Kiểm thử dự án trên nhiều ngôn ngữ và phiên bản bằng matrix build (Python 3.8-3.11, Node 14-18, v.v.)
• Build Docker image: Build, gắn tag và push Docker image lên các registry như Docker Hub, AWS ECR hoặc registry riêng
• Triển khai Kubernetes: Tự động triển khai Kubernetes bằng Helm chart hoặc plugin kubectl sau khi build thành công
• Kiểm thử pull request: Tự động chạy kiểm thử trên mọi PR trước khi merge, với status check tích hợp vào GitHub/GitLab
• Triển khai nhiều môi trường: Triển khai lên các môi trường dev/staging/production dựa trên mẫu branch và phê duyệt thủ công
• Hạ tầng dưới dạng mã: Kiểm thử và triển khai cấu hình Terraform, CloudFormation hoặc Ansible bằng pipeline tự động

Chỉ dẫn cài đặt

Cài đặt trên VPS Ubuntu:

1. Cài đặt Docker (nếu chưa được cài):
# Install Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

# Add current user to docker group
sudo usermod -aG docker $USER
newgrp docker

# Verify installation
docker --version
2. Tạo cấu hình máy chủ Drone:
# Create directory for Drone
sudo mkdir -p /var/lib/drone
sudo chown -R $USER:$USER /var/lib/drone

# Create environment file
cat > /var/lib/drone/.env <<'EOF'
# Drone Server Settings
DRONE_SERVER_HOST=drone.yourdomain.com
DRONE_SERVER_PROTO=https
DRONE_RPC_SECRET=$(openssl rand -hex 16)
DRONE_DATABASE_DRIVER=sqlite3
DRONE_DATABASE_DATASOURCE=/data/database.sqlite

# GitHub Integration (or use GitLab/Bitbucket)
DRONE_GITHUB_CLIENT_ID=your_github_oauth_app_id
DRONE_GITHUB_CLIENT_SECRET=your_github_oauth_secret

# Admin user (your GitHub username)
DRONE_USER_CREATE=username:yourgithubusername,admin:true

# Optional: Enable registration
DRONE_REGISTRATION_CLOSED=false
EOF
3. Tạo RPC secret:
# Generate a secure RPC secret
openssl rand -hex 16
# Update DRONE_RPC_SECRET in .env file
4. Thiết lập ứng dụng OAuth GitHub:
- Truy cập: https://github.com/settings/applications/new
- Tên ứng dụng: Drone CI
- URL trang chủ: https://drone.yourdomain.com
- URL gọi lại ủy quyền: https://drone.yourdomain.com/login
- Sao chép Client ID và Client Secret vào tệp .env

5. Khởi động máy chủ Drone:
docker run   --volume=/var/lib/drone:/data   --env-file=/var/lib/drone/.env   --publish=8080:80   --publish=443:443   --restart=always   --detach=true   --name=drone   drone/drone:2
6. Kiểm tra máy chủ Drone:
# Check container status
docker ps | grep drone

# Check logs
docker logs drone

# Test web interface
curl http://localhost:8080
7. Cài đặt Drone runner (Docker runner):
docker run --detach   --volume=/var/run/docker.sock:/var/run/docker.sock   --env=DRONE_RPC_PROTO=http   --env=DRONE_RPC_HOST=localhost:8080   --env=DRONE_RPC_SECRET=your_rpc_secret_from_env   --env=DRONE_RUNNER_CAPACITY=2   --env=DRONE_RUNNER_NAME=my-first-runner   --publish=3000:3000   --restart=always   --name=runner   drone/drone-runner-docker:1
8. Cấu hình reverse proxy Nginx (production):
sudo nano /etc/nginx/sites-available/drone

# Add this configuration:
server {
    listen 80;
    server_name drone.yourdomain.com;

    location / {
        proxy_pass http://localhost:8080;
        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;
    }
}

# Enable site
sudo ln -s /etc/nginx/sites-available/drone /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
9. Thiết lập SSL với Let's Encrypt:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d drone.yourdomain.com
10. Tạo pipeline mẫu (.drone.yml):
kind: pipeline
type: docker
name: default

steps:
  - name: test
    image: node:18
    commands:
      - npm install
      - npm test

  - name: build
    image: node:18
    commands:
      - npm run build

  - name: docker
    image: plugins/docker
    settings:
      repo: yourusername/yourapp
      tags: latest
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password
    when:
      branch:
        - main
11. Thêm kho mã:
- Đăng nhập vào giao diện web Drone: https://drone.yourdomain.com
- Xác thực bằng GitHub
- Kích hoạt kho mã của bạn
- Push .drone.yml lên kho mã
- Xem build tự động được kích hoạt!

12. Thêm secret (qua CLI hoặc giao diện):
# Install Drone CLI
curl -L https://github.com/harness/drone-cli/releases/latest/download/drone_linux_amd64.tar.gz | tar zx
sudo install -t /usr/local/bin drone

# Configure CLI
export DRONE_SERVER=https://drone.yourdomain.com
export DRONE_TOKEN=your_user_token_from_ui

# Add secrets
drone secret add --repository yourusername/yourrepo --name docker_username --data yourvalue
drone secret add --repository yourusername/yourrepo --name docker_password --data yourvalue

Gợi ý cấu hình

Cấu hình thiết yếu:

1. Cấu hình máy chủ (tệp .env):
# Core Settings
DRONE_SERVER_HOST=drone.example.com
DRONE_SERVER_PROTO=https
DRONE_RPC_SECRET=supersecretkey

# Database
DRONE_DATABASE_DRIVER=postgres
DRONE_DATABASE_DATASOURCE=postgres://user:pass@localhost:5432/drone?sslmode=disable

# VCS Integration (GitHub example)
DRONE_GITHUB_CLIENT_ID=github_oauth_app_id
DRONE_GITHUB_CLIENT_SECRET=github_oauth_secret

# User Management
DRONE_USER_CREATE=username:admin,admin:true
DRONE_USER_FILTER=organization_name  # Only allow specific org
2. Cấu hình pipeline (.drone.yml), cơ bản:
kind: pipeline
type: docker
name: build-test-deploy

steps:
  - name: install
    image: node:18
    commands:
      - npm ci

  - name: test
    image: node:18
    commands:
      - npm test
      - npm run lint

  - name: build
    image: node:18
    commands:
      - npm run build

  - name: deploy
    image: plugins/ssh
    settings:
      host: production.example.com
      username: deploy
      key:
        from_secret: ssh_private_key
      script:
        - cd /var/www/app
        - git pull
        - npm install
        - pm2 restart app
    when:
      branch:
        - main
3. Matrix build (nhiều phiên bản):
kind: pipeline
type: docker
name: matrix-test

steps:
  - name: test
    image: python:${PYTHON_VERSION}
    commands:
      - pip install -r requirements.txt
      - pytest

matrix:
  PYTHON_VERSION:
    - "3.8"
    - "3.9"
    - "3.10"
    - "3.11"
4. Build và push Docker image:
kind: pipeline
type: docker
name: docker-build

steps:
  - name: build-and-push
    image: plugins/docker
    settings:
      repo: username/myapp
      tags:
        - latest
        - ${DRONE_COMMIT_SHA:0:8}
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password
      dockerfile: Dockerfile
      context: .
5. Triển khai Kubernetes:
kind: pipeline
type: docker
name: k8s-deploy

steps:
  - name: deploy-to-k8s
    image: sinlead/drone-kubectl
    settings:
      kubernetes_server: https://k8s.example.com
      kubernetes_token:
        from_secret: k8s_token
      kubernetes_cert:
        from_secret: k8s_cert
    commands:
      - kubectl set image deployment/myapp myapp=username/myapp:${DRONE_COMMIT_SHA}
      - kubectl rollout status deployment/myapp
    when:
      branch:
        - main
6. Thực thi có điều kiện:
kind: pipeline
type: docker
name: conditional-deploy

steps:
  - name: deploy-staging
    image: plugins/ssh
    commands:
      - deploy to staging
    when:
      branch:
        - develop

  - name: deploy-production
    image: plugins/ssh
    commands:
      - deploy to production
    when:
      branch:
        - main
      event:
        - push
        - tag

  - name: deploy-on-tag
    image: plugins/ssh
    commands:
      - deploy release
    when:
      event:
        - tag
      ref:
        - refs/tags/v*
7. Services (cơ sở dữ liệu, Redis để kiểm thử):
kind: pipeline
type: docker
name: integration-tests

services:
  - name: postgres
    image: postgres:14
    environment:
      POSTGRES_USER: testuser
      POSTGRES_PASSWORD: testpass
      POSTGRES_DB: testdb

  - name: redis
    image: redis:7

steps:
  - name: test
    image: python:3.11
    environment:
      DATABASE_URL: postgres://testuser:testpass@postgres:5432/testdb
      REDIS_URL: redis://redis:6379
    commands:
      - pip install -r requirements.txt
      - pytest tests/integration/
8. Quản lý secret:
# Via CLI
drone secret add --repository user/repo --name aws_access_key --data value
drone secret add --repository user/repo --name aws_secret_key --data value

# In pipeline
kind: pipeline
type: docker
name: default

steps:
  - name: deploy
    image: plugins/s3
    settings:
      access_key:
        from_secret: aws_access_key
      secret_key:
        from_secret: aws_secret_key
      bucket: my-bucket
      source: dist/**/*
9. Cache phụ thuộc:
kind: pipeline
type: docker
name: cached-build

steps:
  - name: restore-cache
    image: drillster/drone-volume-cache
    volumes:
      - name: cache
        path: /cache
    settings:
      restore: true
      mount:
        - ./node_modules

  - name: build
    image: node:18
    commands:
      - npm ci
      - npm run build

  - name: rebuild-cache
    image: drillster/drone-volume-cache
    volumes:
      - name: cache
        path: /cache
    settings:
      rebuild: true
      mount:
        - ./node_modules

volumes:
  - name: cache
    host:
      path: /var/lib/drone/cache
10. Thông báo (Slack, email):
kind: pipeline
type: docker
name: notify

steps:
  - name: build
    image: node:18
    commands:
      - npm run build

  - name: slack
    image: plugins/slack
    settings:
      webhook:
        from_secret: slack_webhook
      channel: deployments
      template: >
        {{#success build.status}}
          ✅ Build #{{build.number}} succeeded. Branch: {{build.branch}}
        {{else}}
          ❌ Build #{{build.number}} failed. Branch: {{build.branch}}
        {{/success}}
    when:
      status:
        - success
        - failure
Thực hành tốt nhất:
- Lưu dữ liệu nhạy cảm trong secret, không bao giờ đặt trong .drone.yml
- Dùng tag image cụ thể (node:18) thay vì latest
- Bật caching để build nhanh hơn (node_modules, gói pip)
- Dùng services để kiểm thử tích hợp với cơ sở dữ liệu thật
- Triển khai matrix build để kiểm thử nhiều phiên bản
- Dùng thực thi có điều kiện để tránh triển khai không cần thiết
- Thiết lập bảo vệ branch phù hợp (chỉ triển khai từ main/master)
- Giám sát hiệu năng build và tối ưu các bước chậm

Yêu cầu kỹ thuật

Yêu cầu hệ thống

  • Ký ức: 1024MB RAM

Đánh giá bài viết này

★★★★★
-
Đang tải...

Bạn đã sẵn sàng triển khai ứng dụng của mình chưa? Máy bay không người lái?

Bắt đầu trong vài phút với quá trình triển khai VPS đơn giản của chúng tôi

Được triển khai trong khoảng 2 phút • Truy cập root đầy đủ • Hỗ trợ bằng vé và email