2020-05-28 13:29:02 +02:00
|
|
|
# syntax = docker/dockerfile:1-experimental
|
|
|
|
|
|
2025-05-06 21:01:22 -04:00
|
|
|
FROM --platform=${BUILDPLATFORM} golang:1.24.3-alpine AS base
|
2020-05-28 13:29:02 +02:00
|
|
|
WORKDIR /src
|
2021-07-05 10:31:07 -04:00
|
|
|
ENV CGO_ENABLED=1
|
|
|
|
|
RUN apk update
|
|
|
|
|
RUN apk add gcc g++
|
|
|
|
|
RUN mkdir /out
|
|
|
|
|
|
|
|
|
|
FROM base AS buildbase
|
2020-05-28 13:29:02 +02:00
|
|
|
COPY go.* .
|
2020-10-03 10:48:43 +02:00
|
|
|
RUN --mount=type=cache,target=/go/pkg/mod \
|
|
|
|
|
go mod download
|
2020-05-28 13:29:02 +02:00
|
|
|
|
2021-07-05 10:31:07 -04:00
|
|
|
FROM buildbase AS build
|
2020-05-28 13:29:02 +02:00
|
|
|
ARG TARGETOS
|
|
|
|
|
ARG TARGETARCH
|
|
|
|
|
RUN --mount=target=. \
|
2020-10-03 10:48:43 +02:00
|
|
|
--mount=type=cache,target=/go/pkg/mod \
|
2020-05-28 13:29:02 +02:00
|
|
|
--mount=type=cache,target=/root/.cache/go-build \
|
|
|
|
|
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /out/example .
|
|
|
|
|
|
|
|
|
|
FROM base AS unit-test
|
|
|
|
|
RUN --mount=target=. \
|
2020-10-03 10:48:43 +02:00
|
|
|
--mount=type=cache,target=/go/pkg/mod \
|
2020-05-28 13:29:02 +02:00
|
|
|
--mount=type=cache,target=/root/.cache/go-build \
|
2021-07-05 12:36:36 -04:00
|
|
|
go test -v -coverprofile=/out/cover.out ./...
|
2020-05-28 13:29:02 +02:00
|
|
|
|
2020-10-03 10:48:12 +02:00
|
|
|
FROM golangci/golangci-lint:v1.31.0-alpine AS lint-base
|
2020-05-28 13:29:02 +02:00
|
|
|
|
|
|
|
|
FROM base AS lint
|
|
|
|
|
RUN --mount=target=. \
|
|
|
|
|
--mount=from=lint-base,src=/usr/bin/golangci-lint,target=/usr/bin/golangci-lint \
|
2020-10-03 10:48:43 +02:00
|
|
|
--mount=type=cache,target=/go/pkg/mod \
|
2020-05-28 13:29:02 +02:00
|
|
|
--mount=type=cache,target=/root/.cache/go-build \
|
|
|
|
|
--mount=type=cache,target=/root/.cache/golangci-lint \
|
|
|
|
|
golangci-lint run --timeout 10m0s ./...
|
|
|
|
|
|
2021-07-05 10:31:07 -04:00
|
|
|
FROM base as mod-init-base
|
|
|
|
|
WORKDIR /out
|
|
|
|
|
COPY . .
|
2025-05-06 21:01:22 -04:00
|
|
|
ENV MODNAME example
|
2021-07-05 10:36:12 -04:00
|
|
|
RUN go mod init "${MODNAME}" && go mod tidy
|
2021-07-05 10:31:07 -04:00
|
|
|
|
|
|
|
|
FROM base AS mod-tidy
|
|
|
|
|
WORKDIR /out
|
|
|
|
|
COPY . .
|
|
|
|
|
RUN go mod tidy
|
|
|
|
|
|
|
|
|
|
FROM scratch AS mod-init
|
|
|
|
|
COPY --from=mod-init-base /out/go.mod /go.mod
|
|
|
|
|
COPY --from=mod-init-base /out/go.sum /go.sum
|
|
|
|
|
|
|
|
|
|
FROM scratch AS tidy
|
2021-07-05 17:20:32 -04:00
|
|
|
COPY --from=mod-tidy /out/go.mod /go.mod
|
2021-07-05 10:31:07 -04:00
|
|
|
COPY --from=mod-tidy /out/go.sum /go.sum
|
2021-07-06 08:35:02 -04:00
|
|
|
COPY --from=mod-tidy /out/go.mod /go.mod
|
2021-07-05 10:31:07 -04:00
|
|
|
|
2020-08-12 18:52:18 +02:00
|
|
|
FROM scratch AS unit-test-coverage
|
|
|
|
|
COPY --from=unit-test /out/cover.out /cover.out
|
|
|
|
|
|
2020-05-28 13:29:02 +02:00
|
|
|
FROM scratch AS bin-unix
|
|
|
|
|
COPY --from=build /out/example /
|
|
|
|
|
|
|
|
|
|
FROM bin-unix AS bin-linux
|
|
|
|
|
FROM bin-unix AS bin-darwin
|
|
|
|
|
|
|
|
|
|
FROM scratch AS bin-windows
|
|
|
|
|
COPY --from=build /out/example /example.exe
|
|
|
|
|
|
|
|
|
|
FROM bin-${TARGETOS} as bin
|