chore(build): Optimize Makefile and Dockerfile.app build configuration
This commit is contained in:
@@ -181,7 +181,7 @@ build-prod:
|
|||||||
BUILD_TIME=$${BUILD_TIME:-unknown}; \
|
BUILD_TIME=$${BUILD_TIME:-unknown}; \
|
||||||
GO_VERSION=$${GO_VERSION:-unknown}; \
|
GO_VERSION=$${GO_VERSION:-unknown}; \
|
||||||
LDFLAGS="-X 'github.com/Tencent/WeKnora/internal/handler.Version=$$VERSION' -X 'github.com/Tencent/WeKnora/internal/handler.CommitID=$$COMMIT_ID' -X 'github.com/Tencent/WeKnora/internal/handler.BuildTime=$$BUILD_TIME' -X 'github.com/Tencent/WeKnora/internal/handler.GoVersion=$$GO_VERSION'"; \
|
LDFLAGS="-X 'github.com/Tencent/WeKnora/internal/handler.Version=$$VERSION' -X 'github.com/Tencent/WeKnora/internal/handler.CommitID=$$COMMIT_ID' -X 'github.com/Tencent/WeKnora/internal/handler.BuildTime=$$BUILD_TIME' -X 'github.com/Tencent/WeKnora/internal/handler.GoVersion=$$GO_VERSION'"; \
|
||||||
GOOS=linux go build -v -installsuffix cgo -ldflags="-w -s $$LDFLAGS" -o $(BINARY_NAME) $(MAIN_PATH)
|
go build -ldflags="-w -s $$LDFLAGS" -o $(BINARY_NAME) $(MAIN_PATH)
|
||||||
|
|
||||||
clean-db:
|
clean-db:
|
||||||
@echo "Cleaning database..."
|
@echo "Cleaning database..."
|
||||||
|
|||||||
+18
-23
@@ -3,10 +3,6 @@ FROM golang:1.24-alpine AS builder
|
|||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Install dependencies
|
|
||||||
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
|
|
||||||
apk add --no-cache git build-base
|
|
||||||
|
|
||||||
# 通过构建参数接收敏感信息
|
# 通过构建参数接收敏感信息
|
||||||
ARG GOPRIVATE_ARG
|
ARG GOPRIVATE_ARG
|
||||||
ARG GOPROXY_ARG
|
ARG GOPROXY_ARG
|
||||||
@@ -17,15 +13,17 @@ ENV GOPRIVATE=${GOPRIVATE_ARG}
|
|||||||
ENV GOPROXY=${GOPROXY_ARG}
|
ENV GOPROXY=${GOPROXY_ARG}
|
||||||
ENV GOSUMDB=${GOSUMDB_ARG}
|
ENV GOSUMDB=${GOSUMDB_ARG}
|
||||||
|
|
||||||
# Copy go mod and sum files
|
# Install dependencies
|
||||||
COPY go.mod go.sum ./
|
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
|
||||||
RUN go mod download
|
apk add --no-cache git build-base
|
||||||
|
|
||||||
ENV CGO_ENABLED=1
|
|
||||||
# Install migrate tool
|
# Install migrate tool
|
||||||
RUN go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
|
RUN go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
|
||||||
|
|
||||||
# Copy source code
|
# Copy go mod and sum files
|
||||||
|
COPY go.mod .
|
||||||
|
COPY go.sum .
|
||||||
|
RUN go mod download
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Get version and commit info for build injection
|
# Get version and commit info for build injection
|
||||||
@@ -53,31 +51,28 @@ RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/re
|
|||||||
apk update && apk upgrade && \
|
apk update && apk upgrade && \
|
||||||
apk add --no-cache build-base postgresql-client mysql-client ca-certificates tzdata sed curl bash vim wget
|
apk add --no-cache build-base postgresql-client mysql-client ca-certificates tzdata sed curl bash vim wget
|
||||||
|
|
||||||
# Copy the binary from the builder stage
|
# Create a non-root user and switch to it
|
||||||
COPY --from=builder /app/WeKnora .
|
RUN mkdir -p /data/files && \
|
||||||
COPY --from=builder /app/config ./config
|
adduser -D -g '' appuser && \
|
||||||
COPY --from=builder /app/scripts ./scripts
|
chown -R appuser:appuser /app /data/files
|
||||||
COPY --from=builder /app/migrations ./migrations
|
|
||||||
COPY --from=builder /app/dataset/samples ./dataset/samples
|
|
||||||
|
|
||||||
# Copy migrate tool from builder stage
|
# Copy migrate tool from builder stage
|
||||||
COPY --from=builder /go/bin/migrate /usr/local/bin/
|
COPY --from=builder /go/bin/migrate /usr/local/bin/
|
||||||
COPY --from=builder /go/pkg/mod/github.com/yanyiwu /go/pkg/mod/github.com/yanyiwu/
|
COPY --from=builder /go/pkg/mod/github.com/yanyiwu /go/pkg/mod/github.com/yanyiwu/
|
||||||
|
|
||||||
|
# Copy the binary from the builder stage
|
||||||
|
COPY --from=builder /app/config ./config
|
||||||
|
COPY --from=builder /app/scripts ./scripts
|
||||||
|
COPY --from=builder /app/migrations ./migrations
|
||||||
|
COPY --from=builder /app/dataset/samples ./dataset/samples
|
||||||
|
COPY --from=builder /app/WeKnora .
|
||||||
|
|
||||||
# Make scripts executable
|
# Make scripts executable
|
||||||
RUN chmod +x ./scripts/*.sh
|
RUN chmod +x ./scripts/*.sh
|
||||||
|
|
||||||
# Expose ports
|
# Expose ports
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
# Set environment variables
|
|
||||||
ENV CGO_ENABLED=1
|
|
||||||
|
|
||||||
# Create a non-root user and switch to it
|
|
||||||
RUN mkdir -p /data/files && \
|
|
||||||
adduser -D -g '' appuser && \
|
|
||||||
chown -R appuser:appuser /app /data/files
|
|
||||||
|
|
||||||
# Switch to non-root user and run the application directly
|
# Switch to non-root user and run the application directly
|
||||||
USER appuser
|
USER appuser
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user