From 88aef2e4e2fafd6651f7de7b23cf56bb8767233b Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Wed, 22 Apr 2020 10:04:11 +0200 Subject: [PATCH] Dockerize build and proto gen Signed-off-by: Ulysses Souza --- Dockerfile | 39 ++++++++++++++++++++++++++++++++++ Makefile | 46 ++++++++++++++++++++++++++++++++++------ backend/v1/backend.pb.go | 4 ++-- 3 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..a30b3538a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +# syntax = docker/dockerfile:experimental +ARG GO_VERSION=1.14.2 + +FROM golang:${GO_VERSION} AS fs +ARG TARGET_OS=unknown +ARG TARGET_ARCH=unknown +ARG PWD=$GOPATH/src/github.com/docker/api +RUN apt-get update && apt-get install --no-install-recommends -y \ + make \ + git \ + protobuf-compiler \ + libprotobuf-dev +RUN go get github.com/golang/protobuf/protoc-gen-go +WORKDIR ${PWD} +ADD go.* ${PWD} +RUN go mod download +ADD . ${PWD} + +FROM fs AS make-protos +RUN make protos + +FROM make-protos AS make-bins +RUN --mount=type=cache,target=/root/.cache/go-build \ + GOOS=${TARGET_OS} \ + GOARCH=${TARGET_ARCH} \ + make bins + +FROM make-protos AS make-xbins +RUN --mount=type=cache,target=/root/.cache/go-build \ + make xbins + +FROM scratch AS protos +COPY --from=make-protos /go/src/github.com/docker/api/backend/v1/*.pb.go . + +FROM scratch AS bins +COPY --from=make-bins /go/src/github.com/docker/api/bin/* . + +FROM scratch AS xbins +COPY --from=make-xbins /go/src/github.com/docker/api/bin/* . diff --git a/Makefile b/Makefile index 41d2abe20..d6451f291 100644 --- a/Makefile +++ b/Makefile @@ -25,21 +25,53 @@ GIT_COMMIT=$(shell git rev-parse --short HEAD) GOOS ?= $(shell go env GOOS) +GOARCH ?= $(shell go env GOARCH) + PROTOS=$(shell find . -name \*.proto) -export GO111MODULE=auto +export DOCKER_BUILDKIT=1 -all: protos example cli - -cli: - cd cmd && go build -v -o ../bin/docker +all: dbins +xall: dxbins +bins: cli example +xbins: xcli xexample protos: @protoc -I. --go_out=plugins=grpc,paths=source_relative:. ${PROTOS} -example: +cli: protos + cd cmd && GOOS=${GOOS} GOARCH=${GOARCH} go build -v -o ../bin/docker + +example: protos cd example/backend && go build -v -o ../../bin/backend-example +xcli: cli + cd cmd && GOOS=linux GOARCH=amd64 go build -v -o ../bin/docker-linux-amd64 + cd cmd && GOOS=darwin GOARCH=amd64 go build -v -o ../bin/docker-darwin-amd64 + cd cmd && GOOS=windows GOARCH=amd64 go build -v -o ../bin/docker-windows-amd64.exe + +xexample: example + cd example/backend && GOOS=linux GOARCH=amd64 go build -v -o ../../bin/backend-example-linux-amd64 + cd example/backend && GOOS=darwin GOARCH=amd64 go build -v -o ../../bin/backend-example-darwin-amd64 + cd example/backend && GOOS=windows GOARCH=amd64 go build -v -o ../../bin/backend-example-windows-amd64.exe + +dprotos: + docker build . \ + --output type=local,dest=./backend/v1 \ + --target protos + +dbins: dprotos + docker build . \ + --output type=local,dest=./bin \ + --build-arg TARGET_OS=${GOOS} \ + --build-arg TARGET_ARCH=${GOARCH} \ + --target bins + +dxbins: dbins + docker build . \ + --output type=local,dest=./bin \ + --target xbins + FORCE: -.PHONY: protos example cli +.PHONY: all xall protos example xexample xcli cli bins dbins dxbins dprotos diff --git a/backend/v1/backend.pb.go b/backend/v1/backend.pb.go index c21d53f78..70f86c435 100644 --- a/backend/v1/backend.pb.go +++ b/backend/v1/backend.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.21.0 -// protoc v3.11.4 +// protoc-gen-go v1.21.0-devel +// protoc v3.6.1 // source: backend/v1/backend.proto package v1