From 6411d3182f39a8016acc78fd4f7c9f901a2efec6 Mon Sep 17 00:00:00 2001 From: Nicolas Vanheuverzwijn Date: Fri, 2 Oct 2020 11:25:40 -0400 Subject: [PATCH 1/2] makefile: add LDFLAGS environment variable. LDFLAGS default to empty string ("") --- Makefile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 1a06482e..075256b8 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ OS = darwin freebsd linux openbsd ARCHS = 386 arm amd64 arm64 +LDFLAGS = .DEFAULT_GOAL := help @@ -10,7 +11,7 @@ help: all: build release release-windows build: deps ## Build the project - go build + go build -ldflags="$(LDFLAGS)" release: clean deps ## Generate releases for unix systems @for arch in $(ARCHS);\ @@ -19,7 +20,7 @@ release: clean deps ## Generate releases for unix systems do \ echo "Building $$os-$$arch"; \ mkdir -p build/webhook-$$os-$$arch/; \ - GOOS=$$os GOARCH=$$arch go build -o build/webhook-$$os-$$arch/webhook; \ + GOOS=$$os GOARCH=$$arch go build -ldflags="$(LDFLAGS)" -o build/webhook-$$os-$$arch/webhook; \ tar cz -C build -f build/webhook-$$os-$$arch.tar.gz webhook-$$os-$$arch; \ done \ done @@ -29,7 +30,7 @@ release-windows: clean deps ## Generate release for windows do \ echo "Building windows-$$arch"; \ mkdir -p build/webhook-windows-$$arch/; \ - GOOS=windows GOARCH=$$arch go build -o build/webhook-windows-$$arch/webhook.exe; \ + GOOS=windows GOARCH=$$arch go build -ldflags="$(LDFLAGS)" -o build/webhook-windows-$$arch/webhook.exe; \ tar cz -C build -f build/webhook-windows-$$arch.tar.gz webhook-windows-$$arch; \ done From f1c1110f2064fcf66c0347f0fcbf31eb0386d00b Mon Sep 17 00:00:00 2001 From: Nicolas Vanheuverzwijn Date: Fri, 2 Oct 2020 11:26:21 -0400 Subject: [PATCH 2/2] docker: add Dockerfile and .dockerignore. The Dockerfile produces a scratch image with a statically linked binary without debug flag. --- .dockerignore | 6 ++++++ Dockerfile | 8 ++++++++ 2 files changed, 14 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..956a9c52 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +* +!go.mod +!go.sum +!*.go +!Makefile +!internal/* diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..e2be7220 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM golang:1.15 AS builder +WORKDIR /go/src/github.com/adnanh/webhook/ +COPY . ./ +RUN make CGO_ENABLED=0 LDFLAGS="-w -s" build + +FROM scratch +COPY --from=builder /go/src/github.com/adnanh/webhook/webhook /bin/ +ENTRYPOINT ["/bin/webhook"]