-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathMakefile.docker
More file actions
145 lines (120 loc) · 5.64 KB
/
Copy pathMakefile.docker
File metadata and controls
145 lines (120 loc) · 5.64 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
# Shared Docker image infrastructure.
# Included by the top-level Makefile and by tests/Makefile.
# Repo root: the directory that contains this file, regardless of where make
# is invoked from (root, tests/, tests/tablespaces/, …).
REPO_ROOT := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
VERSION_FILE = $(REPO_ROOT)src/bin/pg_autoctl/git-version.h
ifeq ("$(wildcard $(VERSION_FILE))","")
DUMMY := $(shell git -C $(REPO_ROOT) update-index -q --refresh)
GIT_DIRTY := $(shell test -z "`git -C $(REPO_ROOT) diff-index --name-only HEAD --`" || echo "-dirty")
GIT_VVERSION := $(shell git -C $(REPO_ROOT) describe --match "v[0-9]*" HEAD 2>/dev/null)
GIT_DVERSION := $(shell echo $(GIT_VVERSION) | awk -Fv '{print $$2"$(GIT_DIRTY)"}')
GIT_VERSION := $(shell echo $(GIT_DVERSION) | sed -e 's/-/./g')
else
GIT_VERSION := $(shell awk -F '[ "]' '{print $$4}' $(VERSION_FILE))
endif
.PHONY: version
version: $(VERSION_FILE) ;
$(VERSION_FILE):
@echo "#define GIT_VERSION \"$(GIT_VERSION)\"" > $@
# Supported PostgreSQL versions:
PGVERSIONS = 14 15 16 17 18 19
# Default version:
PGVERSION ?= 19
# Base image that provides all Postgres versions + Citus builds.
# Override locally after building the base image under a different tag.
BASE ?= ghcr.io/hapostgres/pg_auto_failover/pgaf-base:bookworm
CONTAINER_NAME = pg_auto_failover
TEST_CONTAINER_NAME = pg_auto_failover_test
# --init runs Docker's built-in tini as PID 1 instead of the test's own
# `make` invocation. Without it, `make` (which installs its own SIGHUP
# handler to clean up partial targets on interrupt) is PID 1 itself, which
# strips away the kernel's usual "PID 1 ignores signals with no explicit
# handler" protection -- any stray SIGHUP reaching the container then kills
# the whole test run outright ("make: *** [Makefile:67: test] Hangup"),
# with no resilience at all. Seen recurring on pytest/single (PG19) across
# unrelated PRs; tini reaping/forwarding signals properly as a real PID 1
# is the standard fix for this class of Docker footgun.
DOCKER_RUN_OPTS = --init --privileged --rm
# DOCKER BUILDS
#
# make build-base — build the base image (all PG versions + Citus)
# make build — build the 'run' images for every supported PG version
# + the 'pgaftest' image (pgaf:pgaftest, using PGVERSION)
# make build-pg17 — build the 'run' image for PG 17 only
# make force-build — same as build but with --no-cache (busts Docker layer cache)
# make force-build-pg17 — force-rebuild the 'run' image for PG 17 only
# make build-pgaftest — build pgaf:pgaftest (test runner, uses PGVERSION)
# make force-build-pgaftest — same but with --no-cache (busts Docker layer cache)
# make build-pytest — build the 'test' (Python) image for every PG version
# make build-pytest-pg17 — build the 'test' image for PG 17 only
# make build-pytest-image — build the 'test' image for PGVERSION (default latest)
.PHONY: build-base
build-base:
docker build -f $(REPO_ROOT)Dockerfile.base -t $(BASE) $(REPO_ROOT)
BUILD_TARGETS = $(patsubst %,build-pg%,$(PGVERSIONS))
BUILD_PYTEST_TARGETS = $(patsubst %,build-pytest-pg%,$(PGVERSIONS))
BUILD_CHECK_TARGETS = $(patsubst %,build-check-pg%,$(PGVERSIONS))
.PHONY: build
build: $(BUILD_TARGETS) build-pgaftest ;
.PHONY: $(BUILD_TARGETS)
$(BUILD_TARGETS): version
docker build \
--build-arg PGVERSION=$(subst build-pg,,$@) \
--build-arg BASE=$(BASE) \
--target run \
-t $(CONTAINER_NAME):$(subst build-,,$@) $(REPO_ROOT)
.PHONY: build-check build-pytest build-pytest-image
build-check: $(BUILD_CHECK_TARGETS) ;
build-pytest: $(BUILD_PYTEST_TARGETS) ;
build-pytest-image: build-pytest-pg$(PGVERSION) ;
FORCE_BUILD_TARGETS = $(patsubst %,force-build-pg%,$(PGVERSIONS))
.PHONY: force-build
force-build: $(FORCE_BUILD_TARGETS) force-build-pgaftest ;
.PHONY: $(FORCE_BUILD_TARGETS)
$(FORCE_BUILD_TARGETS): version
docker build --no-cache \
--build-arg PGVERSION=$(subst force-build-pg,,$@) \
--build-arg BASE=$(BASE) \
--target run \
-t $(CONTAINER_NAME):$(subst force-build-,,$@) $(REPO_ROOT)
PGAFTEST_CONTAINER_NAME = pgaf
.PHONY: build-pgaftest
build-pgaftest: version
docker build \
--build-arg PGVERSION=$(PGVERSION) \
--build-arg BASE=$(BASE) \
--target pgaftest \
-t $(PGAFTEST_CONTAINER_NAME):pgaftest $(REPO_ROOT)
.PHONY: force-build-pgaftest
force-build-pgaftest: version
docker build --no-cache \
--build-arg PGVERSION=$(PGVERSION) \
--build-arg BASE=$(BASE) \
--target pgaftest \
-t $(PGAFTEST_CONTAINER_NAME):pgaftest $(REPO_ROOT)
.PHONY: $(BUILD_PYTEST_TARGETS)
$(BUILD_PYTEST_TARGETS): version
docker build \
--build-arg PGVERSION=$(subst build-pytest-pg,,$@) \
--build-arg BASE=$(BASE) \
--target test \
-t $(TEST_CONTAINER_NAME):pg$(subst build-pytest-pg,,$@) $(REPO_ROOT)
.SECONDEXPANSION:
.PHONY: $(BUILD_CHECK_TARGETS)
$(BUILD_CHECK_TARGETS): version $$(subst build-check-,build-test-,$$@)
docker run --rm \
-t $(TEST_CONTAINER_NAME):$(subst build-check-,,$@) \
pg_autoctl version --json | jq ".pg_version" | xargs echo $(subst build-check-,,$@):
# make save-pytest-image; compresses the pytest image to a .tar.zst archive.
# Used in CI to pass the built image to downstream test jobs as an artifact.
.PHONY: save-pytest-image
save-pytest-image:
docker save $(TEST_CONTAINER_NAME):pg$(PGVERSION) \
| zstd -T0 -3 > $(TEST_CONTAINER_NAME)-pg$(PGVERSION).tar.zst
# make load-pytest-image; decompresses and loads a .tar.zst image archive.
# Used in CI test jobs that download the image from a prior build job.
.PHONY: load-pytest-image
load-pytest-image:
zstd -d --stdout $(TEST_CONTAINER_NAME)-pg$(PGVERSION).tar.zst \
| docker load