2022-03-10 09:05:44 +08:00
|
|
|
# syntax=docker/dockerfile:1.4
|
2019-03-23 07:23:09 +08:00
|
|
|
|
2022-04-06 07:59:22 +08:00
|
|
|
ARG GO_VERSION=1.18
|
2022-04-08 23:26:40 +08:00
|
|
|
ARG DOCKERD_VERSION=20.10.14
|
2019-03-23 07:23:09 +08:00
|
|
|
|
|
|
|
FROM docker:$DOCKERD_VERSION AS dockerd-release
|
|
|
|
|
2021-06-29 09:30:27 +08:00
|
|
|
# xx is a helper for cross-compilation
|
2022-04-08 23:28:45 +08:00
|
|
|
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.1.0 AS xx
|
2019-03-23 07:23:09 +08:00
|
|
|
|
2021-09-03 05:11:05 +08:00
|
|
|
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS golatest
|
2021-06-29 09:36:21 +08:00
|
|
|
|
2021-09-03 05:11:05 +08:00
|
|
|
FROM golatest AS gobase
|
2021-06-29 09:30:27 +08:00
|
|
|
COPY --from=xx / /
|
2019-03-23 09:18:07 +08:00
|
|
|
RUN apk add --no-cache file git
|
2019-03-23 07:23:09 +08:00
|
|
|
ENV GOFLAGS=-mod=vendor
|
2022-05-06 17:59:43 +08:00
|
|
|
ENV CGO_ENABLED=0
|
2019-03-23 07:23:09 +08:00
|
|
|
WORKDIR /src
|
|
|
|
|
|
|
|
FROM gobase AS buildx-version
|
|
|
|
RUN --mount=target=. \
|
2019-04-25 10:29:56 +08:00
|
|
|
PKG=github.com/docker/buildx VERSION=$(git describe --match 'v[0-9]*' --dirty='.m' --always --tags) REVISION=$(git rev-parse HEAD)$(if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi); \
|
2019-03-23 07:23:09 +08:00
|
|
|
echo "-X ${PKG}/version.Version=${VERSION} -X ${PKG}/version.Revision=${REVISION} -X ${PKG}/version.Package=${PKG}" | tee /tmp/.ldflags; \
|
|
|
|
echo -n "${VERSION}" | tee /tmp/.version;
|
|
|
|
|
|
|
|
FROM gobase AS buildx-build
|
2021-11-05 09:11:26 +08:00
|
|
|
ARG LDFLAGS="-w -s"
|
2019-03-23 07:23:09 +08:00
|
|
|
ARG TARGETPLATFORM
|
2021-09-04 04:49:59 +08:00
|
|
|
RUN --mount=type=bind,target=. \
|
|
|
|
--mount=type=cache,target=/root/.cache \
|
|
|
|
--mount=type=cache,target=/go/pkg/mod \
|
|
|
|
--mount=type=bind,source=/tmp/.ldflags,target=/tmp/.ldflags,from=buildx-version \
|
2021-11-05 09:11:26 +08:00
|
|
|
set -x; xx-go build -ldflags "$(cat /tmp/.ldflags) ${LDFLAGS}" -o /usr/bin/buildx ./cmd/buildx && \
|
2021-06-29 09:30:27 +08:00
|
|
|
xx-verify --static /usr/bin/buildx
|
2019-03-23 07:23:09 +08:00
|
|
|
|
2022-05-06 17:59:43 +08:00
|
|
|
FROM gobase AS test
|
2021-09-04 04:49:59 +08:00
|
|
|
RUN --mount=type=bind,target=. \
|
|
|
|
--mount=type=cache,target=/root/.cache \
|
|
|
|
--mount=type=cache,target=/go/pkg/mod \
|
|
|
|
go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic ./... && \
|
|
|
|
go tool cover -func=/tmp/coverage.txt
|
2019-03-23 09:52:36 +08:00
|
|
|
|
2021-09-04 04:49:59 +08:00
|
|
|
FROM scratch AS test-coverage
|
|
|
|
COPY --from=test /tmp/coverage.txt /coverage.txt
|
2019-03-23 07:23:09 +08:00
|
|
|
|
2019-03-23 08:21:05 +08:00
|
|
|
FROM scratch AS binaries-unix
|
2022-05-06 17:55:49 +08:00
|
|
|
COPY --link --from=buildx-build /usr/bin/buildx /
|
2019-03-23 08:21:05 +08:00
|
|
|
|
|
|
|
FROM binaries-unix AS binaries-darwin
|
|
|
|
FROM binaries-unix AS binaries-linux
|
|
|
|
|
|
|
|
FROM scratch AS binaries-windows
|
2022-05-06 17:55:49 +08:00
|
|
|
COPY --link --from=buildx-build /usr/bin/buildx /buildx.exe
|
2019-03-23 08:21:05 +08:00
|
|
|
|
|
|
|
FROM binaries-$TARGETOS AS binaries
|
|
|
|
|
2021-09-04 04:49:59 +08:00
|
|
|
# Release
|
2019-04-25 08:54:56 +08:00
|
|
|
FROM --platform=$BUILDPLATFORM alpine AS releaser
|
|
|
|
WORKDIR /work
|
|
|
|
ARG TARGETPLATFORM
|
|
|
|
RUN --mount=from=binaries \
|
2021-09-04 04:49:59 +08:00
|
|
|
--mount=type=bind,source=/tmp/.version,target=/tmp/.version,from=buildx-version \
|
2019-04-25 08:54:56 +08:00
|
|
|
mkdir -p /out && cp buildx* "/out/buildx-$(cat /tmp/.version).$(echo $TARGETPLATFORM | sed 's/\//-/g')$(ls buildx* | sed -e 's/^buildx//')"
|
|
|
|
|
|
|
|
FROM scratch AS release
|
|
|
|
COPY --from=releaser /out/ /
|
|
|
|
|
2021-09-04 04:49:59 +08:00
|
|
|
# Shell
|
|
|
|
FROM docker:$DOCKERD_VERSION AS dockerd-release
|
|
|
|
FROM alpine AS shell
|
2019-04-25 01:49:34 +08:00
|
|
|
RUN apk add --no-cache iptables tmux git vim less openssh
|
2019-03-23 07:23:09 +08:00
|
|
|
RUN mkdir -p /usr/local/lib/docker/cli-plugins && ln -s /usr/local/bin/buildx /usr/local/lib/docker/cli-plugins/docker-buildx
|
|
|
|
COPY ./hack/demo-env/entrypoint.sh /usr/local/bin
|
|
|
|
COPY ./hack/demo-env/tmux.conf /root/.tmux.conf
|
|
|
|
COPY --from=dockerd-release /usr/local/bin /usr/local/bin
|
2019-03-24 12:30:29 +08:00
|
|
|
WORKDIR /work
|
|
|
|
COPY ./hack/demo-env/examples .
|
2019-03-23 08:21:05 +08:00
|
|
|
COPY --from=binaries / /usr/local/bin/
|
2019-03-23 07:23:09 +08:00
|
|
|
VOLUME /var/lib/docker
|
|
|
|
ENTRYPOINT ["entrypoint.sh"]
|
2019-04-25 07:00:59 +08:00
|
|
|
|
2021-09-04 04:49:59 +08:00
|
|
|
FROM binaries
|