From 5f9d3f818c605c67b4e9ecb1d0295e420eace40f Mon Sep 17 00:00:00 2001 From: Cedric Meury Date: Fri, 27 Apr 2018 11:11:35 +0200 Subject: [PATCH 1/3] build consistent statically linked binary for docker image * use golang 1.10.1 images everywhere * introduce a `static-linux` target to build a statically linked 64-bit binary only * use that binary for the official image and the circle build and tests * use a multi-stage dockerfile for a consistent build environment --- .circleci/config.yml | 9 ++++----- Dockerfile | 15 ++++++++++++++- Makefile | 6 +++++- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 94be15a4..cbd424bc 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -4,11 +4,11 @@ jobs: build: docker: - - image: circleci/golang:1.10 + - image: circleci/golang:1.10.1 working_directory: /go/src/github.com/roboll/helmfile steps: - checkout - - run: make build + - run: make static-linux - persist_to_workspace: root: /go/src/github.com/roboll/helmfile paths: @@ -16,7 +16,7 @@ jobs: test: docker: - - image: circleci/golang:1.10 + - image: circleci/golang:1.10.1 working_directory: /go/src/github.com/roboll/helmfile steps: - checkout @@ -76,7 +76,7 @@ jobs: release: docker: - - image: circleci/golang:1.10 + - image: circleci/golang:1.10.1 working_directory: /go/src/github.com/roboll/helmfile steps: - checkout @@ -84,7 +84,6 @@ jobs: - run: command: | docker login -u="$DOCKER_USER" -p="$DOCKER_PASS" quay.io - make tools BUILD_URL="$CIRCLE_BUILD_URL" make push release workflows: diff --git a/Dockerfile b/Dockerfile index 656b39bc..87f84778 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,16 @@ +FROM golang:1.10.1-alpine3.7 as builder + +RUN apk add --no-cache make git +WORKDIR /go/src/github.com/roboll/helmfile/ + +COPY Makefile /go/src/github.com/roboll/helmfile/Makefile +RUN make tools + +COPY . /go/src/github.com/roboll/helmfile/ +RUN make static-linux + +# ----------------------------------------------------------------------------- + FROM alpine:3.7 RUN apk add --no-cache ca-certificates @@ -11,6 +24,6 @@ RUN wget ${HELM_LOCATION}/${HELM_FILENAME} && \ tar zxf ${HELM_FILENAME} && mv /linux-amd64/helm /usr/local/bin/ && \ rm ${HELM_FILENAME} && rm -r /linux-amd64 -COPY dist/helmfile_linux_amd64 /usr/local/bin/helmfile +COPY --from=builder /go/src/github.com/roboll/helmfile/dist/helmfile_linux_amd64 /usr/local/bin/helmfile CMD ["/usr/local/bin/helmfile"] diff --git a/Makefile b/Makefile index 60ffd1f2..c62bb7e0 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,10 @@ cross: gox -os '!freebsd !netbsd' -arch '!arm' -output "dist/{{.Dir}}_{{.OS}}_{{.Arch}}" -ldflags '-X main.Version=${TAG}' ${TARGETS} .PHONY: cross +static-linux: + env CGO_ENABLED=0 gox -osarch="linux/amd64" -output "dist/{{.Dir}}_{{.OS}}_{{.Arch}}" -ldflags '-X main.Version=${TAG}' ${TARGETS} +.PHONY: linux + clean: rm dist/helmfile_* .PHONY: clean @@ -41,7 +45,7 @@ release: pristine cross @ghr -b ${BODY} -t ${GITHUB_TOKEN} -u ${ORG} -recreate ${TAG} dist .PHONY: release -image: cross +image: docker build -t quay.io/${ORG}/helmfile:${TAG} . run: image From 2af039dcfc083bacccd7efff38b5c7ed82ca8c4b Mon Sep 17 00:00:00 2001 From: Cedric Meury Date: Fri, 27 Apr 2018 11:20:18 +0200 Subject: [PATCH 2/3] don't use gox for single build --- Dockerfile | 4 ---- Makefile | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 87f84778..54fbd16d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,10 +2,6 @@ FROM golang:1.10.1-alpine3.7 as builder RUN apk add --no-cache make git WORKDIR /go/src/github.com/roboll/helmfile/ - -COPY Makefile /go/src/github.com/roboll/helmfile/Makefile -RUN make tools - COPY . /go/src/github.com/roboll/helmfile/ RUN make static-linux diff --git a/Makefile b/Makefile index c62bb7e0..a940657d 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ cross: .PHONY: cross static-linux: - env CGO_ENABLED=0 gox -osarch="linux/amd64" -output "dist/{{.Dir}}_{{.OS}}_{{.Arch}}" -ldflags '-X main.Version=${TAG}' ${TARGETS} + env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o "dist/helmfile_linux_amd64" -ldflags '-X main.Version=${TAG}' ${TARGETS} .PHONY: linux clean: From 151fe0716e2b84ec01ce68ff1fed91795710d64a Mon Sep 17 00:00:00 2001 From: Cedric Meury Date: Fri, 27 Apr 2018 11:21:43 +0200 Subject: [PATCH 3/3] keep it simple --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index cbd424bc..43b027fb 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -8,7 +8,7 @@ jobs: working_directory: /go/src/github.com/roboll/helmfile steps: - checkout - - run: make static-linux + - run: make build - persist_to_workspace: root: /go/src/github.com/roboll/helmfile paths: