From 9a747596e658ceab192288cad02db849fdc6c1a0 Mon Sep 17 00:00:00 2001 From: Chang lue Tsen Date: Fri, 25 Apr 2025 18:30:45 +0900 Subject: [PATCH] fix: add versioning support to Docker build process --- .github/workflows/release.yml | 4 ++++ Dockerfile | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 77c4e9b..0480620 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -48,6 +48,8 @@ jobs: file: Dockerfile platforms: linux/amd64,linux/arm64 push: true + build-args: | + VERSION=${{ env.VERSION }} tags: | ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:latest ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ env.VERSION }}-${{ env.GIT_SHA }} @@ -60,6 +62,8 @@ jobs: file: Dockerfile platforms: linux/amd64,linux/arm64 push: true + build-args: | + VERSION=${{ env.VERSION }} tags: | ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:beta ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ env.VERSION }}-${{ env.GIT_SHA }} diff --git a/Dockerfile b/Dockerfile index 6aabce2..118076e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,7 @@ FROM golang:alpine AS builder LABEL stage=gobuilder ARG TARGETARCH +ARG VERSION ENV CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} # Combine apk commands into one to reduce layer size @@ -18,8 +19,9 @@ RUN go mod download # Copy the rest of the application code COPY . . -# Build the binary with optimization flags to reduce binary size -RUN go build -ldflags="-s -w" -o /app/ppanel ppanel.go +# Build the binary with version and build time +RUN BUILD_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") && \ + go build -ldflags="-s -w -X 'github.com/perfect-panel/server/pkg/constant.Version=${VERSION}' -X 'github.com/perfect-panel/server/pkg/constant.BuildTime=${BUILD_TIME}'" -o /app/ppanel ppanel.go # Final minimal image FROM scratch @@ -34,11 +36,11 @@ ENV TZ=Asia/Shanghai WORKDIR /app COPY --from=builder /app/ppanel /app/ppanel -COPY --from=builder /etc /app/etc +COPY --from=builder /build/etc /app/etc # Expose the port (optional) EXPOSE 8080 # Specify entry point ENTRYPOINT ["/app/ppanel"] -CMD ["run", "--config", "etc/ppanel.yaml"] +CMD ["run", "--config", "etc/ppanel.yaml"] \ No newline at end of file