From bfff3bc6e061e883ed929a9375eaccc4489e88c6 Mon Sep 17 00:00:00 2001 From: andream16 Date: Mon, 14 Oct 2024 11:39:15 +0100 Subject: [PATCH] Adding utilities to run buf in Docker. --- Makefile | 18 +++++++++++++++--- containers/Dockerfile.buf | 11 ++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index d307a4bb4..c086d733e 100644 --- a/Makefile +++ b/Makefile @@ -315,20 +315,32 @@ build_buf_container: $(DOCKER) build . -t $(BUF_CONTAINER) -f containers/Dockerfile.buf run_buf: build_buf_container - $(DOCKER) run --volume "$(shell pwd):/workspace" --workdir /workspace $(BUF_CONTAINER) $(ARGS) + $(eval BUF_TMP_DP_FOLDER:=buf-tmp) + @if [ ! -d "$(BUF_TMP_DP_FOLDER)" ]; then mkdir $(BUF_TMP_DP_FOLDER); fi + $(DOCKER) run \ + --volume "$(shell pwd):/workspace" \ + --volume $(BUF_TMP_DP_FOLDER):/tmp \ + --workdir /workspace \ + $(BUF_CONTAINER) \ + $(ARGS) + @rm -rf $(BUF_TMP_DP_FOLDER) fmt-proto: build_buf_container @echo "Tidying up Proto files" - $(MAKE) run_buf ARGS="format -w" + $(MAKE) run_buf ARGS="format -w --exclude-path vendor/" lint-proto: build_buf_container @echo "Linting Proto files" - $(MAKE) run_buf ARGS="lint --exclude-path vendor" + $(MAKE) run_buf ARGS="lint --exclude-path vendor/" generate-proto: build_buf_container @echo "Generating Proto files" $(MAKE) run_buf ARGS="generate" +dep-update-proto: + @echo "Updating buf.lock deps" + $(MAKE) run_buf ARGS="dep update" + ######################################## ########### RELEASE UTILITIES ########## ######################################## diff --git a/containers/Dockerfile.buf b/containers/Dockerfile.buf index 56bf92278..173ab4b16 100644 --- a/containers/Dockerfile.buf +++ b/containers/Dockerfile.buf @@ -1,12 +1,21 @@ +# Setup Go alpine to install protoc-gen-go plugin. FROM golang:alpine AS golang ENV GO111MODULE=on - RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.35.1 +# Setup Buf to have buf available. FROM bufbuild/buf:1.45.0 AS buf +# Add diffutils and its dependencies, which are needed by buf format. +FROM alpine AS diffutils +RUN apk add --no-cache diffutils +RUN mkdir /deps && ldd /usr/bin/diff | tr -s '[:blank:]' '\n' | grep '^/' | xargs -I '{}' cp --parents '{}' /deps + +# Wrap everything together in a scratch container to do all things buf. FROM scratch COPY --from=golang /go/bin/protoc-gen-go /go/bin/protoc-gen-go COPY --from=buf /usr/local/bin/buf /usr/local/bin/buf +COPY --from=diffutils /usr/bin/diff /usr/bin/diff +COPY --from=diffutils /deps / ENV PATH="/go/bin:${PATH}" ENTRYPOINT ["/usr/local/bin/buf"]