ARG BUILD_IMAGE="golang:alpine"
ARG RUNTIME_IMAGE="alpine:latest"

FROM ${BUILD_IMAGE} AS garble
ARG GOPROXY="https://goproxy.cn,https://goproxy.io,direct"
RUN GOPROXY=${GOPROXY} go install mvdan.cc/garble@latest

FROM garble AS builder
ARG GOPROXY="https://goproxy.cn,https://goproxy.io,direct"
WORKDIR /app
COPY . /app
RUN GOPROXY=${GOPROXY} GO111MODULE=on CGO_ENABLED=0 GOOS=linux garble -tiny -seed=random -literals build -ldflags '-s -w' -o app

FROM ${RUNTIME_IMAGE} AS runner
ARG APP_NAME="app"
COPY --from=builder /app/app /usr/local/bin/${APP_NAME}
RUN ln -s /usr/local/bin/${APP_NAME} /usr/local/bin/entrypoint
ENTRYPOINT [ "entrypoint" ]