| 1 |
# syntax=docker/dockerfile-upstream:master |
| 2 |
|
| 3 |
ARG GO_VERSION=1.25 |
| 4 |
ARG DEBIAN_VERSION=bookworm |
| 5 |
ARG PROTOC_VERSION=3.11.4 |
| 6 |
ARG PROTOC_GOOGLEAPIS_VERSION=2af421884dd468d565137215c946ebe4e245ae26 |
| 7 |
|
| 8 |
# protoc is dynamically linked to glibc so can't use alpine base |
| 9 |
|
| 10 |
# base defines common stage with Go tools |
| 11 |
FROM golang:${GO_VERSION}-${DEBIAN_VERSION} AS base |
| 12 |
RUN apt-get update && apt-get --no-install-recommends install -y git unzip |
| 13 |
|
| 14 |
FROM base AS protoc |
| 15 |
ARG PROTOC_VERSION |
| 16 |
ARG TARGETOS |
| 17 |
ARG TARGETARCH |
| 18 |
RUN <<EOT |
| 19 |
set -e |
| 20 |
arch=$(echo $TARGETARCH | sed -e s/amd64/x86_64/ -e s/arm64/aarch_64/) |
| 21 |
wget -q https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-${TARGETOS}-${arch}.zip |
| 22 |
unzip protoc-${PROTOC_VERSION}-${TARGETOS}-${arch}.zip -d /opt/protoc |
| 23 |
EOT |
| 24 |
|
| 25 |
FROM base AS googleapis |
| 26 |
ARG PROTOC_GOOGLEAPIS_VERSION |
| 27 |
RUN <<EOT |
| 28 |
set -e |
| 29 |
wget -q https://github.com/googleapis/googleapis/archive/${PROTOC_GOOGLEAPIS_VERSION}.zip -O googleapis.zip |
| 30 |
unzip googleapis.zip '*.proto' -d /opt |
| 31 |
mkdir -p /opt/googleapis |
| 32 |
mv /opt/googleapis-${PROTOC_GOOGLEAPIS_VERSION} /opt/googleapis/include |
| 33 |
EOT |
| 34 |
|
| 35 |
FROM base AS gobuild-base |
| 36 |
WORKDIR /app |
| 37 |
|
| 38 |
FROM gobuild-base AS vtprotobuf |
| 39 |
RUN --mount=type=bind,source=go.mod,target=/app/go.mod \ |
| 40 |
--mount=type=bind,source=go.sum,target=/app/go.sum \ |
| 41 |
--mount=type=cache,target=/root/.cache \ |
| 42 |
--mount=type=cache,target=/go/pkg/mod <<EOT |
| 43 |
set -e |
| 44 |
mkdir -p /opt/vtprotobuf |
| 45 |
go mod download github.com/planetscale/vtprotobuf |
| 46 |
cp -R $(go list -m -f='{{.Dir}}' github.com/planetscale/vtprotobuf)/include /opt/vtprotobuf |
| 47 |
chmod -R 0755 /opt/vtprotobuf |
| 48 |
EOT |
| 49 |
|
| 50 |
FROM gobuild-base AS vendored |
| 51 |
RUN --mount=type=bind,source=vendor,target=/app <<EOT |
| 52 |
set -e |
| 53 |
mkdir -p /opt/vendored/include |
| 54 |
find . -name '*.proto' | tar -cf - --files-from - | tar -C /opt/vendored/include -xf - |
| 55 |
EOT |
| 56 |
|
| 57 |
FROM scratch AS protobuf |
| 58 |
COPY --link --from=protoc /opt/protoc / |
| 59 |
COPY --link --from=googleapis /opt/googleapis / |
| 60 |
COPY --link --from=vtprotobuf /opt/vtprotobuf / |
| 61 |
COPY --link --from=vendored /opt/vendored / |
| 62 |
|
| 63 |
FROM gobuild-base AS tools |
| 64 |
RUN --mount=type=bind,source=go.mod,target=/app/go.mod \ |
| 65 |
--mount=type=bind,source=go.sum,target=/app/go.sum \ |
| 66 |
--mount=type=cache,target=/root/.cache \ |
| 67 |
--mount=type=cache,target=/go/pkg/mod \ |
| 68 |
go install tool \ |
| 69 |
github.com/planetscale/vtprotobuf/cmd/protoc-gen-go-vtproto \ |
| 70 |
google.golang.org/grpc/cmd/protoc-gen-go-grpc \ |
| 71 |
google.golang.org/protobuf/cmd/protoc-gen-go |
| 72 |
COPY --link --from=protobuf / /usr/local |
| 73 |
|
| 74 |
FROM tools AS generated |
| 75 |
RUN --mount=type=bind,target=github.com/moby/buildkit <<EOT |
| 76 |
set -ex |
| 77 |
mkdir /out |
| 78 |
find github.com/moby/buildkit -name '*.proto' -o -name vendor -prune -false | xargs \ |
| 79 |
protoc --go_out=/out --go-grpc_out=require_unimplemented_servers=false:/out \ |
| 80 |
--go-vtproto_out=features=marshal+unmarshal+size+equal+pool+clone:/out |
| 81 |
EOT |
| 82 |
|
| 83 |
FROM scratch AS update |
| 84 |
COPY --from=generated /out/github.com/moby/buildkit / |
| 85 |
|
| 86 |
FROM gobuild-base AS validate |
| 87 |
RUN --mount=type=bind,target=.,rw \ |
| 88 |
--mount=type=bind,from=update,target=/generated-files <<EOT |
| 89 |
set -e |
| 90 |
git add -A |
| 91 |
if [ "$(ls -A /generated-files)" ]; then |
| 92 |
cp -rf /generated-files/* . |
| 93 |
fi |
| 94 |
diff=$(git status --porcelain -- ':!vendor' '**/*.pb.go') |
| 95 |
if [ -n "$diff" ]; then |
| 96 |
echo >&2 'ERROR: The result of "go generate" differs. Please update with "make generated-files"' |
| 97 |
echo "$diff" |
| 98 |
exit 1 |
| 99 |
fi |
| 100 |
EOT |