2023-02-23 00:45:10 +08:00
|
|
|
# syntax=docker/dockerfile:1
|
2023-02-01 01:30:42 +08:00
|
|
|
|
2022-08-29 13:14:14 +08:00
|
|
|
# Forked from https://github.com/moby/buildkit/blob/e1b3b6c4abf7684f13e6391e5f7bc9210752687a/hack/dockerfiles/generated-files.Dockerfile
|
|
|
|
# Copyright The BuildKit Authors.
|
|
|
|
# Copyright The Buildx Authors.
|
|
|
|
# Licensed under the Apache License, Version 2.0
|
|
|
|
|
2024-07-03 13:27:43 +08:00
|
|
|
ARG GO_VERSION="1.22"
|
2022-08-29 13:14:14 +08:00
|
|
|
ARG PROTOC_VERSION="3.11.4"
|
2024-10-03 04:51:59 +08:00
|
|
|
ARG PROTOC_GOOGLEAPIS_VERSION=2af421884dd468d565137215c946ebe4e245ae26
|
2022-08-29 13:14:14 +08:00
|
|
|
|
|
|
|
# protoc is dynamically linked to glibc so can't use alpine base
|
2023-07-17 16:27:54 +08:00
|
|
|
FROM golang:${GO_VERSION}-bookworm AS base
|
2022-08-29 13:14:14 +08:00
|
|
|
RUN apt-get update && apt-get --no-install-recommends install -y git unzip
|
2024-10-03 04:51:59 +08:00
|
|
|
|
|
|
|
FROM base AS protoc
|
2022-08-29 13:14:14 +08:00
|
|
|
ARG PROTOC_VERSION
|
|
|
|
ARG TARGETOS
|
|
|
|
ARG TARGETARCH
|
|
|
|
RUN <<EOT
|
|
|
|
set -e
|
|
|
|
arch=$(echo $TARGETARCH | sed -e s/amd64/x86_64/ -e s/arm64/aarch_64/)
|
|
|
|
wget -q https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-${TARGETOS}-${arch}.zip
|
2024-10-03 04:51:59 +08:00
|
|
|
unzip protoc-${PROTOC_VERSION}-${TARGETOS}-${arch}.zip -d /opt/protoc
|
2022-08-29 13:14:14 +08:00
|
|
|
EOT
|
|
|
|
|
2024-10-03 04:51:59 +08:00
|
|
|
FROM base AS googleapis
|
|
|
|
ARG PROTOC_GOOGLEAPIS_VERSION
|
|
|
|
RUN <<EOT
|
|
|
|
set -e
|
|
|
|
wget -q https://github.com/googleapis/googleapis/archive/${PROTOC_GOOGLEAPIS_VERSION}.zip -O googleapis.zip
|
|
|
|
unzip googleapis.zip '*.proto' -d /opt
|
|
|
|
mv /opt/googleapis-${PROTOC_GOOGLEAPIS_VERSION} /opt/googleapis
|
|
|
|
EOT
|
|
|
|
|
|
|
|
FROM base AS protoc-buildkit
|
|
|
|
WORKDIR /app
|
|
|
|
RUN --mount=type=bind,target=/app \
|
|
|
|
--mount=type=cache,target=/root/.cache \
|
|
|
|
--mount=type=cache,target=/go/pkg/mod <<EOT
|
|
|
|
set -e
|
|
|
|
mkdir -p /opt/protoc
|
|
|
|
find vendor -name '*.proto' | tar -cf - --files-from - | tar -C /opt/protoc -xf -
|
|
|
|
EOT
|
|
|
|
|
|
|
|
FROM base AS gobuild-base
|
|
|
|
WORKDIR /go/src
|
|
|
|
COPY --link --from=protoc /opt/protoc /usr/local
|
|
|
|
COPY --link --from=googleapis /opt/googleapis /usr/local/include
|
|
|
|
COPY --link --from=protoc-buildkit /opt/protoc/vendor /usr/local/include
|
|
|
|
|
|
|
|
FROM gobuild-base AS tools
|
|
|
|
RUN --mount=type=bind,source=go.mod,target=/go/src/go.mod,ro \
|
|
|
|
--mount=type=bind,source=go.sum,target=/go/src/go.sum,ro \
|
2022-08-29 13:14:14 +08:00
|
|
|
--mount=type=cache,target=/root/.cache \
|
|
|
|
--mount=type=cache,target=/go/pkg/mod \
|
|
|
|
go install \
|
2024-10-03 04:51:59 +08:00
|
|
|
google.golang.org/protobuf/cmd/protoc-gen-go \
|
|
|
|
google.golang.org/grpc/cmd/protoc-gen-go-grpc
|
2022-08-29 13:14:14 +08:00
|
|
|
|
|
|
|
FROM tools AS generated
|
2024-10-03 04:51:59 +08:00
|
|
|
RUN --mount=type=bind,target=github.com/docker/buildx,ro <<EOT
|
2022-08-29 13:14:14 +08:00
|
|
|
set -ex
|
|
|
|
mkdir /out
|
2024-10-03 04:51:59 +08:00
|
|
|
find github.com/docker/buildx -name '*.proto' -o -name vendor -prune -false | xargs \
|
|
|
|
protoc --go_out=/out --go-grpc_out=require_unimplemented_servers=false:/out
|
2022-08-29 13:14:14 +08:00
|
|
|
EOT
|
|
|
|
|
|
|
|
FROM scratch AS update
|
2024-10-03 04:51:59 +08:00
|
|
|
COPY --from=generated /out/github.com/docker/buildx /
|
2022-08-29 13:14:14 +08:00
|
|
|
|
2024-10-03 04:51:59 +08:00
|
|
|
FROM gobuild-base AS validate
|
2022-08-29 13:14:14 +08:00
|
|
|
RUN --mount=type=bind,target=.,rw \
|
2024-10-03 04:51:59 +08:00
|
|
|
--mount=type=bind,from=update,target=/generated-files <<EOT
|
2022-08-29 13:14:14 +08:00
|
|
|
set -e
|
|
|
|
git add -A
|
|
|
|
if [ "$(ls -A /generated-files)" ]; then
|
|
|
|
cp -rf /generated-files/* .
|
|
|
|
fi
|
|
|
|
diff=$(git status --porcelain -- ':!vendor' '**/*.pb.go')
|
|
|
|
if [ -n "$diff" ]; then
|
|
|
|
echo >&2 'ERROR: The result of "go generate" differs. Please update with "make generated-files"'
|
|
|
|
echo "$diff"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
EOT
|