From e11e410eb608c883a11c6a11cfb0bf087e60ddf3 Mon Sep 17 00:00:00 2001 From: Andrew Rynhard Date: Sat, 12 Jan 2019 17:06:47 -0800 Subject: [PATCH] chore: use buildkit for builds Signed-off-by: Andrew Rynhard --- Dockerfile | 12 ++++--- Makefile | 88 +++++++++++++++++++++++----------------------------- brigade.js | 73 ------------------------------------------- hack/test.sh | 4 +-- 4 files changed, 47 insertions(+), 130 deletions(-) delete mode 100644 brigade.js diff --git a/Dockerfile b/Dockerfile index 45e8239d..809fcda6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ ARG GOLANG_IMAGE -FROM ${GOLANG_IMAGE} +FROM ${GOLANG_IMAGE} AS common ENV CGO_ENABLED 0 ENV GO111MODULES on @@ -11,26 +11,28 @@ 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" ] diff --git a/Makefile b/Makefile index 17b5d198..f7134c81 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/brigade.js b/brigade.js deleted file mode 100644 index 755d74c5..00000000 --- a/brigade.js +++ /dev/null @@ -1,73 +0,0 @@ -const { events, Job, Group } = require("brigadier"); - -events.on("exec", function (e, project) { - job = conform(e, project) - job.run().then(result => { - console.log(result.toString()) - }) -}) - -events.on("push", function (e, project) { - job = conform(e, project) - job.run().then(result => { - console.log(result.toString()) - }) -}) - -events.on("pull_request", function (e, project) { - start = notify("pending", `Build ${e.buildID} started`, e, project) - job = conform(e, project) - Group.runAll([start, job]) - .then(() => { - return notify("success", `Build ${e.buildID} passed`, e, project).run() - }).catch(err => { - return notify("failure", `Build ${e.buildID} failed`, e, project).run() - }); -}) - -function conform(e, project) { - var job = new Job("conform", "golang:1.11.1") - - job.env = { - // "DOCKER_HOST": "tcp://docker:2375" - "DOCKER_USERNAME": project.secrets.DOCKER_USERNAME, - "DOCKER_PASSWORD": project.secrets.DOCKER_PASSWORD, - "GO111MODULE": "on", - } - - job.tasks = [ - "apt-get update", - "apt-get -y install apt-transport-https ca-certificates curl software-properties-common", - "curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -", - "add-apt-repository \"deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable\"", - "apt-get update", - "apt-get -y install docker-ce=18.06.1~ce~3-0~debian", - "cd /src", - "go install .", - "conform enforce", - "conform build", - ] - - job.docker.enabled = true - - // Unit is milliseconds, 900000ms = 15m. - job.timeout = 900000 - - job.host.nodeSelector.set("node-role.kubernetes.io/ci", "") - - return job -} - -function notify(state, msg, e, project) { - const gh = new Job(`notify-${state}`, "technosophos/github-notify:latest") - gh.env = { - GH_REPO: project.repo.name, - GH_STATE: state, - GH_DESCRIPTION: msg, - GH_CONTEXT: "brigade", - GH_TOKEN: project.secrets.GH_TOKEN, - GH_COMMIT: e.revision.commit, - GH_TARGET_URL: `https://ci.dev.autonomy.io/builds/${e.buildID}`, - } - return gh -} diff --git a/hack/test.sh b/hack/test.sh index 407d9cbb..e4f6e3d2 100755 --- a/hack/test.sh +++ b/hack/test.sh @@ -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