-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMakefile
164 lines (135 loc) · 5.67 KB
/
Makefile
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
SHELL=/usr/bin/env bash -o pipefail
TMP_DIR := $(shell pwd)/tmp
BIN_DIR ?= $(TMP_DIR)/bin
LIB_DIR ?= $(TMP_DIR)/lib
FIRST_GOPATH := $(firstword $(subst :, ,$(shell go env GOPATH)))
OS ?= $(shell go env GOOS)
ARCH ?= $(shell go env GOARCH)
VERSION := $(strip $(shell [ -d .git ] && git describe --always --tags --dirty))
BUILD_DATE := $(shell date -u +"%Y-%m-%d")
BUILD_TIMESTAMP := $(shell date -u +"%Y-%m-%dT%H:%M:%S%Z")
VCS_BRANCH := $(strip $(shell git rev-parse --abbrev-ref HEAD))
VCS_REF := $(strip $(shell [ -d .git ] && git rev-parse --short HEAD))
DOCKER_REPO ?= quay.io/observatorium/thanos-rule-syncer
THANOS ?= $(BIN_DIR)/thanos
THANOS_VERSION ?= 0.17.0
OBSERVATORIUM ?= $(BIN_DIR)/observatorium
HYDRA ?= $(BIN_DIR)/hydra
GOLANGCILINT ?= $(FIRST_GOPATH)/bin/golangci-lint
GOLANGCILINT_VERSION ?= v1.21.0
EMBEDMD ?= $(BIN_DIR)/embedmd
SHELLCHECK ?= $(BIN_DIR)/shellcheck
default: thanos-rule-syncer
all: clean lint test thanos-rule-syncer
tmp/help.txt: thanos-rule-syncer
./thanos-rule-syncer --help &> tmp/help.txt || true
README.md: $(EMBEDMD) tmp/help.txt
$(EMBEDMD) -w README.md
thanos-rule-syncer: main.go $(wildcard *.go) $(wildcard */*.go)
go mod tidy
CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) GO111MODULE=on GOPROXY=https://proxy.golang.org go build -mod mod -a -ldflags '-s -w' -o $@ .
.PHONY: build
build: thanos-rule-syncer
.PHONY: format
format: $(GOLANGCILINT)
$(GOLANGCILINT) run --fix --enable-all -c .golangci.yml
.PHONY: go-fmt
go-fmt:
@fmt_res=$$(gofmt -d -s $$(find . -type f -name '*.go' -not -path './jsonnet/vendor/*' -not -path '${TMP_DIR}/*')); if [ -n "$$fmt_res" ]; then printf '\nGofmt found style issues. Please check the reported issues\nand fix them if necessary before submitting the code for review:\n\n%s' "$$fmt_res"; exit 1; fi
.PHONY: shellcheck
shellcheck: $(SHELLCHECK)
$(SHELLCHECK) $(shell find . -type f -name "*.sh" -not -path "${TMP_DIR}/*")
.PHONY: lint
lint: $(GOLANGCILINT) go-fmt shellcheck
$(GOLANGCILINT) run -v --enable-all -c .golangci.yml
.PHONY: test
test: build test-unit test-integration
.PHONY: test-unit
test-unit:
CGO_ENABLED=1 GO111MODULE=on go test -mod mod -v -race -short ./...
.PHONY: test-integration
test-integration: build integration-test-dependencies
PATH=$(BIN_DIR):$(FIRST_GOPATH)/bin:$$PATH LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(LIB_DIR) ./test/integration.sh
.PHONY: clean
clean:
-rm tmp/help.txt
-rm -rf tmp/bin
-rm thanos-rule-syncer
.PHONY: container
container: Dockerfile
@docker build \
--build-arg BUILD_DATE="$(BUILD_TIMESTAMP)" \
--build-arg VERSION="$(VERSION)" \
--build-arg VCS_REF="$(VCS_REF)" \
--build-arg VCS_BRANCH="$(VCS_BRANCH)" \
--build-arg DOCKERFILE_PATH="/Dockerfile" \
-t $(DOCKER_REPO):$(VCS_BRANCH)-$(BUILD_DATE)-$(VERSION) .
@docker tag $(DOCKER_REPO):$(VCS_BRANCH)-$(BUILD_DATE)-$(VERSION) $(DOCKER_REPO):latest
.PHONY: container-build
container-build:
docker buildx build \
--platform linux/amd64,linux/arm64 \
--cache-to type=local,dest=./.buildxcache/ \
--build-arg BUILD_DATE="$(BUILD_TIMESTAMP)" \
--build-arg VERSION="$(VERSION)" \
--build-arg VCS_REF="$(VCS_REF)" \
--build-arg VCS_BRANCH="$(VCS_BRANCH)" \
--build-arg DOCKERFILE_PATH="/Dockerfile" \
-t $(DOCKER_REPO):$(VCS_BRANCH)-$(BUILD_DATE)-$(VERSION) \
-t $(DOCKER_REPO):latest \
.
.PHONY: container-build-push
container-build-push:
docker buildx build \
--push \
--platform linux/amd64,linux/arm64 \
--cache-to type=local,dest=./.buildxcache/ \
--build-arg BUILD_DATE="$(BUILD_TIMESTAMP)" \
--build-arg VERSION="$(VERSION)" \
--build-arg VCS_REF="$(VCS_REF)" \
--build-arg VCS_BRANCH="$(VCS_BRANCH)" \
--build-arg DOCKERFILE_PATH="/Dockerfile" \
-t $(DOCKER_REPO):$(VCS_BRANCH)-$(BUILD_DATE)-$(VERSION) \
-t $(DOCKER_REPO):latest \
.
.PHONY: conditional-container-build-push
conditional-container-build-push:
build/conditional-container-push.sh $(DOCKER_REPO):$(VCS_BRANCH)-$(BUILD_DATE)-$(VERSION)
.PHONY: container-release-build-push
container-release-build-push: VERSION_TAG = $(strip $(shell [ -d .git ] && git tag --points-at HEAD))
container-release-build-push: container-build-push
# https://git-scm.com/docs/git-tag#Documentation/git-tag.txt---points-atltobjectgt
@docker buildx build \
--push \
--platform linux/amd64,linux/arm64 \
--cache-from type=local,src=./.buildxcache/ \
--build-arg BUILD_DATE="$(BUILD_TIMESTAMP)" \
--build-arg VERSION="$(VERSION)" \
--build-arg VCS_REF="$(VCS_REF)" \
--build-arg VCS_BRANCH="$(VCS_BRANCH)" \
--build-arg DOCKERFILE_PATH="/Dockerfile" \
-t $(DOCKER_REPO):$(VERSION_TAG) \
-t $(DOCKER_REPO):latest \
.
.PHONY: integration-test-dependencies
integration-test-dependencies: $(THANOS) $(HYDRA) $(OBSERVATORIUM)
$(BIN_DIR):
mkdir -p $(BIN_DIR)
$(LIB_DIR):
mkdir -p $@
$(THANOS): | $(BIN_DIR)
@echo "Downloading Thanos"
curl -L "https://github.com/thanos-io/thanos/releases/download/v$(THANOS_VERSION)/thanos-$(THANOS_VERSION).$(OS)-$(ARCH).tar.gz" | tar --strip-components=1 -xzf - -C $(BIN_DIR)
$(OBSERVATORIUM): | $(BIN_DIR)
go build -mod=mod -o $@ github.com/observatorium/api
$(HYDRA): | $(BIN_DIR)
@echo "Downloading Hydra"
curl -L "https://github.com/ory/hydra/releases/download/v1.7.4/hydra_1.7.4_linux_64-bit.tar.gz" | tar -xzf - -C $(BIN_DIR) hydra
$(EMBEDMD): | $(BIN_DIR)
go build -mod=mod -o $@ github.com/campoy/embedmd
$(GOLANGCILINT):
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/$(GOLANGCILINT_VERSION)/install.sh \
| sed -e '/install -d/d' \
| sh -s -- -b $(FIRST_GOPATH)/bin $(GOLANGCILINT_VERSION)
$(SHELLCHECK): | $(BIN_DIR)
curl -sNL "https://github.com/koalaman/shellcheck/releases/download/stable/shellcheck-stable.$(OS).$(shell uname -m).tar.xz" | tar --strip-components=1 -xJf - -C $(BIN_DIR)