|
| 1 | +# syntax=docker/dockerfile:1 |
| 2 | + |
| 3 | +ARG GO_VERSION=1.16.7 |
| 4 | +ARG XX_VERSION=1.1.2 |
| 5 | +ARG OSXCROSS_VERSION=11.3-r7-alpine |
| 6 | + |
| 7 | +ARG PKG=github.com/docker/docker-credential-helpers |
| 8 | + |
| 9 | +# xx is a helper for cross-compilation |
| 10 | +FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx |
| 11 | + |
| 12 | +# osxcross contains the MacOSX cross toolchain for xx |
| 13 | +FROM crazymax/osxcross:${OSXCROSS_VERSION} AS osxcross |
| 14 | + |
| 15 | +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS gobase |
| 16 | +COPY --from=xx / / |
| 17 | +RUN apk add --no-cache clang file git lld llvm pkgconf |
| 18 | +ENV GOFLAGS="-mod=vendor" |
| 19 | +ENV CGO_ENABLED="1" |
| 20 | +WORKDIR /src |
| 21 | + |
| 22 | +FROM gobase AS version |
| 23 | +ARG PKG |
| 24 | +RUN --mount=target=. \ |
| 25 | + VERSION=$(git describe --match 'v[0-9]*' --dirty='.m' --always --tags); \ |
| 26 | + echo "-s -w -X ${PKG}/credentials.Version=${VERSION}" | tee /tmp/.ldflags; \ |
| 27 | + echo -n "${VERSION}" | tee /tmp/.version; |
| 28 | + |
| 29 | +FROM gobase AS base |
| 30 | +ARG TARGETPLATFORM |
| 31 | +RUN xx-apk add musl-dev gcc libsecret-dev pass |
| 32 | + |
| 33 | +FROM base AS build-linux |
| 34 | +ARG TARGETOS |
| 35 | +ARG TARGETARCH |
| 36 | +ARG TARGETVARIANT |
| 37 | +RUN --mount=type=bind,target=. \ |
| 38 | + --mount=type=cache,target=/root/.cache \ |
| 39 | + --mount=type=cache,target=/go/pkg/mod \ |
| 40 | + --mount=type=bind,from=version,source=/tmp/.ldflags,target=/tmp/.ldflags <<EOT |
| 41 | + set -ex |
| 42 | + mkdir /out |
| 43 | + xx-go build -ldflags "$(cat /tmp/.ldflags)" -o /out/docker-credential-pass-${TARGETOS}-${TARGETARCH}${TARGETVARIANT} ./pass/cmd/main.go |
| 44 | + xx-verify /out/docker-credential-pass-${TARGETOS}-${TARGETARCH}${TARGETVARIANT} |
| 45 | + xx-go build -ldflags "$(cat /tmp/.ldflags)" -o /out/docker-credential-secretservice-${TARGETOS}-${TARGETARCH}${TARGETVARIANT} ./secretservice/cmd/main_linux.go |
| 46 | + xx-verify /out/docker-credential-secretservice-${TARGETOS}-${TARGETARCH}${TARGETVARIANT} |
| 47 | +EOT |
| 48 | + |
| 49 | +FROM base AS build-darwin |
| 50 | +ARG TARGETARCH |
| 51 | +ARG TARGETVARIANT |
| 52 | +RUN --mount=type=bind,target=. \ |
| 53 | + --mount=type=cache,target=/root/.cache \ |
| 54 | + --mount=type=cache,target=/go/pkg/mod \ |
| 55 | + --mount=type=bind,from=osxcross,src=/osxsdk,target=/xx-sdk \ |
| 56 | + --mount=type=bind,from=version,source=/tmp/.ldflags,target=/tmp/.ldflags <<EOT |
| 57 | + set -ex |
| 58 | + mkdir /out |
| 59 | + xx-go install std |
| 60 | + xx-go build -ldflags "$(cat /tmp/.ldflags)" -o /out/docker-credential-osxkeychain-${TARGETARCH}${TARGETVARIANT} ./osxkeychain/cmd/main_darwin.go |
| 61 | + xx-verify /out/docker-credential-osxkeychain-${TARGETARCH}${TARGETVARIANT} |
| 62 | +EOT |
| 63 | + |
| 64 | +FROM base AS build-windows |
| 65 | +ARG TARGETARCH |
| 66 | +ARG TARGETVARIANT |
| 67 | +RUN --mount=type=bind,target=. \ |
| 68 | + --mount=type=cache,target=/root/.cache \ |
| 69 | + --mount=type=cache,target=/go/pkg/mod \ |
| 70 | + --mount=type=bind,from=version,source=/tmp/.ldflags,target=/tmp/.ldflags <<EOT |
| 71 | + set -ex |
| 72 | + mkdir /out |
| 73 | + xx-go build -ldflags "$(cat /tmp/.ldflags)" -o /out/docker-credential-wincred-${TARGETARCH}${TARGETVARIANT}.exe ./wincred/cmd/main_windows.go |
| 74 | + xx-verify /out/docker-credential-wincred-${TARGETARCH}${TARGETVARIANT}.exe |
| 75 | +EOT |
| 76 | + |
| 77 | +FROM build-$TARGETOS AS build |
| 78 | + |
| 79 | +FROM scratch AS binaries |
| 80 | +COPY --from=build /out / |
0 commit comments