添加 Docker 和 CI/CD 配置(无 SSH 方案)
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Failing after 0s

This commit is contained in:
2026-01-19 14:56:40 +08:00
parent a6940e78df
commit 589197e284
10 changed files with 1000 additions and 42 deletions

69
Dockerfile.api Normal file
View File

@@ -0,0 +1,69 @@
# ================================
# 阶段 1: 构建阶段
# ================================
FROM golang:1.23.4-alpine AS builder
# 设置工作目录
WORKDIR /build
# 安装必要的构建工具
RUN apk add --no-cache git ca-certificates tzdata
# 复制 go.mod 和 go.sum利用 Docker 缓存)
COPY go.mod go.sum ./
RUN go mod download
# 复制源代码
COPY . .
# 编译 API 服务(静态链接)
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags="-w -s" \
-o /build/api \
cmd/api/main.go
# 下载 golang-migrate 工具
RUN go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
# ================================
# 阶段 2: 运行阶段
# ================================
FROM alpine:latest
# 安装运行时依赖
RUN apk add --no-cache ca-certificates tzdata bash curl
# 设置时区
ENV TZ=Asia/Shanghai
# 创建非 root 用户
RUN addgroup -g 1000 appuser && \
adduser -D -u 1000 -G appuser appuser
# 设置工作目录
WORKDIR /app
# 从构建阶段复制二进制文件
COPY --from=builder /build/api /app/api
COPY --from=builder /go/bin/migrate /usr/local/bin/migrate
# 复制配置文件和迁移文件
COPY configs /app/configs
COPY migrations /app/migrations
# 复制启动脚本
COPY docker/entrypoint-api.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh
# 切换到非 root 用户
USER appuser
# 暴露端口
EXPOSE 8088
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD curl -f http://localhost:8088/health || exit 1
# 启动命令
ENTRYPOINT ["/app/entrypoint.sh"]