From 5f628cd0e5ad8fe4cc460eee909ca8e471397670 Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Mon, 8 Jun 2020 17:41:09 +0200 Subject: [PATCH] Refactor build process to build in containers Signed-off-by: Ulysses Souza Signed-off-by: Nicolas De Loof --- ecs/Dockerfile | 32 ++++++++++++++++++++++++++++++++ ecs/Makefile | 37 ++++++++++++++++++++++++++++++------- ecs/builder.Makefile | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 ecs/Dockerfile create mode 100644 ecs/builder.Makefile diff --git a/ecs/Dockerfile b/ecs/Dockerfile new file mode 100644 index 000000000..db374fb8b --- /dev/null +++ b/ecs/Dockerfile @@ -0,0 +1,32 @@ +# syntax = docker/dockerfile:experimental +ARG GO_VERSION=1.14.2 + +FROM golang:${GO_VERSION} AS base +ARG TARGET_OS=unknown +ARG TARGET_ARCH=unknown +ARG PWD=/ecs-plugin +ENV GO111MODULE=on + +WORKDIR ${PWD} +ADD go.* ${PWD} +RUN go mod download +ADD . ${PWD} + +FROM base AS make-plugin +RUN --mount=type=cache,target=/root/.cache/go-build \ + GOOS=${TARGET_OS} \ + GOARCH=${TARGET_ARCH} \ + make -f builder.Makefile build + +FROM base AS make-cross +RUN --mount=type=cache,target=/root/.cache/go-build \ + make -f builder.Makefile cross + +FROM scratch AS build +COPY --from=make-plugin /ecs-plugin/dist/* . + +FROM scratch AS cross +COPY --from=make-cross /ecs-plugin/dist/* . + +FROM base as test +RUN make -f builder.Makefile test diff --git a/ecs/Makefile b/ecs/Makefile index 0fd6b65ae..c5fe28758 100644 --- a/ecs/Makefile +++ b/ecs/Makefile @@ -1,11 +1,27 @@ -clean: - rm -rf dist/ +GOOS ?= $(shell go env GOOS) +GOARCH ?= $(shell go env GOARCH) +PWD = $(shell pwd) -build: - go build -v -o dist/docker-ecs cmd/main/main.go +export DOCKER_BUILDKIT=1 + +.DEFAULT_GOAL := build + +build: ## Build for the current + @docker build . \ + --output type=local,dest=./dist \ + --build-arg TARGET_OS=${GOOS} \ + --build-arg TARGET_ARCH=${GOARCH} \ + --target build + +cross: ## Cross build for linux, macos and windows + @docker build . \ + --output type=local,dest=./dist \ + --target cross test: build ## Run tests - go test ./... -v + @docker build . \ + --output type=local,dest=./dist \ + --target test e2e: build ## Run tests go test ./... -v -tags=e2e @@ -15,6 +31,13 @@ dev: build ln -f -s "${PWD}/dist/docker-ecs" "${HOME}/.docker/cli-plugins/docker-ecs" lint: ## Verify Go files - golangci-lint run --config ./golangci.yaml ./... + @docker run --rm -t \ + -v $(PWD):/app \ + -w /app \ + golangci/golangci-lint:v1.27-alpine \ + golangci-lint run --timeout 10m0s --config ./golangci.yaml ./... -.PHONY: clean build test dev lint e2e +clean: + rm -rf dist/ + +.PHONY: clean build test dev lint e2e cross diff --git a/ecs/builder.Makefile b/ecs/builder.Makefile new file mode 100644 index 000000000..0fd8e30b6 --- /dev/null +++ b/ecs/builder.Makefile @@ -0,0 +1,39 @@ +GOOS ?= $(shell go env GOOS) +GOARCH ?= $(shell go env GOARCH) + +PROTOS=$(shell find . -name \*.proto) + +EXTENSION := +ifeq ($(GOOS),windows) + EXTENSION := .exe +endif + +STATIC_FLAGS= CGO_ENABLED=0 +LDFLAGS := "-s -w" +GO_BUILD = $(STATIC_FLAGS) go build -trimpath -ldflags=$(LDFLAGS) + +BINARY=dist/docker +BINARY_WITH_EXTENSION=$(BINARY)$(EXTENSION) + +export DOCKER_BUILDKIT=1 + +all: build + +clean: + rm -rf dist/ + +build: + $(GO_BUILD) -v -o $(BINARY_WITH_EXTENSION) cmd/main/main.go + +cross: + @GOOS=linux GOARCH=amd64 $(GO_BUILD) -v -o $(BINARY)-linux-amd64 cmd/main/main.go + @GOOS=darwin GOARCH=amd64 $(GO_BUILD) -v -o $(BINARY)-darwin-amd64 cmd/main/main.go + @GOOS=windows GOARCH=amd64 $(GO_BUILD) -v -o $(BINARY)-windows-amd64.exe cmd/main/main.go + +test: build ## Run tests + @go test ./... -v + +lint: ## Verify Go files + golangci-lint run --timeout 10m0s --config ./golangci.yaml ./... + +.PHONY: clean build test dev lint e2e