From e7a65571c872265a4e596f8ebbcefddcec7c4988 Mon Sep 17 00:00:00 2001 From: Ruslan Popov Date: Wed, 24 Dec 2025 11:31:52 +0300 Subject: [PATCH] Align build tooling --- Dockerfile | 23 +++++++++++++++++------ Makefile | 50 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 57 insertions(+), 16 deletions(-) diff --git a/Dockerfile b/Dockerfile index d0090f6..dcb72c6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,25 @@ -FROM golang:1.24.1-alpine AS builder +FROM golang:1.24-alpine AS builder + +ARG SOURCE_VERSION +ARG SOURCE_COMMIT +ARG GOPROXY +ARG GONOSUMDB +ENV GOPROXY=${GOPROXY} +ENV GONOSUMDB=${GONOSUMDB} + +RUN go env GOPROXY +RUN test -n "$GOPROXY" || (echo "GOPROXY not set" && exit 1) WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bumpversion ./src +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ + -ldflags "-X main.AppVersion=${SOURCE_VERSION} -X main.AppCommit=${SOURCE_COMMIT}" \ + -o bumpversion ./src FROM gcr.io/distroless/static - -WORKDIR / -COPY --from=builder /app/bumpversion . -ENTRYPOINT ["/bumpversion"] +WORKDIR /app +COPY --from=builder /app/bumpversion /app/bumpversion +ENTRYPOINT ["/app/bumpversion"] CMD ["--version"] diff --git a/Makefile b/Makefile index d2913d2..5bc6bfa 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,12 @@ PACKAGE := bumpversion SHELL := /bin/bash REGISTRY := registry.halfakop.ru REPOSITORY := $(NAMESPACE)/$(PACKAGE) +PLATFORM ?= --platform=linux/amd64 SOURCE_VERSION ?= $(shell cat VERSION) SOURCE_COMMIT ?= $(shell git rev-parse --short=8 HEAD) GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD | sed s,feature/,,g) +LDFLAGS := -X main.AppVersion=$(SOURCE_VERSION) -X main.AppCommit=$(SOURCE_COMMIT) IMAGE_NAME_TAGGED = $(REPOSITORY):$(SOURCE_VERSION) EXEC=$(PACKAGE).run @@ -14,34 +16,62 @@ EXEC=$(PACKAGE).run all: help help: - @echo "app - build the application" - @echo "tests - run tests" - @echo "run - run application locally" - @echo "clean - clean build environment" + @printf "\nMain make targets:\n" + @printf " make app - Download deps, fix, build app (binary: %s)\n" "$(EXEC)" + @printf " make test - Run unit tests\n" + @printf " make test-integration - Run tests that require Postgres (testcontainers)\n" + @printf " make run - Build then run locally\n" + @printf " make clean - Clean build artifacts\n" + @printf " make release - Clean, build image, login, push\n" + @printf "\nVariables:\n" + @printf " NAMESPACE=%s\n" "$(NAMESPACE)" + @printf " PACKAGE=%s\n" "$(PACKAGE)" + @printf " IMAGE_NAME_TAGGED=%s\n" "$(IMAGE_NAME_TAGGED)" + @printf " EXEC=%s\n\n" "$(EXEC)" + +download: + @echo "Download dependencies" + @go mod download fix: + @echo "Fix code" @go fix ./... -app: fix - @go build -o ./${EXEC} ./src +app: download fix + @echo "Build application" + @go build -ldflags "$(LDFLAGS)" -o ./${EXEC} ./src -tests: build +tests: app + @echo "Run tests" @go test ./... +test: + @echo "Run unit tests" + @go test -count=1 ./... + +test-integration: + @echo "Run integration tests (requires Docker for Postgres)" + @go test -tags=integration -count=1 ./... + run: + @echo "Run application" @./${EXEC} clean: + @echo "Clean build environment" @rm -rf ./${EXEC}% -release: title clean build login push +release: clean build login push build: - docker build --compress \ + DOCKER_BUILDKIT=0 \ + docker build $(PLATFORM) --progress=plain --compress \ -t $(IMAGE_NAME_TAGGED) \ -t $(REGISTRY)/$(IMAGE_NAME_TAGGED) \ --build-arg SOURCE_VERSION=$(SOURCE_VERSION) \ --build-arg SOURCE_COMMIT=$(SOURCE_COMMIT) \ + --build-arg GOPROXY=$(GOPROXY) \ + --build-arg GONOSUMDB=$(GONOSUMDB) \ ${DOCKER_OPTS} \ -f Dockerfile . @@ -54,4 +84,4 @@ login: push: docker push $(REGISTRY)/$(IMAGE_NAME_TAGGED) -.PHONY: tests release build login push \ No newline at end of file +.PHONY: tests test test-integration release build login push download fix app clean run help