| 1 |
# syntax=docker/dockerfile:1 |
| 2 |
|
| 3 |
ARG GO_VERSION=1.23 |
| 4 |
ARG XX_VERSION=1.6.1 |
| 5 |
|
| 6 |
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx |
| 7 |
|
| 8 |
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS base |
| 9 |
RUN apk add --no-cache git |
| 10 |
COPY --from=xx / / |
| 11 |
WORKDIR /src |
| 12 |
|
| 13 |
FROM base AS build |
| 14 |
ARG TARGETPLATFORM |
| 15 |
RUN --mount=target=. --mount=target=/go/pkg/mod,type=cache \ |
| 16 |
--mount=target=/root/.cache,type=cache \ |
| 17 |
xx-go build ./... |
| 18 |
|
| 19 |
FROM base AS test |
| 20 |
ARG TESTFLAGS |
| 21 |
RUN --mount=target=. --mount=target=/go/pkg/mod,type=cache \ |
| 22 |
--mount=target=/root/.cache,type=cache \ |
| 23 |
CGO_ENABLED=0 xx-go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic ${TESTFLAGS} ./... |
| 24 |
|
| 25 |
FROM base AS test-noroot |
| 26 |
RUN mkdir /go/pkg && chmod 0777 /go/pkg |
| 27 |
USER 1000:1000 |
| 28 |
RUN --mount=target=. \ |
| 29 |
--mount=target=/tmp/.cache,type=cache \ |
| 30 |
CGO_ENABLED=0 GOCACHE=/tmp/gocache xx-go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic ./... |
| 31 |
|
| 32 |
FROM scratch AS test-coverage |
| 33 |
COPY --from=test /tmp/coverage.txt /coverage-root.txt |
| 34 |
|
| 35 |
FROM scratch AS test-noroot-coverage |
| 36 |
COPY --from=test-noroot /tmp/coverage.txt /coverage-noroot.txt |
| 37 |
|
| 38 |
FROM build |