test ci/cd
Build and Push to Harbor / build-deploy (push) Waiting to run
Details
Build and Push to Harbor / build-deploy (push) Waiting to run
Details
This commit is contained in:
parent
67f42dad43
commit
410bd2515c
|
|
@ -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
|
||||
44
Dockerfile
44
Dockerfile
|
|
@ -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"]
|
||||
Loading…
Reference in New Issue