From 925998b26e3fb8efab9f3ac2852ca683fc6c6ce4 Mon Sep 17 00:00:00 2001 From: Dominic Breuker Date: Thu, 5 Apr 2018 08:47:43 +0200 Subject: [PATCH] add build command for small binaries --- .gitignore | 3 +++ Makefile | 38 ++++++++++++++++++++++++++++++++++---- docker/Dockerfile.build | 3 +++ 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 docker/Dockerfile.build diff --git a/.gitignore b/.gitignore index f1c181e..3faa7c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +bin/* + + # Binaries for programs and plugins *.exe *.exe~ diff --git a/Makefile b/Makefile index 1dc3edf..95043b8 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,26 @@ PROJECT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) +BUILD_IMAGE = local/pspy-build:latest +BUILD_DOCKERFILE = $(PROJECT_DIR)/docker/Dockerfile.build + DEV_IMAGE = local/pspy-development:latest DEV_DOCKERFILE = $(PROJECT_DIR)/docker/Dockerfile.development TEST_IMAGE = local/pspy-testing:latest TEST_DOCKERFILE = $(PROJECT_DIR)/docker/Dockerfile.testing +# Run unit test and integration test inside container test: docker build -f $(TEST_DOCKERFILE) -t $(TEST_IMAGE) . docker run -it --rm $(TEST_IMAGE) +# Build Docker image for development +# pspy has to run on Linux - use this if you develop on another OS dev-build: docker build -f $(DEV_DOCKERFILE) -t $(DEV_IMAGE) . +# Drops you into a shell in the development container and mounts the source code +# You can edit to source on your host, then run go commans (e.g., `go test ./...`) inside the container dev: docker run -it \ --rm \ @@ -23,13 +31,21 @@ dev: EXAMPLE_IMAGE = local/pspy-example:latest EXAMPLE_DOCKERFILE = $(PROJECT_DIR)/docker/Dockerfile.example +# Run the example demonstrating what pspy does example: docker build -t $(EXAMPLE_IMAGE) -f $(EXAMPLE_DOCKERFILE) . docker run -it --rm $(EXAMPLE_IMAGE) -BUILD_IMAGE = golang:1.10-alpine -release: +build-build-image: + docker build -f $(BUILD_DOCKERFILE) -t $(BUILD_IMAGE) . + +# Build different binaries +# builds binaries for both 32bit and 64bit systems +# builds one set of static binaries that should work on any system without dependencies, but are huge +# builds another set of binaries that are as small as possible, but may not work +build: + mkdir -p $(PROJECT_DIR)/bin docker run -it \ --rm \ -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \ @@ -37,7 +53,7 @@ release: --env CGO_ENABLED=0 \ --env GOOS=linux \ --env GOARCH=386 \ - $(BUILD_IMAGE) go build -a -ldflags '-extldflags "-static"' -o pspy/bin/pspy32 pspy/main.go + $(BUILD_IMAGE) /bin/sh -c "go build -a -ldflags '-extldflags \"-static\"' -o pspy/bin/pspy32 pspy/main.go" docker run -it \ --rm \ -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \ @@ -45,4 +61,18 @@ release: --env CGO_ENABLED=0 \ --env GOOS=linux \ --env GOARCH=amd64 \ - $(BUILD_IMAGE) go build -a -ldflags '-extldflags "-static"' -o pspy/bin/pspy64 pspy/main.go + $(BUILD_IMAGE) /bin/sh -c "go build -a -ldflags '-extldflags \"-static\"' -o pspy/bin/pspy64 pspy/main.go" + docker run -it \ + --rm \ + -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \ + -w "/go/src/github.com/dominicbreuker" \ + --env GOOS=linux \ + --env GOARCH=386 \ + $(BUILD_IMAGE) /bin/sh -c "go build -ldflags '-w -s' -o pspy/bin/pspy32s pspy/main.go && upx pspy/bin/pspy32s" + docker run -it \ + --rm \ + -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \ + -w "/go/src/github.com/dominicbreuker" \ + --env GOOS=linux \ + --env GOARCH=amd64 \ + $(BUILD_IMAGE) /bin/sh -c "go build -ldflags '-w -s' -o pspy/bin/pspy64s pspy/main.go && upx pspy/bin/pspy64s" diff --git a/docker/Dockerfile.build b/docker/Dockerfile.build new file mode 100644 index 0000000..0f7988a --- /dev/null +++ b/docker/Dockerfile.build @@ -0,0 +1,3 @@ +FROM golang:1.10-alpine + +RUN apk add upx --no-cache