test ci/cd
Build and Push to Harbor / build-deploy (push) Waiting to run Details

This commit is contained in:
xinyu 2026-03-22 17:27:24 +08:00
parent 67f42dad43
commit 410bd2515c
2 changed files with 95 additions and 14 deletions

View File

@ -0,0 +1,65 @@
name: Build and Push to Harbor
# 触发条件:推送到 main 分支时执行
on:
push:
branches: [ main ]
# 定义作业
jobs:
build-deploy:
# 使用 Ubuntu 环境Gitea Actions 兼容 GitHub Actions 运行器)
runs-on: ubuntu-latest
steps:
# 步骤1拉取代码到运行器
- name: Checkout code
uses: actions/checkout@v4
# 步骤2设置 Go 环境(适配 Go 项目编译)
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.26' # 替换为你的 Go 版本
cache: true # 缓存 go mod 依赖,提升编译速度
# 步骤3编译 Go 二进制文件(无 CGO适配 Alpine 镜像)
- name: Build Go binary
run: |
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o heritage ./main.go
# 验证编译结果
chmod +x heritage
./heritage -version || true
# 步骤4登录 Harbor 私有仓库
- name: Login to Harbor Registry
uses: docker/login-action@v3
with:
registry: ${{ secrets.HARBOR_REGISTRY }}
username: ${{ secrets.HARBOR_USERNAME }}
password: ${{ secrets.HARBOR_PASSWORD }}
# 步骤5构建 Docker 镜像(使用优化后的多阶段 Dockerfile
- name: Build Docker image
run: |
# 定义镜像标签(使用提交哈希作为版本,避免覆盖)
IMAGE_TAG=${{ secrets.HARBOR_REPO }}:${{ github.sha }}
IMAGE_LATEST=${{ secrets.HARBOR_REPO }}:latest
# 构建镜像
docker build -t $IMAGE_TAG -t $IMAGE_LATEST .
# 验证镜像
docker images | grep heritage
# 步骤6推送镜像到 Harbor
- name: Push to Harbor
run: |
IMAGE_TAG=${{ secrets.HARBOR_REPO }}:${{ github.sha }}
IMAGE_LATEST=${{ secrets.HARBOR_REPO }}:latest
# 推送两个标签(版本标签 + latest 标签)
docker push $IMAGE_TAG
docker push $IMAGE_LATEST
# 可选步骤:清理本地镜像(节省运行器资源)
- name: Cleanup
if: always()
run: |
docker rmi ${{ secrets.HARBOR_REPO }}:${{ github.sha }} || true
docker rmi ${{ secrets.HARBOR_REPO }}:latest || true

View File

@ -1,23 +1,39 @@
# 阶段1编译 Go 二进制
FROM golang:1.22-alpine AS builder
ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64
WORKDIR /build
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -ldflags="-s -w" -o heritage ./main.go
# 阶段2运行镜像使用你的私有 Alpine 镜像)
FROM harbor.ks.easyj.top/zt/alpine:0.1
# 安装必要依赖(时区、健康检查)
RUN apk add --no-cache tzdata curl
# 创建非 root 用户
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
# 环境变量(仅保留非敏感默认值,敏感值运行时传入)
ENV APP_DIR=/app \
MYSQL_DSN=root:SG1231@tcp(mysql:3306)/heritage?charset=utf8mb4&parseTime=True&loc=Local \
MYSQL_MAXLIFETIME=1 \
MYSQL_MAXIDLECONNS=2 \
MYSQL_MAXOPENCONNS=50 \
MYSQL_INIT=true \
GIN_MODE=release \
REDIS_DSN=redis:6379 \
REDIS_DB=1 \
REDIS_PWD=eYVX7EwVmmxKPCDmwMtyKVge8oLd2t81 \
LOGLEVEL=debug
COPY heritage ${APP_DIR}/heritage
MYSQL_MAXLIFETIME=1 \
MYSQL_MAXIDLECONNS=2 \
MYSQL_MAXOPENCONNS=50 \
MYSQL_INIT=true \
GIN_MODE=release \
REDIS_DB=1 \
LOGLEVEL=debug \
TZ=Asia/Shanghai
WORKDIR ${APP_DIR}
# 从编译阶段复制二进制文件
COPY --from=builder /build/heritage ${APP_DIR}/
RUN chmod +x heritage && chown -R appuser:appgroup ${APP_DIR}
RUN chmod +x heritage
# 暴露端口 + 健康检查
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD curl -f http://localhost:8080/health || exit 1
# 切换非 root 用户运行
USER appuser
CMD ["./heritage"]