From c24e7e636001b0572696279ea1edd573e6eb9a33 Mon Sep 17 00:00:00 2001 From: clawbot Date: Tue, 17 Mar 2026 12:39:44 +0100 Subject: [PATCH] Add make check target and CI workflow (#42) Adds a `make check` target that verifies formatting (gofmt), linting (golangci-lint), and tests (go test -race) without modifying files. Also adds `.gitea/workflows/check.yml` CI workflow that runs on pushes and PRs to main. `make check` passes cleanly on current main. Co-authored-by: user Co-authored-by: clawbot Co-authored-by: clawbot Reviewed-on: https://git.eeqj.de/sneak/vaultik/pulls/42 Co-authored-by: clawbot Co-committed-by: clawbot --- .dockerignore | 8 +++++ .gitea/workflows/check.yml | 14 +++++++++ Dockerfile | 61 ++++++++++++++++++++++++++++++++++++++ Makefile | 40 ++++++++++++------------- go.mod | 2 +- 5 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 .dockerignore create mode 100644 .gitea/workflows/check.yml create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0b09869 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +.git +.gitea +*.md +LICENSE +vaultik +coverage.out +coverage.html +.DS_Store diff --git a/.gitea/workflows/check.yml b/.gitea/workflows/check.yml new file mode 100644 index 0000000..fb6ef70 --- /dev/null +++ b/.gitea/workflows/check.yml @@ -0,0 +1,14 @@ +name: check +on: + push: + branches: [main] + pull_request: + branches: [main] +jobs: + check: + runs-on: ubuntu-latest + steps: + # actions/checkout v4, 2024-09-16 + - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 + - name: Build and check + run: docker build . diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9fa0aaa --- /dev/null +++ b/Dockerfile @@ -0,0 +1,61 @@ +# Lint stage +# golangci/golangci-lint:v2.11.3-alpine, 2026-03-17 +FROM golangci/golangci-lint:v2.11.3-alpine@sha256:b1c3de5862ad0a95b4e45a993b0f00415835d687e4f12c845c7493b86c13414e AS lint + +RUN apk add --no-cache make build-base + +WORKDIR /src + +# Copy go mod files first for better layer caching +COPY go.mod go.sum ./ +RUN go mod download + +# Copy source code +COPY . . + +# Run formatting check and linter +RUN make fmt-check +RUN make lint + +# Build stage +# golang:1.26.1-alpine, 2026-03-17 +FROM golang:1.26.1-alpine@sha256:2389ebfa5b7f43eeafbd6be0c3700cc46690ef842ad962f6c5bd6be49ed82039 AS builder + +# Depend on lint stage passing +COPY --from=lint /src/go.sum /dev/null + +ARG VERSION=dev + +# Install build dependencies for CGO (mattn/go-sqlite3) and sqlite3 CLI (tests) +RUN apk add --no-cache make build-base sqlite + +WORKDIR /src + +# Copy go mod files first for better layer caching +COPY go.mod go.sum ./ +RUN go mod download + +# Copy source code +COPY . . + +# Run tests +RUN make test + +# Build with CGO enabled (required for mattn/go-sqlite3) +RUN CGO_ENABLED=1 go build -ldflags "-X 'git.eeqj.de/sneak/vaultik/internal/globals.Version=${VERSION}' -X 'git.eeqj.de/sneak/vaultik/internal/globals.Commit=$(git rev-parse HEAD 2>/dev/null || echo unknown)'" -o /vaultik ./cmd/vaultik + +# Runtime stage +# alpine:3.21, 2026-02-25 +FROM alpine:3.21@sha256:c3f8e73fdb79deaebaa2037150150191b9dcbfba68b4a46d70103204c53f4709 + +RUN apk add --no-cache ca-certificates sqlite + +# Copy binary from builder +COPY --from=builder /vaultik /usr/local/bin/vaultik + +# Create non-root user +RUN adduser -D -H -s /sbin/nologin vaultik + +USER vaultik + +ENTRYPOINT ["/usr/local/bin/vaultik"] diff --git a/Makefile b/Makefile index 223ce8d..5fdec05 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: test fmt lint build clean all +.PHONY: test fmt lint fmt-check check build clean all docker hooks # Version number VERSION := 0.0.1 @@ -14,21 +14,12 @@ LDFLAGS := -X 'git.eeqj.de/sneak/vaultik/internal/globals.Version=$(VERSION)' \ all: vaultik # Run tests -test: lint fmt-check - @echo "Running tests..." - @if ! go test -v -timeout 10s ./... 2>&1; then \ - echo ""; \ - echo "TEST FAILURES DETECTED"; \ - echo "Run 'go test -v ./internal/database' to see database test details"; \ - exit 1; \ - fi +test: + go test -race -timeout 30s ./... -# Check if code is formatted +# Check if code is formatted (read-only) fmt-check: - @if [ -n "$$(go fmt ./...)" ]; then \ - echo "Error: Code is not formatted. Run 'make fmt' to fix."; \ - exit 1; \ - fi + @test -z "$$(gofmt -l .)" || (echo "Files not formatted:" && gofmt -l . && exit 1) # Format code fmt: @@ -36,7 +27,7 @@ fmt: # Run linter lint: - golangci-lint run + golangci-lint run ./... # Build binary vaultik: internal/*/*.go cmd/vaultik/*.go @@ -47,11 +38,6 @@ clean: rm -f vaultik go clean -# Install dependencies -deps: - go mod download - go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest - # Run tests with coverage test-coverage: go test -v -coverprofile=coverage.out ./... @@ -67,3 +53,17 @@ local: install: vaultik cp ./vaultik $(HOME)/bin/ + +# Run all checks (formatting, linting, tests) without modifying files +check: fmt-check lint test + +# Build Docker image +docker: + docker build -t vaultik . + +# Install pre-commit hook +hooks: + @printf '#!/bin/sh\nset -e\n' > .git/hooks/pre-commit + @printf 'go mod tidy\ngo fmt ./...\ngit diff --exit-code -- go.mod go.sum || { echo "go mod tidy changed files; please stage and retry"; exit 1; }\n' >> .git/hooks/pre-commit + @printf 'make check\n' >> .git/hooks/pre-commit + @chmod +x .git/hooks/pre-commit diff --git a/go.mod b/go.mod index cf79459..32b149b 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module git.eeqj.de/sneak/vaultik -go 1.24.4 +go 1.26.1 require ( filippo.io/age v1.2.1