forked from thockin/go-build-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
443 lines (392 loc) · 18.2 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
DBG_MAKEFILE ?=
ifeq ($(DBG_MAKEFILE),1)
$(warning ***** starting Makefile for goal(s) "$(MAKECMDGOALS)")
$(warning ***** $(shell date))
else
# If we're not debugging the Makefile, don't echo recipes.
MAKEFLAGS += -s
endif
# The binaries to build (just the basenames)
BINS ?= myapp-1 myapp-2
# The platforms we support. In theory this can be used for Windows platforms,
# too, but they require specific base images, which we do not have.
ALL_PLATFORMS ?= linux/amd64 linux/arm linux/arm64 linux/ppc64le linux/s390x
# The "FROM" part of the Dockerfile. This should be a manifest-list which
# supports all of the platforms listed in ALL_PLATFORMS.
BASE_IMAGE ?= gcr.io/distroless/static
# Where to push the docker images.
REGISTRY ?= example.com
# Credentials to access the registry.
REGISTRY_USERNAME ?= oauth2accesstoken
REGISTRY_PASSWORD ?= $$(gcloud auth print-access-token)
# This version-strategy uses git tags to set the version string
VERSION ?= $(shell git describe --tags --always --dirty)
#
# This version-strategy uses a manual value to set the version string
#VERSION ?= 1.2.3
# Set this to 1 to build a debugger-friendly binaries.
DBG ?=
###
### These variables should not need tweaking.
###
# We don't need make's built-in rules.
MAKEFLAGS += --no-builtin-rules
# Be pedantic about undefined variables.
MAKEFLAGS += --warn-undefined-variables
.SUFFIXES:
# Used internally. Users should pass GOOS and/or GOARCH.
OS := $(if $(GOOS),$(GOOS),$(shell GOTOOLCHAIN=local go env GOOS))
ARCH := $(if $(GOARCH),$(GOARCH),$(shell GOTOOLCHAIN=local go env GOARCH))
TAG := $(VERSION)__$(OS)_$(ARCH)
GO_VERSION := 1.23
BUILD_IMAGE := golang:$(GO_VERSION)-alpine
BIN_EXTENSION :=
ifeq ($(OS), windows)
BIN_EXTENSION := .exe
endif
# It's necessary to set this because some environments don't link sh -> bash.
SHELL := /usr/bin/env bash -o errexit -o pipefail -o nounset
# This is used in docker buildx commands
BUILDX_NAME := $(shell basename $$(pwd))
# Satisfy --warn-undefined-variables.
GOFLAGS ?=
HTTP_PROXY ?=
HTTPS_PROXY ?=
# Because we store the module cache locally.
GOFLAGS := $(GOFLAGS) -modcacherw
# If you want to build all binaries, see the 'all-build' rule.
# If you want to build all containers, see the 'all-container' rule.
# If you want to build AND push all containers, see the 'all-push' rule.
all: # @HELP builds binaries for one platform ($OS/$ARCH)
all: build
# For the following OS/ARCH expansions, we transform OS/ARCH into OS_ARCH
# because make pattern rules don't match with embedded '/' characters.
build-%:
$(MAKE) build \
--no-print-directory \
GOOS=$(firstword $(subst _, ,$*)) \
GOARCH=$(lastword $(subst _, ,$*))
container-%:
$(MAKE) container \
--no-print-directory \
GOOS=$(firstword $(subst _, ,$*)) \
GOARCH=$(lastword $(subst _, ,$*))
push-%:
$(MAKE) push \
--no-print-directory \
GOOS=$(firstword $(subst _, ,$*)) \
GOARCH=$(lastword $(subst _, ,$*))
all-build: # @HELP builds binaries for all platforms
all-build: $(addprefix build-, $(subst /,_, $(ALL_PLATFORMS)))
all-container: # @HELP builds containers for all platforms
all-container: $(addprefix container-, $(subst /,_, $(ALL_PLATFORMS)))
all-push: # @HELP pushes containers for all platforms to the defined registry
all-push: $(addprefix push-, $(subst /,_, $(ALL_PLATFORMS)))
# The following structure defeats Go's (intentional) behavior to always touch
# result files, even if they have not changed. This will still run `go` but
# will not trigger further work if nothing has actually changed.
OUTBINS = $(foreach bin,$(BINS),bin/$(OS)_$(ARCH)/$(bin)$(BIN_EXTENSION))
build: $(OUTBINS)
echo
# Directories that we need created to build/test.
BUILD_DIRS := bin/$(OS)_$(ARCH) \
bin/tools \
.go/bin/$(OS)_$(ARCH) \
.go/bin/$(OS)_$(ARCH)/$(OS)_$(ARCH) \
.go/cache \
.go/pkg
# Each outbin target is just a facade for the respective stampfile target.
# This `eval` establishes the dependencies for each.
$(foreach outbin,$(OUTBINS),$(eval \
$(outbin): .go/$(outbin).stamp \
))
# This is the target definition for all outbins.
$(OUTBINS):
true
# Each stampfile target can reference an $(OUTBIN) variable.
$(foreach outbin,$(OUTBINS),$(eval $(strip \
.go/$(outbin).stamp: OUTBIN = $(outbin) \
)))
# This is the target definition for all stampfiles.
# This will build the binary under ./.go and update the real binary iff needed.
STAMPS = $(foreach outbin,$(OUTBINS),.go/$(outbin).stamp)
.PHONY: $(STAMPS)
$(STAMPS): go-build
echo -ne "binary: $(OUTBIN) "
if ! cmp -s .go/$(OUTBIN) $(OUTBIN); then \
mv .go/$(OUTBIN) $(OUTBIN); \
date >$@; \
echo; \
else \
echo "(cached)"; \
fi
# This runs the actual `go build` which updates all binaries.
go-build: | $(BUILD_DIRS)
echo "# building for $(OS)/$(ARCH)"
docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \
-v $$(pwd)/.go/cache:/.cache \
--env GOCACHE="/.cache/gocache" \
--env GOMODCACHE="/.cache/gomodcache" \
--env ARCH="$(ARCH)" \
--env OS="$(OS)" \
--env VERSION="$(VERSION)" \
--env DEBUG="$(DBG)" \
--env GOFLAGS="$(GOFLAGS)" \
--env HTTP_PROXY="$(HTTP_PROXY)" \
--env HTTPS_PROXY="$(HTTPS_PROXY)" \
$(BUILD_IMAGE) \
./build/build.sh ./...
# Example: make shell CMD="-c 'date > datefile'"
shell: # @HELP launches a shell in the containerized build environment
shell: | $(BUILD_DIRS)
echo "# launching a shell in the containerized build environment"
docker run \
-ti \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \
-v $$(pwd)/.go/cache:/.cache \
--env GOCACHE="/.cache/gocache" \
--env GOMODCACHE="/.cache/gomodcache" \
--env ARCH="$(ARCH)" \
--env OS="$(OS)" \
--env VERSION="$(VERSION)" \
--env DEBUG="$(DBG)" \
--env GOFLAGS="$(GOFLAGS)" \
--env HTTP_PROXY="$(HTTP_PROXY)" \
--env HTTPS_PROXY="$(HTTPS_PROXY)" \
$(BUILD_IMAGE) \
/bin/sh $(CMD)
LICENSES = .licenses
$(LICENSES): | $(BUILD_DIRS)
# Don't assume that `go` is available locally.
docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd)/tools:/src \
-w /src \
-v $$(pwd)/bin/tools:/go/bin \
-v $$(pwd)/.go/cache:/.cache \
--env GOCACHE="/.cache/gocache" \
--env GOMODCACHE="/.cache/gomodcache" \
--env CGO_ENABLED=0 \
--env HTTP_PROXY="$(HTTP_PROXY)" \
--env HTTPS_PROXY="$(HTTPS_PROXY)" \
$(BUILD_IMAGE) \
go install github.com/google/go-licenses/v2
# The tool runs in a container because it execs `go`, which doesn't
# play nicely with CI. The tool also wants its output dir to not
# exist, so we can't just volume mount $(LICENSES).
rm -rf $(LICENSES).tmp
mkdir $(LICENSES).tmp
docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd)/$(LICENSES).tmp:/output \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/bin/tools:/go/bin \
-v $$(pwd)/.go/cache:/.cache \
-v $$(pwd)/.go/pkg:/go/pkg \
--env HTTP_PROXY="$(HTTP_PROXY)" \
--env HTTPS_PROXY="$(HTTPS_PROXY)" \
$(BUILD_IMAGE) \
go-licenses save ./... --save_path=/output/licenses
rm -rf $(LICENSES)
mv $(LICENSES).tmp/licenses $(LICENSES)
rmdir $(LICENSES).tmp
find $(LICENSES) -type d | xargs chmod 0755
find $(LICENSES) -type f | xargs chmod 0644
CONTAINER_DOTFILES = $(foreach bin,$(BINS),.container-$(subst /,_,$(REGISTRY)/$(bin))-$(TAG))
# We print the container names here, rather than in CONTAINER_DOTFILES so
# they are always at the end of the output.
container containers: # @HELP builds containers for one platform ($OS/$ARCH)
container containers: $(CONTAINER_DOTFILES)
for bin in $(BINS); do \
echo "container: $(REGISTRY)/$$bin:$(TAG)"; \
done
echo
# Each container-dotfile target can reference a $(BIN) variable.
# This is done in 2 steps to enable target-specific variables.
$(foreach bin,$(BINS),$(eval $(strip \
.container-$(subst /,_,$(REGISTRY)/$(bin))-$(TAG): BIN = $(bin) \
)))
$(foreach bin,$(BINS),$(eval \
.container-$(subst /,_,$(REGISTRY)/$(bin))-$(TAG): bin/$(OS)_$(ARCH)/$(bin)$(BIN_EXTENSION) $(LICENSES) Dockerfile.in \
))
# This is the target definition for all container-dotfiles.
# These are used to track build state in hidden files.
$(CONTAINER_DOTFILES): .buildx-initialized
echo
sed \
-e 's|{ARG_BIN}|$(BIN)$(BIN_EXTENSION)|g' \
-e 's|{ARG_ARCH}|$(ARCH)|g' \
-e 's|{ARG_OS}|$(OS)|g' \
-e 's|{ARG_FROM}|$(BASE_IMAGE)|g' \
Dockerfile.in > .dockerfile-$(BIN)-$(OS)_$(ARCH)
HASH_LICENSES=$$(find $(LICENSES) -type f \
| xargs md5sum | md5sum | cut -f1 -d' '); \
HASH_BINARY=$$(md5sum bin/$(OS)_$(ARCH)/$(BIN)$(BIN_EXTENSION) \
| cut -f1 -d' '); \
FORCE=0; \
docker buildx build \
--builder "$(BUILDX_NAME)" \
--build-arg FORCE_REBUILD="$$FORCE" \
--build-arg HASH_LICENSES="$$HASH_LICENSES" \
--build-arg HASH_BINARY="$$HASH_BINARY" \
--progress=plain \
--load \
--platform "$(OS)/$(ARCH)" \
--build-arg HTTP_PROXY="$(HTTP_PROXY)" \
--build-arg HTTPS_PROXY="$(HTTPS_PROXY)" \
-t $(REGISTRY)/$(BIN):$(TAG) \
-f .dockerfile-$(BIN)-$(OS)_$(ARCH) \
.
docker images -q $(REGISTRY)/$(BIN):$(TAG) > $@
echo
login: # @HELP configures docker to be authenticated to the defined registry
docker login $(REGISTRY) \
-u "$(REGISTRY_USERNAME)" \
-p "$(REGISTRY_PASSWORD)"
push: # @HELP pushes the container for one platform ($OS/$ARCH) to the defined registry
push: container
for bin in $(BINS); do \
docker push $(REGISTRY)/$$bin:$(TAG); \
done
echo
# This depends on github.com/estesp/manifest-tool.
manifest-list: # @HELP builds a manifest list of containers for all platforms
manifest-list: all-push manifest-tool
for bin in $(BINS); do \
platforms=$$(echo $(ALL_PLATFORMS) | sed 's/ /,/g'); \
bin/tools/manifest-tool \
--username="$(REGISTRY_USERNAME)" \
--password="$(REGISTRY_PASSWORD)" \
push from-args \
--platforms "$$platforms" \
--template $(REGISTRY)/$$bin:$(VERSION)__OS_ARCH \
--target $(REGISTRY)/$$bin:$(VERSION); \
done
manifest-tool: # @HELP builds manifest-tool
manifest-tool:
# Don't assume that `go` is available locally.
docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd)/tools:/src \
-w /src \
-v $$(pwd)/bin/tools:/go/bin \
-v $$(pwd)/.go/cache:/.cache \
--env GOCACHE="/.cache/gocache" \
--env GOMODCACHE="/.cache/gomodcache" \
--env CGO_ENABLED=0 \
--env HTTP_PROXY="$(HTTP_PROXY)" \
--env HTTPS_PROXY="$(HTTPS_PROXY)" \
$(BUILD_IMAGE) \
go install github.com/estesp/manifest-tool/v2/cmd/manifest-tool
version: # @HELP outputs the version string
version:
echo $(VERSION)
test: # @HELP runs tests, as defined in ./build/test.sh
test: | $(BUILD_DIRS)
docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \
-v $$(pwd)/.go/cache:/.cache \
--env GOCACHE="/.cache/gocache" \
--env GOMODCACHE="/.cache/gomodcache" \
--env ARCH="$(ARCH)" \
--env OS="$(OS)" \
--env VERSION="$(VERSION)" \
--env DEBUG="$(DBG)" \
--env GOFLAGS="$(GOFLAGS)" \
--env HTTP_PROXY="$(HTTP_PROXY)" \
--env HTTPS_PROXY="$(HTTPS_PROXY)" \
$(BUILD_IMAGE) \
./build/test.sh ./...
lint: # @HELP runs golangci-lint
lint: | $(BUILD_DIRS)
docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \
-v $$(pwd)/.go/cache:/.cache \
--env GOCACHE="/.cache/gocache" \
--env GOMODCACHE="/.cache/gomodcache" \
--env ARCH="$(ARCH)" \
--env OS="$(OS)" \
--env VERSION="$(VERSION)" \
--env DEBUG="$(DBG)" \
--env GOFLAGS="$(GOFLAGS)" \
--env HTTP_PROXY="$(HTTP_PROXY)" \
--env HTTPS_PROXY="$(HTTPS_PROXY)" \
$(BUILD_IMAGE) \
./build/lint.sh ./...
$(BUILD_DIRS):
mkdir -p $@
clean: # @HELP removes built binaries and temporary files
clean: container-clean bin-clean
container-clean:
rm -rf .container-* .dockerfile-* .push-* .buildx-initialized $(LICENSES)
bin-clean:
test -d .go && chmod -R u+w .go || true
rm -rf .go bin
help: # @HELP prints this message
help:
echo "VARIABLES:"
echo " BINS = $(BINS)"
echo " OS = $(OS)"
echo " ARCH = $(ARCH)"
echo " DBG = $(DBG)"
echo " GOFLAGS = $(GOFLAGS)"
echo " REGISTRY = $(REGISTRY)"
echo
echo "TARGETS:"
grep -E '^.*: *# *@HELP' $(MAKEFILE_LIST) \
| awk ' \
BEGIN {FS = ": *# *@HELP"}; \
{ printf " %-30s %s\n", $$1, $$2 }; \
'
# Help set up multi-arch build tools. This assumes you have the tools
# installed. If you already have a buildx builder available, you don't need
# this. See https://medium.com/@artur.klauser/building-multi-architecture-docker-images-with-buildx-27d80f7e2408
# for great context.
.buildx-initialized:
docker buildx create --name "$(BUILDX_NAME)" --node "$(BUILDX_NAME)-0" >/dev/null
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes >/dev/null
date > $@