From d088ef2d75cfc03a661c7de748bc4b99c36f2763 Mon Sep 17 00:00:00 2001 From: robinj0726 <88188467+robinj0726@users.noreply.github.com> Date: Thu, 10 Sep 2026 11:01:09 +0800 Subject: [PATCH] feat(docker): add Dockerfile and CI workflow for building and deploying ids-svc-product --- .dockerignore | 14 +++++++ .gitea/workflows/ci.yml | 84 +++++++++++++++++++++++++++++++++++++++++ Dockerfile | 33 ++++++++++++++++ README.md | 23 +++++++++++ 4 files changed, 154 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitea/workflows/ci.yml create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..254929b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,14 @@ +.git +**/.git +**/.DS_Store +**/.env +**/.env.* +!**/.env.example + +ids-svc-product/docs +ids-svc-product/.gitea +ids-svc-product/docker-compose.yaml +ids-svc-product/**/*_test.go + +ids-proto/docs +ids-proto/.git diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..d81d0b4 --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,84 @@ +name: Build & Deploy + +on: + push: + branches: + - multi-tenant + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + env: + IMAGE: ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product + IMAGE_TAG: ${{ gitea.sha }} + + steps: + - name: Checkout Product + uses: actions/checkout@v4 + with: + path: ids-svc-product + + - name: Checkout Proto + uses: actions/checkout@v4 + with: + repository: ids-ecommerce/ids-proto + ref: multi-tenant + path: ids-proto + token: ${{ secrets.REGISTRY_TOKEN }} + + - name: Prepare Docker ignorefile + run: cp ids-svc-product/.dockerignore .dockerignore + + - name: Login Registry + run: | + echo "${{ secrets.REGISTRY_TOKEN }}" | \ + docker login ids-dev.idsbuybuybuy.com \ + -u "${{ secrets.REGISTRY_USER }}" \ + --password-stdin + + - name: Build Image + run: | + docker build \ + -f ids-svc-product/Dockerfile \ + -t "${IMAGE}:${IMAGE_TAG}" \ + . + + - name: Push Image + run: | + docker push "${IMAGE}:${IMAGE_TAG}" + + - name: Configure SSH + run: | + mkdir -p ~/.ssh + chmod 700 ~/.ssh + + printf '%s\n' "${{ secrets.DEPLOY_SSH_KEY }}" \ + > ~/.ssh/ids_deploy + + chmod 600 ~/.ssh/ids_deploy + + ssh-keyscan -H "${{ secrets.DEPLOY_HOST }}" \ + >> ~/.ssh/known_hosts + + - name: Deploy + run: | + ssh \ + -i ~/.ssh/ids_deploy \ + "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" \ + " + set -e + + cd /opt/ids-ecommerce + + sed -i \ + 's/^IDS_SVC_PRODUCT_TAG=.*/IDS_SVC_PRODUCT_TAG=${IMAGE_TAG}/' \ + .env + + docker compose pull ids-svc-product + + docker compose up -d ids-svc-product + + docker ps \ + --filter name=ids-svc-product \ + --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}' + " diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f61f766 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,33 @@ +# Build from the parent directory that contains both ids-svc-product/ and ids-proto/: +# docker build -f ids-svc-product/Dockerfile -t ids-svc-product:local . +# +# go.mod replace points at ../ids-proto, so both trees must be present in the context. + +FROM golang:1.26.4-alpine AS builder + +RUN apk add --no-cache ca-certificates git + +WORKDIR /src + +COPY ids-proto /src/ids-proto +COPY ids-svc-product/go.mod ids-svc-product/go.sum /src/ids-svc-product/ + +WORKDIR /src/ids-svc-product +RUN go mod download + +COPY ids-svc-product/ /src/ids-svc-product/ + +RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/ids-svc-product ./cmd + +FROM alpine:3.21 + +RUN apk add --no-cache ca-certificates tzdata \ + && adduser -D -H -u 10001 appuser + +COPY --from=builder /out/ids-svc-product /usr/local/bin/ids-svc-product + +USER appuser + +EXPOSE 50052 9102 + +ENTRYPOINT ["/usr/local/bin/ids-svc-product"] diff --git a/README.md b/README.md index 1e2b911..1ae3f95 100644 --- a/README.md +++ b/README.md @@ -190,11 +190,34 @@ ids-svc-product/ ├── docs/ │ ├── ARCHITECTURE.md # 架构白皮书 │ └── DEVELOPMENT.md # 开发规约与 AI 生成指令 +├── Dockerfile # 生产镜像(需与 ids-proto 同级 context) ├── .env.example ├── go.mod └── AGENTS.md ``` +## CI / Deploy(Gitea Actions) + +`multi-tenant` 推送触发 [`.gitea/workflows/ci.yml`](.gitea/workflows/ci.yml):构建并推送镜像,再 SSH 滚动远端 compose。 + +本地/CI 构建(context 必须同时包含 `ids-svc-product/` 与 `ids-proto/`): + +```bash +# 在两仓的父目录执行 +cp ids-svc-product/.dockerignore .dockerignore +docker build -f ids-svc-product/Dockerfile \ + -t ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product: \ + . +``` + +**Gitea Secrets(建议配在组织级):** `REGISTRY_USER`、`REGISTRY_TOKEN`、`DEPLOY_SSH_KEY`、`DEPLOY_HOST`、`DEPLOY_USER`。 + +**远端 `/opt/ids-ecommerce` 前置(本仓不维护):** + +- `.env` 含 `IDS_SVC_PRODUCT_TAG=` 行(流水线用 `sed` 替换为 `${{ gitea.sha }}`) +- `docker compose` 服务名 `ids-svc-product`,镜像引用该 tag +- Runner 可 `checkout` 同实例仓库 `ids-ecommerce/ids-proto` + ## 快速开始 ### 前置依赖