From 4f2cc0e2207c7ce259f9f99e909a4d0c81345fbd Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Fri, 22 Mar 2019 13:29:28 -0700 Subject: [PATCH] hack: base build scripts Signed-off-by: Tonis Tiigi --- Dockerfile | 1 + gometalinter.json | 13 ++++++++ hack/dockerfiles/lint.Dockerfile | 10 ++++++ hack/dockerfiles/vendor.Dockerfile | 18 +++++++++++ hack/lint | 37 ++++++++++++++++++++++ hack/update-vendor | 45 +++++++++++++++++++++++++++ hack/util | 21 +++++++++++++ hack/validate-vendor | 49 ++++++++++++++++++++++++++++++ 8 files changed, 194 insertions(+) create mode 100644 Dockerfile create mode 100644 gometalinter.json create mode 100644 hack/dockerfiles/lint.Dockerfile create mode 100644 hack/dockerfiles/vendor.Dockerfile create mode 100755 hack/lint create mode 100755 hack/update-vendor create mode 100755 hack/util create mode 100755 hack/validate-vendor diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..c3c78df4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1 @@ +FROM alpine \ No newline at end of file diff --git a/gometalinter.json b/gometalinter.json new file mode 100644 index 00000000..e79b3f10 --- /dev/null +++ b/gometalinter.json @@ -0,0 +1,13 @@ +{ + "Vendor": true, + "Deadline": "8m", + "Exclude": [".*.pb.go"], + "DisableAll": true, + "Enable": [ + "gofmt", + "goimports", + "ineffassign", + "vet", + "deadcode" + ] +} diff --git a/hack/dockerfiles/lint.Dockerfile b/hack/dockerfiles/lint.Dockerfile new file mode 100644 index 00000000..9e2bd3e2 --- /dev/null +++ b/hack/dockerfiles/lint.Dockerfile @@ -0,0 +1,10 @@ +# syntax=docker/dockerfile:1.0-experimental + +FROM golang:1.12-alpine +RUN apk add --no-cache git +RUN go get -u gopkg.in/alecthomas/gometalinter.v1 \ + && mv /go/bin/gometalinter.v1 /go/bin/gometalinter \ + && gometalinter --install +WORKDIR /go/src/github.com/moby/buildkit +RUN --mount=target=/go/src/github.com/tonistiigi/buildx \ + gometalinter --config=gometalinter.json ./... diff --git a/hack/dockerfiles/vendor.Dockerfile b/hack/dockerfiles/vendor.Dockerfile new file mode 100644 index 00000000..6fd12e5d --- /dev/null +++ b/hack/dockerfiles/vendor.Dockerfile @@ -0,0 +1,18 @@ +# syntax = docker/dockerfile:1.0-experimental +FROM golang:1.12-alpine AS vendored +RUN apk add --no-cache git +WORKDIR /src +RUN --mount=target=/src,rw \ + --mount=target=/go/pkg/mod,type=cache \ + go mod tidy && go mod vendor && \ + mkdir /out && cp -r go.mod go.sum vendor /out + +FROM scratch AS update +COPY --from=vendored /out /out + +FROM vendored AS validate +RUN --mount=target=.,rw \ + git add -A && \ + rm -rf vendor && \ + cp -rf /out/* . && \ + ./hack/validate-vendor check diff --git a/hack/lint b/hack/lint new file mode 100755 index 00000000..dc4c690d --- /dev/null +++ b/hack/lint @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +. $(dirname $0)/util +set -eu -o pipefail -x + +: ${CONTINUOUS_INTEGRATION=} + +progressFlag="" +if [ "$CONTINUOUS_INTEGRATION" == "true" ]; then progressFlag="--progress=plain"; fi + +lintDocker() { + export DOCKER_BUILDKIT=1 + iidfile=$(mktemp -t docker-iidfile.XXXXXXXXXX) + docker build --iidfile $iidfile -f ./hack/dockerfiles/lint.Dockerfile --force-rm . + iid=$(cat $iidfile) + docker rmi $iid + rm -f $iidfile +} + +lint() { + buildctl build $progressFlag --frontend=dockerfile.v0 \ + --local context=. --local dockerfile=. \ + --frontend-opt filename=./hack/dockerfiles/lint.Dockerfile +} + +case $buildmode in +"buildkit") + lint + ;; +"docker-buildkit") + lintDocker + ;; +*) + echo "buildctl or docker with buildkit support is required" + exit 1 + ;; +esac diff --git a/hack/update-vendor b/hack/update-vendor new file mode 100755 index 00000000..29aae687 --- /dev/null +++ b/hack/update-vendor @@ -0,0 +1,45 @@ +#!/usr/bin/env bash + +. $(dirname $0)/util +set -eu -o pipefail -x + +: ${CONTINUOUS_INTEGRATION=} + +progressFlag="" +if [ "$CONTINUOUS_INTEGRATION" == "true" ]; then progressFlag="--progress=plain"; fi + +case $buildmode in +"buildkit") + output=$(mktemp -d -t buildctl-output.XXXXXXXXXX) + buildctl build $progressFlag --frontend=dockerfile.v0 --local context=. --local dockerfile=. \ + --frontend-opt target=update \ + --frontend-opt filename=./hack/dockerfiles/vendor.Dockerfile \ + --output type=local,dest=$output + rm -rf ./vendor + cp -R "$output/out/" . + rm -rf $output + ;; +*) + iidfile=$(mktemp -t docker-iidfile.XXXXXXXXXX) + case $buildmode in + "docker-buildkit") + export DOCKER_BUILDKIT=1 + docker build --iidfile $iidfile -f ./hack/dockerfiles/vendor.Dockerfile --target update --force-rm . + ;; + *) + echo "buildctl or docker with buildkit support is required" + exit 1 + ;; + esac + iid=$(cat $iidfile) + cid=$(docker create $iid noop) + rm -rf ./vendor + + docker cp $cid:/out/go.mod . + docker cp $cid:/out/go.sum . + docker cp $cid:/out/vendor . + + docker rm $cid + rm -f $iidfile + ;; +esac diff --git a/hack/util b/hack/util new file mode 100755 index 00000000..2d11817a --- /dev/null +++ b/hack/util @@ -0,0 +1,21 @@ +#!/usr/bin/env sh + +: ${PREFER_BUILDCTL=} +: ${PREFER_LEGACY=} + +newerEqualThan() { # $1=minimum wanted version $2=actual-version + [ "$1" = "$(printf "$1\n$2" | sort -V | head -n 1)" ] +} + +buildmode="legacy" +if [ "$PREFER_BUILDCTL" = "1" ]; then + buildmode="buildkit"; +else + serverVersion=$(docker info --format '{{.ServerVersion}}') + experimental=$(docker info --format '{{.ExperimentalBuild}}') + if [ "$PREFER_LEGACY" != "1" ] && ( newerEqualThan "18.09" $serverVersion || \ + ( newerEqualThan "18.06" $serverVersion && [ "true" = "$experimental" ] ) || \ + [ "$DOCKER_BUILDKIT" = "1" ]); then + buildmode="docker-buildkit"; + fi +fi diff --git a/hack/validate-vendor b/hack/validate-vendor new file mode 100755 index 00000000..5bcec4d0 --- /dev/null +++ b/hack/validate-vendor @@ -0,0 +1,49 @@ +#!/usr/bin/env sh + +set -eu + +: ${CONTINUOUS_INTEGRATION=} +: ${DOCKER_BUILDKIT=} + +progressFlag="" +if [ "$CONTINUOUS_INTEGRATION" = "true" ]; then progressFlag="--progress=plain"; fi + +case ${1:-} in +'') + . $(dirname $0)/util + case $buildmode in + "buildkit") + buildctl build $progressFlag --frontend=dockerfile.v0 --local context=. --local dockerfile=. --frontend-opt filename=./hack/dockerfiles/vendor.Dockerfile --frontend-opt target=validate + ;; + "docker-buildkit") + export DOCKER_BUILDKIT=1 + iidfile=$(mktemp -t docker-iidfile.XXXXXXXXXX) + docker build --iidfile $iidfile -f ./hack/dockerfiles/vendor.Dockerfile --target validate --force-rm . || exit 1 + iid=$(cat $iidfile) + docker rmi $iid + rm -f $iidfile + ;; + *) + echo "buildkit support is required" + exit 1 + ;; + esac + ;; +check) + status="$(git status --porcelain -- go.mod go.sum vendor 2>/dev/null)" + diffs=$(echo "$status" | grep -v '^[RAD] ' || true) + if [ "$diffs" ]; then + { + set +x + echo 'The result of "make vendor" differs' + echo + echo "$diffs" + echo + echo 'Please vendor your package with "make vendor"' + echo + } >&2 + exit 1 + fi + echo 'Congratulations! All vendoring changes are done the right way.' + ;; +esac