-
-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathMakefile
More file actions
204 lines (171 loc) · 7.51 KB
/
Makefile
File metadata and controls
204 lines (171 loc) · 7.51 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
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
ROOT_DIR="$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))/"
## For Docker <=20.04
export DOCKER_BUILDKIT=1
## For Docker <=20.04
export DOCKER_CLI_EXPERIMENTAL=enabled
## Required to have docker build output always printed on stdout
export BUILDKIT_PROGRESS=plain
current_os := $(shell uname -s)
current_arch := $(shell uname -m)
export OS ?= $(shell \
case "$(current_os)" in \
(Linux) echo linux ;; \
(Darwin) echo linux ;; \
(MINGW*|MSYS*|CYGWIN*) echo windows ;; \
(*) echo unknown ;; \
esac)
export ARCH ?= $(shell \
case $(current_arch) in \
(x86_64) echo "amd64" ;; \
(aarch64|arm64) echo "arm64" ;; \
(armv7*) echo "arm/v7";; \
(s390*|riscv*|ppc64le) echo $(current_arch);; \
(*) echo "UNKNOWN-CPU";; \
esac)
##### Macros
## Check the presence of a CLI in the current PATH
check_cli = type "$(1)" >/dev/null 2>&1 || { echo "Error: command '$(1)' required but not found. Exiting." ; exit 1 ; }
## Check if a given image or group exists in the current manifest docker-bake.hcl
check_image = $(MAKE) --silent list listgroup-all | grep -w '$(1)' >/dev/null 2>&1 || { echo "Error: the image or group '$(1)' does not exist in manifest for the current platform '$(OS)/$(ARCH)'. Please check the output of '$(MAKE) list' or '$(MAKE) listgroup-all'. Exiting." ; exit 1 ; }
## Base "docker buildx base" command to be reused everywhere
bake_base_cli := docker buildx bake --file docker-bake.hcl
## Command to be used on build (only)
bake_cli := $(bake_base_cli) --load
## Default bake target
bake_default_target := all
.PHONY: build
.PHONY: test
check-reqs:
## Build requirements
@$(call check_cli,bash)
@$(call check_cli,git)
@$(call check_cli,docker)
@docker info | grep 'buildx:' >/dev/null 2>&1 || { echo "Error: Docker BuildX plugin required but not found. Exiting." ; exit 1 ; }
## Test requirements
@$(call check_cli,curl)
@$(call check_cli,jq)
## This function is specific to Jenkins infrastructure and isn't required in other contexts
docker-init: check-reqs
ifeq ($(CI),true)
ifeq ($(wildcard /etc/buildkitd.toml),)
echo 'WARNING: /etc/buildkitd.toml not found, using default configuration.'
docker buildx create --use --bootstrap --driver docker-container
else
docker buildx create --use --bootstrap --driver docker-container --config /etc/buildkitd.toml
endif
else
docker buildx create --use --bootstrap --driver docker-container
endif
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
docker info
# Lint check on all Dockerfiles
hadolint:
find . -type f -name 'Dockerfile*' -not -path "./bats/*" -print0 | xargs -0 $(ROOT_DIR)/tools/hadolint
# Shellcheck on all bash scripts
shellcheck:
@$(ROOT_DIR)/tools/shellcheck -e SC1091 jenkins-agent *.sh tests/test_helpers.bash tests/*.bats tools/hadolint tools/shellcheck
# Build all targets with the current OS and architecture
build: check-reqs target showarch-$(ARCH)
@set -x; $(bake_cli) --metadata-file=target/build-result-metadata_$(bake_default_target).json --set '*.platform=$(OS)/$(ARCH)' $(shell $(MAKE) --silent list)
# Build a specific target with the current OS and architecture
build-%: check-reqs target show-%
@$(call check_image,$*)
@echo "== building $*"
@set -x; $(bake_cli) --metadata-file=target/build-result-metadata_$*.json --set '*.platform=$(OS)/$(ARCH)' '$*'
# Build default bake group corresponding to the current OS but independently of the architecture
multiarchbuild: check-reqs show-$(OS)
@set -x; $(bake_base_cli) $(OS)
# Build a specific bake group or target independently of the architecture or the OS
multiarchbuild-%: check-reqs show-%
@set -x; $(bake_base_cli) $*
# Show all default targets
show:
@set -x; $(MAKE) --silent show-$(bake_default_target)
# Show a specific target
show-%:
@set -x; $(bake_base_cli) --progress=quiet --print $* | jq
# Show all targets depending on the architecture
showarch-%:
@set -x; $(MAKE) --silent show | jq --arg arch "$(OS)/$*" '.target |= with_entries(select(.value.platforms | index($$arch)))'
# List tags of all default targets
tags:
@set -x; $(MAKE) --silent tags-$(bake_default_target)
# List tags of a specific target
tags-%:
@set -x; $(MAKE) --silent show-$* | jq -r '.target | to_entries[] | .key as $$name | .value.tags[] | "\(.) (\($$name))"' | LC_ALL=C sort -u
# Return the list of targets depending on the current OS and architecture
list: check-reqs
@set -x; $(MAKE) --silent listarch-$(ARCH)
# Return the list of targets of a specific "target" (can be a docker bake group)
list-%: check-reqs
@set -x; $(MAKE) --silent show-$* | jq -r '.target | keys[]'
# Return the list of targets depending on the current OS and architecture
listarch-%: check-reqs
@set -x; $(MAKE) --silent showarch-$* | jq -r '.target | keys[]'
# Return the list of targets of a specific bake group
listgroup-%: check-reqs
@set -x; $(MAKE) --silent show-$* | jq -r '.group | keys[]' | grep -v -e $* -e default
# Ensure bats exists in the current folder
bats:
git clone --branch v1.13.0 https://github.com/bats-core/bats-core ./bats
# Ensure all bats submodules are up to date
prepare-test: bats check-reqs target
git submodule update --init --recursive
# Ensure tests and build metadata "target" folder exist
target:
mkdir -p target
# Publish all targets corresponding to the current OS
publish:
@set -x; $(MAKE) --silent publish-$(OS)
# Publish a specific "target" (can be a docker bake group)
publish-%: target show-%
@set -x; $(bake_base_cli) --metadata-file=target/build-result-metadata_$*_publish.json --push $*
## Define bats options based on environment
# common flags for all tests
bats_flags := ""
# if DISABLE_PARALLEL_TESTS true, then disable parallel execution
ifneq (true,$(DISABLE_PARALLEL_TESTS))
# If the GNU 'parallel' command line is absent, then disable parallel execution
parallel_cli := $(shell command -v parallel 2>/dev/null)
ifneq (,$(parallel_cli))
# If parallel execution is enabled, then set 2 tests per core available for the Docker Engine
test-%: PARALLEL_JOBS ?= $(shell echo $$(( $(shell docker run --rm alpine grep -c processor /proc/cpuinfo) * 2)))
test-%: bats_flags += --jobs $(PARALLEL_JOBS)
endif
endif
# Optional bats flags (see https://bats-core.readthedocs.io/en/stable/usage.html)
ifneq (,$(BATS_FLAGS))
test-%: bats_flags += $(BATS_FLAGS)
endif
test-%: prepare-test
# Check that the image exists in the manifest
@$(call check_image,$*)
# Ensure that the image is built
@set -x; $(MAKE) --silent build-$*
@set -x; $(MAKE) --silent _test-dispatch TARGET=$*
# Dispatch the test depending if it's a bake target or a group
_test-dispatch:
@set -x; if $(MAKE) --silent listgroup-linux | grep -w "$(TARGET)" >/dev/null 2>&1; then \
$(MAKE) --silent _test-group TARGET=$(TARGET); \
else \
$(MAKE) --silent _test-image TARGET=$(TARGET); \
fi
# Test a group by iterating on its targets
_test-group:
@set -x; $(MAKE) --silent list-$(TARGET) | while read image; do $(MAKE) --silent test-$$image; done
# Test an image
_test-image:
@echo "Testing $(TARGET)"
# Show bats version
@set -x; bats/bin/bats --version
# Each type of image ("agent" or "inbound-agent") has its own tests suite
ifeq ($(CI), true)
# Execute the test harness and write result to a TAP file
IMAGE=$(TARGET) bats/bin/bats $(CURDIR)/tests/tests_$(shell echo $(TARGET) | cut -d "_" -f 1).bats $(bats_flags) --formatter junit | tee target/junit-results-$(TARGET).xml
else
# Execute the test harness
IMAGE=$(TARGET) bats/bin/bats $(CURDIR)/tests/tests_$(shell echo $(TARGET) | cut -d "_" -f 1).bats $(bats_flags) --formatter pretty --timing
endif
# Test all targets depending on the current OS and architecture
test:
@set -x; $(MAKE) --silent test-$(OS)