-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathMakefile.common
238 lines (214 loc) · 7.1 KB
/
Makefile.common
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
GO ?= go
FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH)))
GOOPTS ?=
GO_ARCHITECTURES_DARWIN ?= amd64 arm64
GO_ARCHITECTURES_FREEBSD ?= amd64
GO_ARCHITECTURES_LINUX ?= amd64 arm arm64
GO_ARCHITECTURES_OPENBSD ?=
GO_ARCHITECTURES_WINDOWS ?= amd64 arm64
GO_VERSION ?= $(shell $(GO) version)
GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION))
GO_BUILD_TAGS ?= "netgo"
PREFIX ?= $(shell pwd)
BIN_DIR ?= $(shell pwd)
DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
DOCKERFILE_PATH ?= ./Dockerfile
DOCKERBUILD_CONTEXT ?= ./
DOCKER_REPO ?= releaseargus/argus
DOCKER_ARCHS ?= amd64
BUILD_DOCKER_ARCHS = $(addprefix common-docker-,$(DOCKER_ARCHS))
PUBLISH_DOCKER_ARCHS = $(addprefix common-docker-publish-,$(DOCKER_ARCHS))
TAG_DOCKER_ARCHS = $(addprefix common-docker-tag-latest-,$(DOCKER_ARCHS))
define GetFromPkg
$(shell node -p "require('./web/ui/react-app/package.json').$(1)")
endef
BUILD_DATE ?= ${shell date -u +"%FT%TZ"}
BUILD_VERSION ?= $(call GetFromPkg,version)
OUTPUT_BINARY ?= argus
NCU_EXISTS := $(shell command -v ncu 2> /dev/null)
.PHONY: commit-prereqs
commit-prereqs:
@if [ "$(NCU_EXISTS)" ]; then \
ncu -u; \
npm install; \
else \
echo "Please install ncu (npm-check-updates) to update package.json"; \
fi
# This rule is used to forward a target like "build" to "common-build". This
# allows a new "build" target to be defined in a Makefile which includes this
# one and override "common-build" without override warnings.
%: common-% ;
.PHONY: common-deps
common-deps:
@echo ">> getting dependencies"
$(GO) get $(GOOPTS) -t ./...
.PHONY: update-go-deps
update-go-deps:
@echo ">> updating Go dependencies"
@for m in $$($(GO) list -mod=readonly -m -f '{{ if and (not .Indirect) (not .Main)}}{{.Path}}{{end}}' all); do \
$(GO) get -d $$m; \
done
$(GO) mod tidy
ifneq (,$(wildcard vendor))
$(GO) mod vendor
endif
.PHONY: update-npm-deps
update-npm-deps:
@echo ">> updating NPM dependencies"
ncu -u && npm install; \
cd web/ui/react-app; \
ncu -u && npm install; \
.PHONY: common-unused
common-unused: $(GOVENDOR)
ifdef GOVENDOR
@echo ">> running check for unused packages"
@$(GOVENDOR) list +unused | grep . && exit 1 || echo 'No unused packages'
else
@echo ">> running check for unused/missing packages in go.mod"
$(GO) mod tidy
ifeq (,$(wildcard vendor))
@git diff --exit-code -- go.sum go.mod
else
@echo ">> running check for unused packages in vendor/"
$(GO) mod vendor
@git diff --exit-code -- go.sum go.mod vendor/
endif
endif
.PHONY: common-build
common-build:
@sh -c "trap 'echo \">> restoring assets\"; web/ui/compress_web.sh -d;' EXIT; \
echo '>> compressing assets'; \
web/ui/compress_web.sh; \
echo '>> building binaries'; \
CGO_ENABLED=0; \
go build \
-tags="${GO_BUILD_TAGS}" \
-ldflags=\"\
-X 'github.com/release-argus/Argus/util.Version=${BUILD_VERSION}' \
-X 'github.com/release-argus/Argus/util.BuildDate=${BUILD_DATE}'\" \
-o ${OUTPUT_BINARY} \
./cmd/argus/; \
go build \
-tags="${GO_BUILD_TAGS}" \
-o healthcheck \
./_healthcheck/;"
.PHONY: build-darwin
build-darwin:
@echo "Building for darwin\n" \
$(foreach GOARCH, $(GO_ARCHITECTURES_DARWIN), \
" ${GOARCH}" \
$(shell \
GOOS=darwin \
GOARCH=$(GOARCH) \
CGO_ENABLED=0; \
go build \
-tags="${GO_BUILD_TAGS}" \
-ldflags="\
-X 'github.com/release-argus/argus/util.Version=\"${BUILD_VERSION}\"' \
-X 'github.com/release-argus/argus/util.BuildDate=\"${BUILD_DATE}\"'" \
-o .build/argus-${GOARCH}.darwin \
./cmd/argus \
) \
"- DONE\n" \
)
.PHONY: build-freebsd
build-freebsd:
@echo "Building for freebsd\n" \
$(foreach GOARCH, $(GO_ARCHITECTURES_FREEBSD), \
" ${GOARCH}" \
$(shell \
GOOS=freebsd \
GOARCH=$(GOARCH) \
CGO_ENABLED=0; \
go build \
-tags="${GO_BUILD_TAGS}" \
-ldflags="\
-X 'github.com/release-argus/argus/util.Version=\"${BUILD_VERSION}\"' \
-X 'github.com/release-argus/argus/util.BuildDate=\"${BUILD_DATE}\"'" \
-o .build/argus-${GOARCH}.freebsd \
./cmd/argus \
) \
"- DONE\n" \
)
.PHONY: build-linux
build-linux:
@echo "Building for linux\n" \
$(foreach GOARCH, $(GO_ARCHITECTURES_LINUX), \
" ${GOARCH}" \
$(shell \
GOOS=linux \
GOARCH=$(GOARCH) \
CGO_ENABLED=0; \
go build \
-tags="${GO_BUILD_TAGS}" \
-ldflags="\
-X 'github.com/release-argus/argus/util.Version=\"${BUILD_VERSION}\"' \
-X 'github.com/release-argus/argus/util.BuildDate=\"${BUILD_DATE}\"'" \
-o .build/argus-${GOARCH}.linux \
./cmd/argus \
) \
"- DONE\n" \
)
.PHONY: build-openbsd
build-openbsd:
@echo "Building for openbsd\n" \
$(foreach GOARCH, $(GO_ARCHITECTURES_OPENBSD), \
" ${GOARCH}" \
$(shell \
GOOS=openbsd \
GOARCH=$(GOARCH) \
CGO_ENABLED=0; \
go build \
-tags="${GO_BUILD_TAGS}" \
-ldflags="\
-X 'github.com/release-argus/argus/util.Version=\"${BUILD_VERSION}\"' \
-X 'github.com/release-argus/argus/util.BuildDate=\"${BUILD_DATE}\"'" \
-o .build/argus-${GOARCH}.openbsd \
./cmd/argus \
) \
"- DONE\n" \
)
.PHONY: build-windows
build-windows:
@echo "Building for windows\n" \
$(foreach GOARCH, $(GO_ARCHITECTURES_WINDOWS), \
" ${GOARCH}" \
$(shell \
GOOS=windows \
GOARCH=$(GOARCH) \
CGO_ENABLED=0; \
go build \
-tags="${GO_BUILD_TAGS}" \
-ldflags="\
-X 'github.com/release-argus/argus/util.Version=\"${BUILD_VERSION}\"' \
-X 'github.com/release-argus/argus/util.BuildDate=\"${BUILD_DATE}\"'" \
-o .build/argus-${GOARCH}.windows \
./cmd/argus \
) \
"- DONE\n" \
)
.PHONY: compress-web
compress-web:
@sh "web/ui/compress_web.sh"
.PHONY: common-docker $(BUILD_DOCKER_ARCHS)
common-docker: $(BUILD_DOCKER_ARCHS)
$(BUILD_DOCKER_ARCHS): common-docker-%:
docker build -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" \
-f $(DOCKERFILE_PATH) \
--build-arg ARCH="$*" \
--build-arg OS="linux" \
$(DOCKERBUILD_CONTEXT)
.PHONY: common-docker-publish $(PUBLISH_DOCKER_ARCHS)
common-docker-publish: $(PUBLISH_DOCKER_ARCHS)
$(PUBLISH_DOCKER_ARCHS): common-docker-publish-%:
docker push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)"
DOCKER_MAJOR_VERSION_TAG = $(firstword $(subst ., ,$(shell cat VERSION)))
.PHONY: common-docker-tag-latest $(TAG_DOCKER_ARCHS)
common-docker-tag-latest: $(TAG_DOCKER_ARCHS)
$(TAG_DOCKER_ARCHS): common-docker-tag-latest-%:
docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:latest"
docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:v$(DOCKER_MAJOR_VERSION_TAG)"
.PHONY: common-docker-manifest
common-docker-manifest:
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest create -a "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" $(foreach ARCH,$(DOCKER_ARCHS),$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$(ARCH):$(DOCKER_IMAGE_TAG))
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)"