Skip to content

Commit

Permalink
chore: use buildkit for builds (#93)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Rynhard <[email protected]>
  • Loading branch information
andrewrynhard authored Jan 13, 2019
1 parent b59ae9c commit 36df035
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 131 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
13 changes: 7 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ARG GOLANG_IMAGE
FROM ${GOLANG_IMAGE}
FROM ${GOLANG_IMAGE} AS common

ENV CGO_ENABLED 0
ENV GO111MODULES on
Expand All @@ -9,28 +9,29 @@ COPY ./ ./
RUN go mod download
RUN go mod verify
RUN go mod tidy
RUN go mod vendor

FROM common AS build
ARG TAG
ARG SHA
ARG BUILT
ENV GOOS linux
ENV GOARCH amd64
RUN go build -o /build/conform-${GOOS}-${GOARCH} -ldflags "-s -w -X \"github.com/autonomy/conform/cmd.Tag=${TAG}\" -X \"github.com/autonomy/conform/cmd.SHA=${SHA}\" -X \"github.com/autonomy/conform/cmd.Built=${BUILT}\"" .
RUN go build -o /conform-${GOOS}-${GOARCH} -ldflags "-s -w -X \"github.com/autonomy/conform/cmd.Tag=${TAG}\" -X \"github.com/autonomy/conform/cmd.SHA=${SHA}\" -X \"github.com/autonomy/conform/cmd.Built=${BUILT}\"" .

ARG TAG
ARG SHA
ARG BUILT
ENV GOOS darwin
ENV GOARCH amd64
RUN go build -o /build/conform-${GOOS}-${GOARCH} -ldflags "-s -w -X \"github.com/autonomy/conform/cmd.Tag=${TAG}\" -X \"github.com/autonomy/conform/cmd.SHA=${SHA}\" -X \"github.com/autonomy/conform/cmd.Built=${BUILT}\"" .
RUN go build -o /conform-${GOOS}-${GOARCH} -ldflags "-s -w -X \"github.com/autonomy/conform/cmd.Tag=${TAG}\" -X \"github.com/autonomy/conform/cmd.SHA=${SHA}\" -X \"github.com/autonomy/conform/cmd.Built=${BUILT}\"" .

FROM common AS test
ENV GOOS linux
ENV GOARCH amd64
COPY ./hack ./hack
RUN chmod +x ./hack/test.sh
RUN ./hack/test.sh --all

FROM scratch
COPY /build/conform-linux-amd64 /conform
FROM scratch AS image
COPY --from=build /conform-linux-amd64 /conform
ENTRYPOINT [ "/conform" ]
88 changes: 38 additions & 50 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,59 +1,47 @@
REPO ?= docker.io/autonomy
EXECUTOR ?= gcr.io/kaniko-project/executor
EXECUTOR_TAG ?= v0.6.0
WARMER ?= gcr.io/kaniko-project/warmer
WARMER_TAG ?= v0.6.0
GOLANG_IMAGE ?= golang:1.11.2
AUTH_CONFIG ?= $(HOME)/.kaniko/config.json

SHA := $(shell gitmeta git sha)
TAG := $(shell gitmeta image tag)
BUILT := $(shell gitmeta built)
PUSH := $(shell gitmeta image pushable --negate)

EXECUTOR_ARGS := --context=/workspace --cache=true --cache-dir=/cache --cleanup
EXECUTOR_VOLUMES := --volume $(AUTH_CONFIG):/kaniko/.docker/config.json:ro --volume $(PWD)/cache:/cache --volume $(PWD)/build:/build
GOLANG_IMAGE ?= golang:1.11.4

COMMON_ARGS := -f ./Dockerfile --build-arg GOLANG_IMAGE=$(GOLANG_IMAGE) --build-arg SHA=$(SHA) --build-arg TAG=$(TAG) --build-arg BUILT="$(BUILT)" .

export DOCKER_BUILDKIT := 1

all: enforce clean conform
all: enforce build test image

enforce:
conform enforce

conform: cache
docker run \
--rm \
$(EXECUTOR_VOLUMES) \
--volume $(PWD):/workspace \
$(EXECUTOR):$(EXECUTOR_TAG) \
$(EXECUTOR_ARGS) \
--dockerfile=Dockerfile \
--cache-repo=$(REPO)/$@ \
--destination=$(REPO)/$@:$(TAG) \
--destination=$(REPO)/$@:latest \
--single-snapshot \
--no-push=$(PUSH) \
--build-arg GOLANG_IMAGE=$(GOLANG_IMAGE) \
--build-arg SHA=$(SHA) \
--build-arg TAG=$(TAG) \
--build-arg BUILT="$(BUILT)"

.PHONY: cache
cache:
docker run \
--rm \
$(EXECUTOR_VOLUMES) \
$(WARMER):$(WARMER_TAG) \
--cache-dir=/cache \
--image=$(GOLANG_IMAGE)

debug:
docker run \
--rm \
-it \
$(EXECUTOR_VOLUMES) \
--volume $(PWD):/workspace \
--entrypoint=/busybox/sh \
$(EXECUTOR):debug-${EXECUTOR_TAG}
@conform enforce

.PHONY: build
build:
@docker build \
-t conform/$@:$(SHA) \
--target=$@ \
$(COMMON_ARGS)
@docker run --rm -it -v $(PWD)/build:/build conform/$@:$(SHA) cp /conform-linux-amd64 /build
@docker run --rm -it -v $(PWD)/build:/build conform/$@:$(SHA) cp /conform-darwin-amd64 /build

test:
@docker build \
-t conform/$@:$(SHA) \
--target=$@ \
$(COMMON_ARGS)
@docker run --rm -it -v $(PWD)/build:/build conform/$@:$(SHA) cp /coverage.txt /build

image: build
@docker build \
-t autonomy/conform:$(SHA) \
--target=$@ \
$(COMMON_ARGS)

push: image
@docker push autonomy/conform:$(SHA)

deps:
@GO111MODULES=on CGO_ENABLED=0 go get -u github.com/autonomy/gitmeta
@GO111MODULES=on CGO_ENABLED=0 go get -u github.com/autonomy/conform

clean:
rm -rf ./build
go clean -modcache
rm -rf build vendor
73 changes: 0 additions & 73 deletions brigade.js

This file was deleted.

4 changes: 2 additions & 2 deletions hack/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ go_test() {
coverage_tests() {
if [ "${coverage}" = true ]; then
echo "performing coverage tests"
local coverage_report="../build/coverage.txt"
local profile="../build/profile.out"
local coverage_report="/coverage.txt"
local profile="/profile.out"
if [[ -f ${coverage_report} ]]; then
rm ${coverage_report}
fi
Expand Down

0 comments on commit 36df035

Please sign in to comment.