feat(docker): 将 APP_VERSION 构建进镜像,解决 Watchtower 更新后版本号显示不准的问题 #200

Merged
xiaoxia merged 3 commits from feat/app-version-in-dockerfile into develop 2026-07-10 22:07:25 +08:00
Owner

问题

当前 APP_VERSION 是容器启动时注入的环境变量,Watchtower 更新镜像时会保留旧容器的环境变量配置,导致版本号一直显示旧值(虽然代码实际已更新)。

解决方案

将 APP_VERSION 通过 --build-arg 在构建时传入,并用 ENV 固化到镜像中。这样 Watchtower 更新镜像后,版本号自动同步为新镜像的构建版本。

修改内容

  1. infra/docker/api.Dockerfile — 添加 ARG APP_VERSION=devENV APP_VERSION=$APP_VERSION
  2. infra/docker/worker.Dockerfile — 同上
  3. scripts/build_release_images.sh — API 和 Worker 的 docker build / buildx build 命令添加 --build-arg APP_VERSION=$VERSION

影响

  • 默认值为 dev,本地构建不受影响
  • CI 构建时自动传入 commit SHA 作为版本号
  • Watchtower 更新后版本号自动同步,无需手动修改环境变量
  • health 端点返回的 version 与镜像构建版本一致
## 问题 当前 APP_VERSION 是容器启动时注入的环境变量,Watchtower 更新镜像时会保留旧容器的环境变量配置,导致版本号一直显示旧值(虽然代码实际已更新)。 ## 解决方案 将 APP_VERSION 通过 --build-arg 在构建时传入,并用 ENV 固化到镜像中。这样 Watchtower 更新镜像后,版本号自动同步为新镜像的构建版本。 ## 修改内容 1. **infra/docker/api.Dockerfile** — 添加 `ARG APP_VERSION=dev` 和 `ENV APP_VERSION=$APP_VERSION` 2. **infra/docker/worker.Dockerfile** — 同上 3. **scripts/build_release_images.sh** — API 和 Worker 的 docker build / buildx build 命令添加 `--build-arg APP_VERSION=$VERSION` ## 影响 - 默认值为 `dev`,本地构建不受影响 - CI 构建时自动传入 commit SHA 作为版本号 - Watchtower 更新后版本号自动同步,无需手动修改环境变量 - health 端点返回的 version 与镜像构建版本一致
xiaoxia added 3 commits 2026-07-09 18:13:43 +08:00
feat(ci): 构建镜像时传入 APP_VERSION build-arg,将版本号内建到镜像中
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 51s
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Successful in 1m10s
CI/CD Pipeline / Build & Push Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Build Production Runtime Images (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
f5266f85e0
xiaoxia merged commit 4ab641f765 into develop 2026-07-10 22:07:25 +08:00
Sign in to join this conversation.