-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
59 lines (51 loc) · 2.87 KB
/
Makefile
File metadata and controls
59 lines (51 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
.PHONY:help
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
CMDS=$(shell ls cmd)
COMMIT=$(git rev-parse --short HEAD)
REV=$(shell git describe --dirty 2>/dev/null || echo "dev-$(COMMIT)"; )
BUILD_TIME=$(shell TZ="UTC" date +'%Y-%m-%dT%H:%M:%S')
GO_VERSION=$(shell go version | cut -d ' ' -f3)
# Add go ldflags using LDFLAGS at the time of compilation.
BUILD_INFO_LDFLAGS = -X github.com/boxjan/misc/commom/cmd.version=$(REV) \
-X github.com/boxjan/misc/commom/cmd.buildTime=$(BUILD_TIME)
EXT_LDFLAGS = -extldflags "-static"
LDFLAGS =
FULL_LDFLAGS = $(LDFLAGS) $(BUILD_INFO_LDFLAGS) $(EXT_LDFLAGS)
BUILD_PLATFORMS ?=linux/amd64,linux/arm64
.PHONY: build-% build
build: ## build bin
build: $(CMDS:%=build-%)
$(CMDS:%=build-%): build-%:
mkdir -p bin
echo '$(BUILD_PLATFORMS)' | tr '/' ' ' | tr ',' '\n' | while read -r os arch suffix; do \
if ! (set -x; CGO_ENABLED=0 GOOS="$$os" GOARCH="$$arch" go build $(GOFLAGS_VENDOR) -a -ldflags '$(FULL_LDFLAGS)' -o ./bin/"$*_$$os"_"$$arch" ./cmd/$*/); then \
echo "Building $* for GOOS=$$os GOARCH=$$arch failed, see error(s) above."; \
exit 1; \
fi; \
done
REGISTRY ?= boxjan/misc
.PHONY: container-% container
container: ## Package container
container: $(CMDS:%=cdontainer-%)
$(CMDS:%=container-%): container-%: build-%
set -x; \
echo '$(BUILD_PLATFORMS)' | tr '/' ' ' | tr ',' '\n' | while read -r os arch suffix; do \
docker build -t $(REGISTRY)-$*:$(REV)-$$arch -t $(REGISTRY)-$*:latest-$$arch \
--build-arg os=$$os --build-arg arch=$$arch --platform $$os/$$arch \
-f $(shell if [ -e ./cmd/$*/Dockerfile ]; then echo ./cmd/$*/Dockerfile; else echo Dockerfile; fi) . ; \
docker push $(REGISTRY)-$*:$(REV)-$$arch; \
docker push $(REGISTRY)-$*:latest-$$arch; \
done; \
docker manifest create $(REGISTRY)-$*:latest $(shell echo '$(BUILD_PLATFORMS)' | tr '/' ' ' | tr ',' '\n' | while read -r os arch suffix; do echo $(REGISTRY)-$*:latest-"$$arch"; done); \
docker manifest create $(REGISTRY)-$*:$(REV) $(shell echo '$(BUILD_PLATFORMS)' | tr '/' ' ' | tr ',' '\n' | while read -r os arch suffix; do echo $(REGISTRY)-$*:$(REV)-"$$arch"; done); \
echo '$(BUILD_PLATFORMS)' | tr '/' ' ' | tr ',' '\n' | while read -r os arch suffix; do \
docker manifest annotate $(REGISTRY)-$*:$(REV) $(REGISTRY)-$*:$(REV)-$$arch --os $$os --arch $$arch; \
docker manifest annotate $(REGISTRY)-$*:latest $(REGISTRY)-$*:latest-$$arch --os $$os --arch $$arch; \
done; \
docker manifest push $(REGISTRY)-$*:latest;\
docker manifest push $(REGISTRY)-$*:$(REV)
.PHONY: test
test: ## run all test case
@ echo; echo "### $@:"
go test $(GOFLAGS_VENDOR) `go list $(GOFLAGS_VENDOR) ./... | grep -v -e 'vendor' -e '/test/e2e$$' $(TEST_GO_FILTER_CMD)` $(TESTARGS)