diff --git a/tools/dockerfile/Dockerfile b/tools/dockerfile/Dockerfile index 3f1ee455..6081e969 100644 --- a/tools/dockerfile/Dockerfile +++ b/tools/dockerfile/Dockerfile @@ -1,26 +1,40 @@ -FROM --platform=$TARGETPLATFORM golang:1.22-bullseye as build +ARG GO_VERSION=1.22 +ARG BASE_IMAGE="golang:${GO_VERSION}-bullseye" + +FROM --platform=$TARGETPLATFORM ${BASE_IMAGE} as build ENV PROJ_ROOT="/go/src/github.com/erda-project/erda-infra" -COPY . "${PROJ_ROOT}" WORKDIR "${PROJ_ROOT}" -ARG GOPROXY=https://goproxy.cn,direct -RUN go env -w GO111MODULE=on && go env -w GOPROXY="${GOPROXY}" +ENV GOPROXY=https://goproxy.cn,direct \ + GO111MODULE=on \ + CGO_ENABLED=0 + +# Copy go.mod and go.sum to leverage Docker cache +COPY go.mod go.sum ./ +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod download + +# Copy all project files +COPY . . + +# Install goimports and gohub RUN go install golang.org/x/tools/cmd/goimports@latest -RUN --mount=type=cache,target=/root/.cache/go-build\ +# Build gohub with cache mechanism +RUN --mount=type=cache,target=/root/.cache/go-build \ --mount=type=cache,target=/go/pkg/mod \ - cd ./tools && \ - go install ./gohub && \ + go install ./tools/gohub && \ gohub tools install --verbose --local - -FROM --platform=$TARGETPLATFORM golang:1.22-bullseye +FROM --platform=$TARGETPLATFORM ${BASE_IMAGE} RUN go install golang.org/x/tools/cmd/goimports@latest + +# Copy built tools from build stage COPY --from=build /root/.gohub /root/.gohub COPY --from=build /go/bin/* /go/bin/ WORKDIR /go -CMD gohub +CMD ["gohub"]