Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions backend/scripts/resolve-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,6 @@ set -eu

SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
BACKEND_DIR="$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)"
REPO_DIR="$(CDPATH= cd -- "$BACKEND_DIR/.." && pwd)"
VERSION_FILE="$BACKEND_DIR/cmd/server/VERSION"

# Prefer the exact release tag when building from a tagged checkout so
# source builds from vX.Y.Z don't inherit the previous VERSION file value.
if command -v git >/dev/null 2>&1; then
TAG="$(
git -C "$REPO_DIR" describe --tags --exact-match --match 'v[0-9]*' 2>/dev/null || \
git -C "$REPO_DIR" describe --tags --exact-match --match '[0-9]*' 2>/dev/null || \
true
)"
if [ -n "$TAG" ]; then
printf '%s\n' "${TAG#v}"
exit 0
fi
fi

printf '%s\n' "$(tr -d '\r\n' < "$VERSION_FILE")"
8 changes: 4 additions & 4 deletions deploy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ FROM ${GOLANG_IMAGE} AS backend-builder
ARG VERSION=
ARG COMMIT=docker
ARG DATE
ARG GOPROXY
ARG GOSUMDB
ARG GOPROXY=https://goproxy.cn,direct
ARG GOSUMDB=sum.golang.google.cn

ENV GOPROXY=${GOPROXY}
ENV GOSUMDB=${GOSUMDB}
Expand All @@ -68,9 +68,9 @@ COPY backend/ ./
COPY --from=frontend-builder /app/backend/internal/web/dist ./internal/web/dist

# Build the binary (BuildType=release for CI builds, embed frontend)
# Version precedence: build arg VERSION > exact git tag > cmd/server/VERSION
# Version precedence: build arg VERSION > cmd/server/VERSION
RUN VERSION_VALUE="${VERSION}" && \
if [ -z "${VERSION_VALUE}" ]; then VERSION_VALUE="$(./scripts/resolve-version.sh)"; fi && \
if [ -z "${VERSION_VALUE}" ]; then VERSION_VALUE="$(tr -d '\r\n' < ./cmd/server/VERSION)"; fi && \
DATE_VALUE="${DATE:-$(date -u +%Y-%m-%dT%H:%M:%SZ)}" && \
CGO_ENABLED=0 GOOS=linux go build \
-tags embed \
Expand Down
8 changes: 8 additions & 0 deletions deploy/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ services:
# ===========================================================================
sub2api:
image: ${SUB2API_IMAGE:-weishaw/sub2api:latest}
build:
context: .
dockerfile: deploy/Dockerfile
args:
VERSION: ${SOURCE_COMMIT:-}
COMMIT: ${SOURCE_COMMIT:-docker}
GOPROXY: ${GOPROXY:-https://goproxy.cn,direct}
GOSUMDB: ${GOSUMDB:-sum.golang.google.cn}
restart: unless-stopped
ulimits:
nofile:
Expand Down
16 changes: 16 additions & 0 deletions tools/test_docker_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ def test_compose_image_can_be_overridden(self):
if "image: weishaw/sub2api" in text:
self.assertIn("${SUB2API_IMAGE:-", text, path)

def test_production_compose_builds_from_local_source(self):
with (DEPLOY / "docker-compose.yml").open("r", encoding="utf-8") as handle:
compose = yaml.safe_load(handle)
service = compose["services"]["sub2api"]
self.assertIn("build", service)
self.assertEqual(service["build"]["context"], ".")
self.assertEqual(service["build"]["dockerfile"], "deploy/Dockerfile")
self.assertEqual(service["build"]["args"]["COMMIT"], "${SOURCE_COMMIT:-docker}")
self.assertEqual(service["build"]["args"]["GOPROXY"], "${GOPROXY:-https://goproxy.cn,direct}")
self.assertEqual(service["build"]["args"]["GOSUMDB"], "${GOSUMDB:-sum.golang.google.cn}")

def test_release_dockerfile_version_fallback_uses_version_file(self):
text = (DEPLOY / "Dockerfile").read_text(encoding="utf-8")
self.assertNotIn("./scripts/resolve-version.sh", text)
self.assertIn("cmd/server/VERSION", text)

def test_compose_files_do_not_define_custom_networks(self):
for path in COMPOSE_FILES:
with path.open("r", encoding="utf-8") as handle:
Expand Down