From 1a905ab966549bf9ce80494a36728c497dcaea9e Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Wed, 20 Jul 2022 14:18:18 +0200 Subject: [PATCH] ci: git validation target Signed-off-by: CrazyMax --- .github/workflows/validate.yml | 7 ++++--- Makefile | 5 ++++- docker-bake.hcl | 15 ++++++++++++++- dockerfiles/git.Dockerfile | 23 +++++++++++++++++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 dockerfiles/git.Dockerfile diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 9a5b4dec2..73e953afa 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -8,8 +8,6 @@ on: tags: - 'v*' pull_request: - branches: - - '*' jobs: validate: @@ -20,11 +18,14 @@ jobs: target: - lint - validate-vendor + - validate-git steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Run run: | make ${{ matrix.target }} + env: + COMMIT_RANGE: ${{ format('{0}..{1}', github.sha, 'HEAD') }} diff --git a/Makefile b/Makefile index 2f58f44f7..42db005ed 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ BINARIES=$(addprefix bin/,$(COMMANDS)) TESTFLAGS ?= -v $(TESTFLAGS_RACE) TESTFLAGS_PARALLEL ?= 8 -.PHONY: all build binaries clean test test-race test-full integration coverage validate lint validate-vendor vendor mod-outdated +.PHONY: all build binaries clean test test-race test-full integration coverage validate lint validate-git validate-vendor vendor mod-outdated .DEFAULT: all all: binaries @@ -103,6 +103,9 @@ validate: ## run all validators lint: ## run all linters docker buildx bake $@ +validate-git: ## validate git + docker buildx bake $@ + validate-vendor: ## validate vendor docker buildx bake $@ diff --git a/docker-bake.hcl b/docker-bake.hcl index 4b776fccc..db9fa1f51 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -3,7 +3,7 @@ group "default" { } group "validate" { - targets = ["lint", "validate-vendor"] + targets = ["lint", "validate-git", "validate-vendor"] } target "lint" { @@ -11,6 +11,19 @@ target "lint" { output = ["type=cacheonly"] } +variable "COMMIT_RANGE" { + default = "" +} +target "validate-git" { + dockerfile = "./dockerfiles/git.Dockerfile" + target = "validate" + args = { + COMMIT_RANGE = COMMIT_RANGE + BUILDKIT_CONTEXT_KEEP_GIT_DIR = 1 + } + output = ["type=cacheonly"] +} + target "validate-vendor" { dockerfile = "./dockerfiles/vendor.Dockerfile" target = "validate" diff --git a/dockerfiles/git.Dockerfile b/dockerfiles/git.Dockerfile new file mode 100644 index 000000000..1d217eff2 --- /dev/null +++ b/dockerfiles/git.Dockerfile @@ -0,0 +1,23 @@ +# syntax=docker/dockerfile:1 + +ARG GO_VERSION=1.18 +ARG ALPINE_VERSION=3.16 + +FROM alpine:${ALPINE_VERSION} AS base +RUN apk add --no-cache git gpg + +FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS gitvalidation +ARG GIT_VALIDATION_VERSION=v1.1.0 +RUN --mount=type=cache,target=/root/.cache \ + --mount=type=cache,target=/go/pkg/mod \ + GOBIN=/out go install "github.com/vbatts/git-validation@${GIT_VALIDATION_VERSION}" + +FROM base AS validate +ARG COMMIT_RANGE +RUN if [ -z "$COMMIT_RANGE" ]; then echo "COMMIT_RANGE required" && exit 1; fi +ENV GIT_CHECK_EXCLUDE="./vendor" +WORKDIR /src +RUN --mount=type=bind,target=. \ + --mount=type=cache,target=/root/.cache \ + --mount=from=gitvalidation,source=/out/git-validation,target=/usr/bin/git-validation \ + git-validation -q -range "$COMMIT_RANGE" -run short-subject,dangling-whitespace