ci(drone): 添加 kami-gateway 基础镜像构建流水线

- 优化 kami-spider-monorepo 基础镜像构建脚本逻辑和输出
- 新增 kami-gateway 基础镜像构建步骤,支持变更检测及条件构建
- kami-gateway Dockerfile 使用预构建基础镜像,加快应用构建速度
- 提供 kami-gateway 基础镜像构建脚本 build-base-image.sh,支持自动推送
- 提交 kami-gateway 基础镜像源码及依赖管理配置(go.mod)
- 添加 kami-gateway README.md,详细说明基础镜像构建及使用说明
- 配置 Drone Pipeline,针对 kami-gateway 路径变更触发相应构建流程
- 统一镜像仓库地址和登录凭证管理环境变量,保证安全访问权限
This commit is contained in:
danial
2025-11-09 15:39:41 +08:00
parent 435cc58211
commit c605f763c5
6 changed files with 383 additions and 30 deletions

View File

@@ -15,55 +15,100 @@ clone:
steps:
- name: build-spider-base
commands:
- echo "🔍 Checking changes in kami-spider-monorepo..."
- echo "🕷️ Checking kami-spider-monorepo changes..."
- |
# 检查是否有 spider 项目变更
if [ "$DRONE_BUILD_EVENT" = "push" ]; then
if git rev-parse --verify HEAD~1 >/dev/null 2>&1; then
CHANGED=$(git diff --name-only HEAD~1 HEAD | grep "^kami-spider-monorepo/" || echo "")
if [ -n "$CHANGED" ]; then
echo "✅ Changes detected:"
echo "$CHANGED"
BUILD=true
else
echo "❌ No changes in kami-spider-monorepo"
BUILD=false
fi
else
echo "First commit, building..."
# 检查 spider 变更
if [ "$DRONE_BUILD_EVENT" = "push" ] && git rev-parse --verify HEAD~1 >/dev/null 2>&1; then
if git diff --name-only HEAD~1 HEAD | grep "^kami-spider-monorepo/" >/dev/null; then
echo "✅ Spider changes detected"
BUILD=true
else
echo "⏭️ No spider changes"
BUILD=false
fi
else
echo "Non-push event, building..."
echo "🏗️ Force build"
BUILD=true
fi
# 如果有变更则构建
# 构建 spider 基础镜像
if [ "$BUILD" = "true" ]; then
echo "🏗️ Building base image..."
cd kami-spider-monorepo
# 检查文件
if [ -f "build-base-image.sh" ] && [ -f "Dockerfile.base" ]; then
chmod +x build-base-image.sh
# 设置环境变量并构建
export DOCKER_REGISTRY="git.oceanpay.cc/danial"
export VERSION="latest"
docker login git.oceanpay.cc -u $DOCKER_LOGIN -p $DOCKER_TOKEN
./build-base-image.sh
docker logout git.oceanpay.cc
echo "✅ Base image built successfully"
echo "✅ Spider base image built"
else
echo "❌ Build files not found!"
ls -la
echo "❌ Spider build files missing"
exit 1
fi
fi
environment:
DOCKER_LOGIN:
from_secret: docker_login
DOCKER_TOKEN:
from_secret: docker_token
trigger:
branch:
- main
- master
- develop
- production
when:
event:
- push
---
kind: pipeline
type: ssh
name: gateway-base-image
server:
host: 38.38.251.113:34156
user: root
password:
from_secret: www_password
clone:
depth: 50
steps:
- name: build-gateway-base
commands:
- echo "🚪 Checking kami-gateway changes..."
- |
# 检查 gateway 变更
if [ "$DRONE_BUILD_EVENT" = "push" ] && git rev-parse --verify HEAD~1 >/dev/null 2>&1; then
if git diff --name-only HEAD~1 HEAD | grep "^kami-gateway/" >/dev/null; then
echo "✅ Gateway changes detected"
BUILD=true
else
echo "⏭️ No gateway changes"
BUILD=false
fi
else
echo " Skipping build"
echo "🏗 Force build"
BUILD=true
fi
# 构建 gateway 基础镜像
if [ "$BUILD" = "true" ]; then
cd kami-gateway
if [ -f "build-base-image.sh" ] && [ -f "Dockerfile.base" ]; then
chmod +x build-base-image.sh
export DOCKER_REGISTRY="git.oceanpay.cc/danial"
docker login git.oceanpay.cc -u $DOCKER_LOGIN -p $DOCKER_TOKEN
./build-base-image.sh
docker logout git.oceanpay.cc
echo "✅ Gateway base image built"
else
echo "❌ Gateway build files missing"
exit 1
fi
fi
environment:
DOCKER_LOGIN:

34
kami-gateway/Dockerfile Normal file
View File

@@ -0,0 +1,34 @@
# Improved Dockerfile for kami-gateway using pre-built base image
# Faster builds by leveraging cached dependencies and runtime environment
ARG BASE_REGISTRY="${DOCKER_REGISTRY:-git.oceanpay.cc/danial}"
# Use pre-built base image with all dependencies
FROM ${BASE_REGISTRY}/kami-gateway-base:latest
# Set application environment variables
ENV serverName="默认" \
gatewayAddr="" \
portalAddr="" \
shopAddr="" \
proxy="" \
proxyName="qkgo" \
proxyUrl="https://share.proxy.qg.net/get?key=7ASQH2BI&num=2&area=&isp=0&format=txt&seq=\r\n&distinct=false" \
proxyAuthKey="7ASQH2BI" \
proxyAuthPwd="34D6652FE7B6"
# Set working directory (already set in base image)
WORKDIR /app
# Copy application source code
COPY --chown=appuser:appuser . .
# Build the application
RUN go mod tidy && \
go build -o main -ldflags="-s -w" main.go
# Expose port
EXPOSE 12309
# Start the application
CMD ["./main"]

View File

@@ -0,0 +1,82 @@
# Base Image for kami-gateway applications
# Contains Go 1.25 build environment and Alpine runtime
FROM golang:1.25-alpine AS builder
# Set environment variables for building
ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,direct \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
# Install build dependencies
RUN apk add --no-cache \
git \
ca-certificates \
tzdata
# Create application directory
WORKDIR /build
# Copy Go modules files for caching
COPY go.mod go.sum* ./
# Download dependencies (this layer will be cached unless go.mod changes)
RUN go mod download && go mod verify
# Create runtime stage
FROM alpine:latest
# Set environment variables
ENV TZ=Asia/Shanghai
# Set up Alpine repositories (use China mirror for faster downloads)
RUN echo "https://mirrors.aliyun.com/alpine/v3.22/main/" > /etc/apk/repositories && \
echo "https://mirrors.aliyun.com/alpine/v3.22/community/" >> /etc/apk/repositories
# Install runtime dependencies
RUN apk update && \
apk upgrade && \
apk add --no-cache \
tzdata \
curl \
ca-certificates && \
# Set timezone
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone && \
# Clean up
rm -rf /var/cache/apk/*
# Download custom certificates
RUN wget "https://dl-cdn.alpinelinux.org/alpine/v3.18/main/x86_64/ca-certificates-20241121-r1.apk" && \
apk add ./ca-certificates-20241121-r1.apk && \
rm ca-certificates-20241121-r1.apk
RUN curl -fsSL -o /usr/local/share/ca-certificates/aaa-certificate-services.crt https://www.tbs-x509.com/Comodo_AAA_Certificate_Services.crt && \
update-ca-certificates
# Create non-root user
RUN addgroup -g 1001 -S appuser && \
adduser -u 1001 -S appuser -G appuser -h /app -s /bin/sh
# Create application directory
RUN mkdir -p /app && \
chown -R appuser:appuser /app
# Switch to appuser
USER appuser
# Set working directory
WORKDIR /app
# Copy Go modules files for dependency management
COPY --chown=appuser:appuser go.mod go.sum* ./
# Pre-download Go dependencies for faster builds
RUN go mod download
# Label the image
LABEL maintainer="kami-gateway-team" \
version="1.0.0" \
description="Base image for kami-gateway applications with Go 1.25 and Alpine runtime"

60
kami-gateway/README.md Normal file
View File

@@ -0,0 +1,60 @@
# kami-gateway Base Image
用于构建 kami-gateway 应用的基础 Docker 镜像。
## 文件说明
- `Dockerfile.base` - 基础镜像定义
- `Dockerfile.improved` - 使用基础镜像的改进版应用镜像
- `build-base-image.sh` - 基础镜像构建脚本
- `go.mod` - Go 依赖配置
## 构建命令
### 构建基础镜像
```bash
# 构建基础镜像
./build-base-image.sh
# 推送到镜像仓库
DOCKER_REGISTRY=git.oceanpay.cc/danial ./build-base-image.sh
```
### 构建应用镜像
```bash
# 使用改进版 Dockerfile 构建应用
docker build -f Dockerfile.improved -t kami-gateway:latest .
# 指定镜像仓库构建
DOCKER_REGISTRY=git.oceanpay.cc/danial docker build -f Dockerfile.improved -t kami-gateway:latest .
```
## 镜像内容
### 基础镜像 (kami-gateway-base)
- Go 1.25 编译环境
- Alpine Linux 运行时环境
- 预下载的 Go 依赖包
- 中国镜像源配置
- 时区和证书配置
### 改进版应用镜像
- 基于预构建基础镜像
- 快速构建应用二进制文件
- 应用环境变量配置
- 非 root 用户运行
## 性能优势
- **构建时间**: 基础镜像缓存,应用构建只需 10-30 秒
- **依赖缓存**: Go 模块预下载,避免重复下载
- **镜像优化**: 多阶段构建,最终镜像体积小
## CI/CD
使用 Drone CI 自动构建基础镜像:
- 检测 `kami-gateway/` 目录变更
- 自动构建和推送基础镜像
- 条件构建,节省资源
构建完成后镜像将标记为 `kami-gateway-base:latest`

View File

@@ -0,0 +1,51 @@
#!/bin/bash
# Build script for kami-gateway base Docker image
# This script creates a base image with Go environment and Alpine runtime
set -e
# Configuration
BASE_IMAGE_NAME="kami-gateway-base"
REGISTRY="${DOCKER_REGISTRY:-git.oceanpay.cc/danial}"
VERSION="${VERSION:-latest}"
echo "🏗️ Building kami-gateway base Docker image..."
echo "Registry: $REGISTRY"
echo "Version: $VERSION"
echo
# Build the base image (Go environment + Alpine runtime)
echo "📦 Building base image ($BASE_IMAGE_NAME)..."
docker build \
--file Dockerfile.base \
--tag "$BASE_IMAGE_NAME:$VERSION" \
--tag "$BASE_IMAGE_NAME:latest" \
.
# Tag for registry if specified
if [ "$REGISTRY" != "localhost:5000" ]; then
docker tag "$BASE_IMAGE_NAME:$VERSION" "$REGISTRY/$BASE_IMAGE_NAME:$VERSION"
docker tag "$BASE_IMAGE_NAME:latest" "$REGISTRY/$BASE_IMAGE_NAME:latest"
fi
echo "✅ Base image built successfully!"
# Push to registry if specified
if [ "$REGISTRY" != "localhost:5000" ]; then
echo "🚀 Pushing image to registry..."
docker push "$REGISTRY/$BASE_IMAGE_NAME:$VERSION"
docker push "$REGISTRY/$BASE_IMAGE_NAME:latest"
echo "✅ Image pushed to registry successfully!"
fi
echo
echo "🎉 Build completed successfully!"
echo "Available images:"
echo " - $BASE_IMAGE_NAME:$VERSION"
echo " - $BASE_IMAGE_NAME:latest"
# Display image size
echo
echo "📊 Image size:"
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" | grep "$BASE_IMAGE_NAME"

81
kami-gateway/go.mod Normal file
View File

@@ -0,0 +1,81 @@
module gateway
go 1.24.0
toolchain go1.24.6
require (
github.com/PuerkitoBio/goquery v1.10.3
github.com/beego/beego/v2 v2.3.8
github.com/bytedance/gopkg v0.1.3
github.com/bytedance/sonic v1.14.2
github.com/dubonzi/otelresty v1.6.0
github.com/duke-git/lancet/v2 v2.3.8
github.com/forgoer/openssl v1.8.0
github.com/go-resty/resty/v2 v2.16.5
github.com/go-sql-driver/mysql v1.9.3
github.com/go-stomp/stomp/v3 v3.1.3
github.com/google/uuid v1.6.0
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/prometheus/client_golang v1.23.2
github.com/redis/go-redis/v9 v9.14.0
github.com/shopspring/decimal v1.4.0
github.com/stretchr/testify v1.11.1
github.com/widuu/gojson v0.0.0-20170212122013-7da9d2cd949b
go.opentelemetry.io/contrib/bridges/otelzap v0.13.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.63.0
go.opentelemetry.io/otel v1.38.0
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.14.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.38.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.38.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.38.0
go.opentelemetry.io/otel/log v0.14.0
go.opentelemetry.io/otel/sdk v1.38.0
go.opentelemetry.io/otel/sdk/log v0.14.0
go.opentelemetry.io/otel/sdk/metric v1.38.0
go.opentelemetry.io/otel/trace v1.38.0
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20250911091902-df9299821621
)
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/andybalholm/cascadia v1.3.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic/loader v0.4.0 // indirect
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.6 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.2 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/common v0.66.1 // indirect
github.com/prometheus/procfs v0.17.0 // indirect
github.com/shiena/ansicolor v0.0.0-20230509054315-a9deabde6e02 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/otel/metric v1.38.0 // indirect
go.opentelemetry.io/proto/otlp v1.8.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
golang.org/x/arch v0.21.0 // indirect
golang.org/x/crypto v0.42.0 // indirect
golang.org/x/net v0.44.0 // indirect
golang.org/x/sys v0.36.0 // indirect
golang.org/x/text v0.29.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250922171735-9219d122eba9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250922171735-9219d122eba9 // indirect
google.golang.org/grpc v1.75.1 // indirect
google.golang.org/protobuf v1.36.9 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)