Skip to content

Commit 182fa5e

Browse files
committed
Use a local registry for the multiarch images and then test each one
1 parent f908ed6 commit 182fa5e

4 files changed

Lines changed: 86 additions & 42 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ jobs:
3131
permissions:
3232
contents: read
3333
packages: write
34+
services:
35+
registry:
36+
image: registry:3
37+
ports:
38+
- 5000:5000
3439
strategy:
3540
fail-fast: false
3641
matrix:
@@ -47,6 +52,7 @@ jobs:
4752
- "9.6"
4853
env:
4954
DOCKER_PLATFORM: linux/amd64,linux/arm64
55+
LOCAL_REGISTRY: localhost:5000
5056
# Indicates what's the equivalent to tecnativa/postgres-autoconf:latest image
5157
LATEST_RELEASE: "18-alpine"
5258
DOCKER_REPO: "" # Set in the next step
@@ -62,10 +68,11 @@ jobs:
6268
- name: Set image repository name
6369
run: |
6470
if [ "$GITHUB_REPOSITORY" = "Tecnativa/docker-postgres-autoconf" ]; then
65-
echo "DOCKER_REPO=tecnativa/postgres-autoconf" >> "$GITHUB_ENV"
71+
DOCKER_REPO=tecnativa/postgres-autoconf
6672
else
67-
echo "DOCKER_REPO=${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV"
73+
DOCKER_REPO=${GITHUB_REPOSITORY,,}
6874
fi
75+
echo "DOCKER_REPO=$DOCKER_REPO" >> "$GITHUB_ENV"
6976
- uses: actions/checkout@v4
7077
- uses: actions/setup-python@v6
7178
- run: pip install -r tests/ci-requirements.txt
@@ -74,10 +81,19 @@ jobs:
7481
uses: docker/setup-qemu-action@v4
7582
- name: Set up Docker Buildx
7683
uses: docker/setup-buildx-action@v4
84+
with:
85+
driver-opts: network=host
7786
# Build images
78-
- run: ./hooks/build
87+
- name: Build images
88+
run: ./hooks/build
7989
# Test
80-
- run: python -m unittest tests.test -v
90+
- name: Test each platform
91+
run: |
92+
IFS=',' read -ra PLATFORMS <<< "$DOCKER_PLATFORM"
93+
for platform in "${PLATFORMS[@]}"; do
94+
echo "Testing platform: $platform"
95+
TEST_PLATFORM="$platform" python -m unittest tests.test -v
96+
done
8197
# Push
8298
- name: Push Docker Image to Docker Hub
8399
if: env.DOCKERHUB_TOKEN && env.DOCKERHUB_LOGIN

hooks/build

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,42 @@ from plumbum import FG, local
33
from plumbum.cmd import date, docker
44

55
# Check environment variables are present
6-
DOCKER_PLATFORM = local.env.get("DOCKER_PLATFORM", "linux/amd64,linux/arm64")
6+
DOCKER_PLATFORM = local.env.get("DOCKER_PLATFORM", "linux/amd64")
77
DOCKER_TAG = local.env["DOCKER_TAG"]
88
BASE_TAG = local.env.get("BASE_TAG", DOCKER_TAG)
99
REPO = local.env["DOCKER_REPO"]
1010
COMMIT = local.env.get("GIT_SHA1")
1111
DATE = date("--rfc-3339", "ns")
12-
13-
# Build image
14-
(
15-
docker[
16-
"buildx",
17-
"build",
18-
"--platform",
19-
local.env["DOCKER_PLATFORM"],
20-
"--build-arg",
21-
"VCS_REF={}".format(COMMIT),
22-
"--build-arg",
23-
"BUILD_DATE={}".format(DATE),
24-
"--build-arg",
25-
"BASE_TAG={}".format(BASE_TAG),
26-
"--tag",
27-
"{}:{}".format(REPO, DOCKER_TAG),
28-
".",
29-
]
30-
& FG
12+
LOCAL_REGISTRY = local.env.get("LOCAL_REGISTRY")
13+
IMAGE = (
14+
"%s/%s:%s" % (LOCAL_REGISTRY, REPO, DOCKER_TAG)
15+
if LOCAL_REGISTRY
16+
else "%s:%s" % (REPO, DOCKER_TAG)
3117
)
18+
PLATFORMS = [p.strip() for p in DOCKER_PLATFORM.split(",") if p.strip()]
19+
20+
build = docker[
21+
"buildx",
22+
"build",
23+
"--platform",
24+
DOCKER_PLATFORM,
25+
"--build-arg",
26+
"VCS_REF={}".format(COMMIT),
27+
"--build-arg",
28+
"BUILD_DATE={}".format(DATE),
29+
"--build-arg",
30+
"BASE_TAG={}".format(BASE_TAG),
31+
"--tag",
32+
IMAGE,
33+
".",
34+
]
35+
if LOCAL_REGISTRY:
36+
build = build["--push"]
37+
elif len(PLATFORMS) == 1:
38+
build = build["--load"]
39+
else:
40+
raise SystemExit(
41+
"Multi-platform builds require LOCAL_REGISTRY; "
42+
"set DOCKER_PLATFORM to one value for local --load builds."
43+
)
44+
(build & FG)

hooks/push

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ REPO = local.env["DOCKER_REPO"]
77
SUFFIX = local.env.get("DOCKER_REPO_SUFFIX", "")
88
VERSION = local.env["DOCKER_TAG"]
99

10-
# Log all locally available images; will help to pin images
11-
docker["image", "ls", "--digests", REPO] & FG
10+
def image_ref(registry, tag):
11+
return "%s/%s%s:%s" % (registry, REPO, SUFFIX, tag)
12+
13+
14+
source = image_ref(local.env["LOCAL_REGISTRY"], VERSION)
15+
dest_tags = [image_ref(REGISTRY, VERSION)]
16+
if VERSION == local.env.get("LATEST_RELEASE"):
17+
latest = "alpine" if VERSION.endswith("-alpine") else "latest"
18+
dest_tags.append(image_ref(REGISTRY, latest))
19+
20+
docker["buildx", "imagetools", "inspect", source] & FG
1221

13-
# Login in Docker Hub
1422
docker(
1523
"login",
1624
"--username",
@@ -20,13 +28,7 @@ docker(
2028
REGISTRY,
2129
)
2230

23-
# Push built images
24-
local_image = "%s:%s" % (REPO, VERSION)
25-
public_image = "%s/%s%s:%s" % (REGISTRY, REPO, SUFFIX, VERSION)
26-
docker["image", "tag", local_image, public_image] & FG
27-
docker["image", "push", public_image] & FG
28-
if VERSION == local.env.get("LATEST_RELEASE"):
29-
latest_version = "alpine" if VERSION.endswith("-alpine") else "latest"
30-
public_image = "%s/%s%s:%s" % (REGISTRY, REPO, SUFFIX, latest_version)
31-
docker["image", "tag", local_image, public_image] & FG
32-
docker["image", "push", public_image] & FG
31+
promote = docker["buildx", "imagetools", "create"]
32+
for dest in dest_tags:
33+
promote = promote["-t", dest]
34+
(promote[source] & FG)

tests/test.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@
2222
class PostgresAutoconfCase(unittest.TestCase):
2323
"""Test behavior for this docker image"""
2424

25+
@classmethod
26+
def _platform_args(cls):
27+
platform = os.environ.get("TEST_PLATFORM")
28+
if platform:
29+
return ("--platform", platform)
30+
return ()
31+
2532
@classmethod
2633
def setUpClass(cls):
27-
base_tag = local.env.get("BASE_TAG", local.env["DOCKER_TAG"])
28-
with local.cwd(local.cwd / ".."), local.env(BASE_TAG=base_tag):
29-
print("Building image")
30-
local["./hooks/build"] & FG
3134
repo = local.env.get("DOCKER_REPO", "tecnativa/postgres-autoconf")
32-
cls.image = f"{repo}:{local.env['DOCKER_TAG']}"
35+
tag = local.env["DOCKER_TAG"]
36+
registry = local.env.get("LOCAL_REGISTRY")
37+
cls.image = f"{registry}/{repo}:{tag}" if registry else f"{repo}:{tag}"
3338
cls.cert_files = ("client.ca.cert.pem", "server.cert.pem", "server.key.pem")
3439
return super().setUpClass()
3540

@@ -95,6 +100,7 @@ def _check_password_auth(self, host=None):
95100
docker(
96101
"container",
97102
"run",
103+
*self._platform_args(),
98104
"--network",
99105
"lan",
100106
"-e",
@@ -128,6 +134,7 @@ def _check_cert_auth(self):
128134
docker(
129135
"container",
130136
"run",
137+
*self._platform_args(),
131138
"--network",
132139
"wan",
133140
"-e",
@@ -165,6 +172,7 @@ def test_server_certs_var(self):
165172
self.postgres_container = docker(
166173
"container",
167174
"run",
175+
*self._platform_args(),
168176
"-d",
169177
"--network",
170178
"lan",
@@ -200,6 +208,7 @@ def test_server_certs_mount(self):
200208
self.postgres_container = docker(
201209
"container",
202210
"run",
211+
*self._platform_args(),
203212
"-d",
204213
"--network",
205214
"lan",
@@ -223,6 +232,7 @@ def test_no_certs_lan(self):
223232
self.postgres_container = docker(
224233
"container",
225234
"run",
235+
*self._platform_args(),
226236
"-d",
227237
"--network",
228238
"lan",
@@ -246,6 +256,7 @@ def test_no_certs_wan(self):
246256
self.postgres_container = docker(
247257
"container",
248258
"run",
259+
*self._platform_args(),
249260
"-d",
250261
"--network",
251262
"lan",
@@ -273,6 +284,7 @@ def test_certs_falsy_lan(self):
273284
self.postgres_container = docker(
274285
"container",
275286
"run",
287+
*self._platform_args(),
276288
"-d",
277289
"--network",
278290
"lan",
@@ -314,6 +326,7 @@ def test_hba_extra_rules_added(self):
314326
# Start the Postgres container with HBA_EXTRA_RULES
315327
self.postgres_container = docker(
316328
"run",
329+
*self._platform_args(),
317330
"-d",
318331
"--name",
319332
"postgres_test_hba_extra_rules",

0 commit comments

Comments
 (0)