Files
gerrit/install_drone_ci.sh
2025-12-22 17:12:39 +08:00

242 lines
7.1 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Drone CI 安装脚本(轻量级,适合个人小团队)
# 支持 CentOS/RHEL 系统
set -e
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 配置变量
GERRIT_HOST="${GERRIT_HOST:-101.43.95.130}"
GERRIT_PORT="${GERRIT_PORT:-8080}"
DRONE_PORT="${DRONE_PORT:-3000}"
DRONE_DATA_DIR="/opt/drone"
DRONE_RPC_SECRET=""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Drone CI 自动化安装脚本${NC}"
echo -e "${GREEN}(轻量级,适合个人小团队)${NC}"
echo -e "${GREEN}========================================${NC}"
# 检查 Docker
check_docker() {
echo -e "${GREEN}检查 Docker 环境...${NC}"
if ! command -v docker &> /dev/null; then
echo -e "${YELLOW}未检测到 Docker正在安装...${NC}"
# 安装 Docker
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
fi
# 检查 Docker 服务状态
if ! sudo systemctl is-active --quiet docker; then
echo -e "${YELLOW}启动 Docker 服务...${NC}"
sudo systemctl start docker
fi
echo -e "${GREEN}Docker 版本: $(docker --version)${NC}"
}
# 生成 RPC Secret
generate_rpc_secret() {
echo -e "${GREEN}生成 RPC Secret...${NC}"
if command -v openssl &> /dev/null; then
DRONE_RPC_SECRET=$(openssl rand -hex 16)
else
DRONE_RPC_SECRET=$(date +%s | sha256sum | base64 | head -c 32)
fi
echo -e "${GREEN}RPC Secret 已生成${NC}"
}
# 创建数据目录
create_directories() {
echo -e "${GREEN}创建数据目录...${NC}"
sudo mkdir -p ${DRONE_DATA_DIR}
sudo chmod 755 ${DRONE_DATA_DIR}
echo -e "${GREEN}目录创建完成: ${DRONE_DATA_DIR}${NC}"
}
# 安装 Drone Server
install_drone_server() {
echo -e "${GREEN}安装 Drone Server...${NC}"
# 停止并删除旧容器(如果存在)
sudo docker stop drone 2>/dev/null || true
sudo docker rm drone 2>/dev/null || true
# 运行 Drone Server
sudo docker run -d \
--name=drone \
--restart=always \
--volume=${DRONE_DATA_DIR}:/data \
--env=DRONE_GERRIT_SERVER=http://${GERRIT_HOST}:${GERRIT_PORT} \
--env=DRONE_GERRIT_USERNAME=admin \
--env=DRONE_GERRIT_PASSWORD="" \
--env=DRONE_RPC_SECRET=${DRONE_RPC_SECRET} \
--env=DRONE_SERVER_HOST=${GERRIT_HOST}:${DRONE_PORT} \
--env=DRONE_SERVER_PROTO=http \
--env=DRONE_USER_CREATE=username:admin,admin:true \
--publish=${DRONE_PORT}:80 \
drone/drone:latest
echo -e "${GREEN}Drone Server 安装完成${NC}"
}
# 安装 Drone Runner
install_drone_runner() {
echo -e "${GREEN}安装 Drone Runner...${NC}"
# 停止并删除旧容器(如果存在)
sudo docker stop drone-runner 2>/dev/null || true
sudo docker rm drone-runner 2>/dev/null || true
# 运行 Drone Runner
sudo docker run -d \
--name=drone-runner \
--restart=always \
--volume=/var/run/docker.sock:/var/run/docker.sock \
--env=DRONE_RPC_PROTO=http \
--env=DRONE_RPC_HOST=${GERRIT_HOST}:${DRONE_PORT} \
--env=DRONE_RPC_SECRET=${DRONE_RPC_SECRET} \
--env=DRONE_RUNNER_CAPACITY=2 \
--env=DRONE_RUNNER_NAME=runner-1 \
drone/drone-runner-docker:latest
echo -e "${GREEN}Drone Runner 安装完成${NC}"
}
# 检查防火墙
check_firewall() {
echo -e "${YELLOW}检查防火墙配置...${NC}"
if command -v firewall-cmd &> /dev/null; then
if sudo firewall-cmd --list-ports | grep -q "${DRONE_PORT}"; then
echo -e "${GREEN}防火墙端口 ${DRONE_PORT} 已开放${NC}"
else
echo -e "${YELLOW}需要开放防火墙端口 ${DRONE_PORT}${NC}"
echo -e "${YELLOW}执行: sudo firewall-cmd --permanent --add-port=${DRONE_PORT}/tcp && sudo firewall-cmd --reload${NC}"
fi
else
echo -e "${YELLOW}未检测到 firewall-cmd请手动检查防火墙${NC}"
fi
}
# 创建 .drone.yml 示例
create_drone_yml_example() {
echo -e "${GREEN}创建 .drone.yml 示例...${NC}"
cat > /tmp/.drone.yml.example << 'EOF'
# Drone CI 配置文件示例
# 将此文件复制到项目根目录并命名为 .drone.yml
kind: pipeline
type: docker
name: default
steps:
- name: build
image: maven:3.8-openjdk-17
commands:
- echo "开始构建..."
- mvn clean package
- name: test
image: maven:3.8-openjdk-17
commands:
- echo "运行测试..."
- mvn test
- name: deploy
image: alpine
commands:
- echo "部署应用..."
# 在这里添加部署命令
---
# 如果是 Node.js 项目,使用以下配置:
kind: pipeline
type: docker
name: nodejs
steps:
- name: install
image: node:18
commands:
- npm install
- name: build
image: node:18
commands:
- npm run build
- name: test
image: node:18
commands:
- npm test
EOF
echo -e "${GREEN}示例文件已保存到: /tmp/.drone.yml.example${NC}"
}
# 输出访问信息
print_info() {
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Drone CI 安装完成!${NC}"
echo -e "${GREEN}========================================${NC}"
echo -e "${YELLOW}访问地址:${NC}"
echo -e " Drone CI Web 界面: http://${GERRIT_HOST}:${DRONE_PORT}"
echo -e " Gerrit Web 界面: http://${GERRIT_HOST}:${GERRIT_PORT}"
echo -e ""
echo -e "${YELLOW}常用命令:${NC}"
echo -e " 查看 Server 状态: sudo docker ps | grep drone"
echo -e " 查看 Server 日志: sudo docker logs -f drone"
echo -e " 查看 Runner 日志: sudo docker logs -f drone-runner"
echo -e " 重启 Server: sudo docker restart drone"
echo -e " 重启 Runner: sudo docker restart drone-runner"
echo -e ""
echo -e "${YELLOW}数据目录:${NC}"
echo -e " ${DRONE_DATA_DIR}"
echo -e ""
echo -e "${YELLOW}下一步:${NC}"
echo -e " 1. 访问 http://${GERRIT_HOST}:${DRONE_PORT}"
echo -e " 2. 使用 admin 账号登录(首次登录会自动创建)"
echo -e " 3. 在项目根目录创建 .drone.yml 配置文件"
echo -e " 4. 参考示例: cat /tmp/.drone.yml.example"
echo -e " 5. 在 Gerrit 项目设置中配置 Webhook:"
echo -e " URL: http://${GERRIT_HOST}:${DRONE_PORT}/hook"
echo -e " 事件: patchset-created, change-merged"
echo -e ""
echo -e "${YELLOW}配置 Gerrit Webhook:${NC}"
echo -e " 1. 访问 Gerrit: http://${GERRIT_HOST}:${GERRIT_PORT}"
echo -e " 2. 进入项目设置 → Webhooks"
echo -e " 3. 添加 Webhook:"
echo -e " URL: http://${GERRIT_HOST}:${DRONE_PORT}/hook"
echo -e " 事件: patchset-created, change-merged"
echo -e "${GREEN}========================================${NC}"
}
# 主函数
main() {
check_docker
generate_rpc_secret
create_directories
install_drone_server
sleep 5 # 等待 Server 启动
install_drone_runner
check_firewall
create_drone_yml_example
print_info
}
# 执行主函数
main