From e4abc9665cec7782767e6fb8414353baf54c87ab Mon Sep 17 00:00:00 2001 From: Ben Howard Date: Tue, 31 Oct 2017 11:00:53 -0600 Subject: [PATCH] Makefile: enable build via make and added ability to build and use local docker containers. --- .dockerignore | 2 -- Dockerfile.build | 10 ++++++++ Dockerfile.cntr | 27 +++++++++++++++++++++ Makefile | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) delete mode 100644 .dockerignore create mode 100644 Dockerfile.build create mode 100644 Dockerfile.cntr create mode 100644 Makefile diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 5d0f124ff..000000000 --- a/.dockerignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!Dockerfile diff --git a/Dockerfile.build b/Dockerfile.build new file mode 100644 index 000000000..165d1253d --- /dev/null +++ b/Dockerfile.build @@ -0,0 +1,10 @@ +FROM golang:1.9-alpine + +ENV MAKE_TARGET linux_amd64 + +RUN apk update && \ + apk add bash && \ + apk add curl && \ + apk add git && \ + apk add make && \ + apk add libc6-compat diff --git a/Dockerfile.cntr b/Dockerfile.cntr new file mode 100644 index 000000000..3dc56f4da --- /dev/null +++ b/Dockerfile.cntr @@ -0,0 +1,27 @@ +FROM doctl_builder + +ENV CGO=0 +ENV OUT_D /out + +RUN mkdir -p /out +RUN mkdir -p /go/src/github.com/digitalocean/doctl +ADD . /go/src/github.com/digitalocean/doctl/ + +RUN cd /go/src/github.com/digitalocean/doctl && \ + make build_linux_amd64 OUT=/out && \ + make build_linux_386 OUT=/out && \ + make build_mac OUT=/out + +RUN find /out + +FROM alpine:latest + +VOLUME ["/copy"] + +RUN apk update && \ + apk add rsync && \ + apk add libc6-compat + +COPY --from=0 /out/* /app/ + +ENTRYPOINT ["/app/doctl_linux_amd64"] diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..423a057da --- /dev/null +++ b/Makefile @@ -0,0 +1,61 @@ +.PHONE: build +export CGO=0 + +my_d=$(shell pwd) +OUT_D = $(shell echo $${OUT_D:-$(my_d)/builds}) + +GOOS = linux +UNAME_S := $(shell uname -s) +UNAME_P := $(shell uname -p) +ifeq ($(UNAME_S),Darwin) + NATIVE = darwin + GOARCH = 386 +endif + +GOARCH = amd64 +ifneq ($(UNAME_P), x86_64) + GOARCH = 386 +endif + +native: _build +native: + @mv $(OUT_D)/doctl_$(GOOS)_$(GOARCH) $(OUT_D)/doctl + @echo "built $(OUT_D)/doctl" + +_build: + @mkdir -p builds + @echo "building doctl" + @cd cmd/doctl && env GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o $(OUT_D)/doctl_$(GOOS)_$(GOARCH) + @echo "built $(OUT_D)/doctl_$(GOOS)_$(GOARCH)" + + +build_mac: GOOS = darwin +build_mac: GOARCH = 386 +build_mac: _build + +build_linux_386: GOARCH = 386 +build_linux_386: _build + +build_linux_amd64: GOARCH = amd64 +build_linux_amd64: _build + +clean: + @rm -rf builds + +_base_docker_cntr: + docker build -f Dockerfile.build . -t doctl_builder + +docker_build: _base_docker_cntr +docker_build: + @mkdir -p $(OUT_D) + @docker build -f Dockerfile.cntr . -t doctl_local + @docker run --rm \ + -v $(OUT_D):/copy \ + -it --entrypoint /usr/bin/rsync \ + doctl_local -av /app/ /copy/ + @docker run --rm \ + -v $(OUT_D):/copy \ + -it --entrypoint /bin/chown \ + alpine -R $(shell whoami | id -u): /copy + @echo "Built binaries to $(OUT_D)" + @echo "Created a local Docker container. To use, run: docker run --rm -it doctl_local"