Skip to content

Commit bae7ef8

Browse files
committed
exp
1 parent fbffb62 commit bae7ef8

4 files changed

Lines changed: 63 additions & 18 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ on:
99
# See https://crontab.guru/weekly
1010
- cron: 0 0 * * 0
1111

12+
# Forks: change DOCKER_REPO to your image name (e.g. myuser/postgres-autoconf).
13+
env:
14+
DOCKER_REPO: tecnativa/postgres-autoconf
15+
IS_PR: ${{ github.event_name == 'pull_request' }}
16+
PUBLISH: ${{ github.ref == 'refs/heads/master' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository) }}
17+
1218
jobs:
1319
pre-commit:
1420
runs-on: ubuntu-latest
@@ -42,12 +48,11 @@ jobs:
4248
- "10"
4349
- "9.6"
4450
env:
45-
# Indicates what's the equivalent to tecnativa/postgres-autoconf:latest image
51+
# Tag equivalent to ${DOCKER_REPO}:latest (see hooks/push)
4652
LATEST_RELEASE: "18-alpine"
47-
DOCKER_REPO: tecnativa/postgres-autoconf
4853
DOCKER_TAG: ${{ matrix.pg_version }}-alpine
4954
GIT_SHA1: ${{ github.sha }}
50-
IS_PR: ${{ github.event_name == 'pull_request' }}
55+
PG_VERSION: ${{ matrix.pg_version }}
5156
steps:
5257
- uses: actions/checkout@v4
5358
- uses: actions/setup-python@v6
@@ -67,18 +72,26 @@ jobs:
6772
- name: Tag Docker Image for PR
6873
if: env.IS_PR
6974
run: docker tag ${{ env.DOCKER_REPO }}:${{ matrix.pg_version }}-alpine ${{ env.DOCKER_REPO }}:${{ env.DOCKER_TAG }}
75+
- name: Set up QEMU
76+
if: env.PUBLISH && (secrets.DOCKERHUB_TOKEN != '' || secrets.BOT_TOKEN != '')
77+
uses: docker/setup-qemu-action@v3
78+
- name: Set up Docker Buildx
79+
if: env.PUBLISH && (secrets.DOCKERHUB_TOKEN != '' || secrets.BOT_TOKEN != '')
80+
uses: docker/setup-buildx-action@v3
7081
# Push
7182
- name: Push Docker Image to Docker Hub
72-
if: github.repository == 'Tecnativa/docker-postgres-autoconf' && (github.ref == 'refs/heads/master' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository))
83+
if: env.PUBLISH && secrets.DOCKERHUB_TOKEN != ''
7384
env:
7485
REGISTRY_HOST: docker.io
7586
REGISTRY_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
7687
REGISTRY_USERNAME: ${{ secrets.DOCKERHUB_LOGIN }}
88+
DOCKER_PLATFORM: linux/amd64,linux/arm64
7789
run: ./hooks/push
7890
- name: Push Docker Image to GitHub Registry
79-
if: github.repository == 'Tecnativa/docker-postgres-autoconf' && (github.ref == 'refs/heads/master' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository))
91+
if: env.PUBLISH && secrets.BOT_TOKEN != ''
8092
env:
8193
REGISTRY_HOST: ghcr.io
8294
REGISTRY_TOKEN: ${{ secrets.BOT_TOKEN }}
8395
REGISTRY_USERNAME: ${{ secrets.BOT_LOGIN }}
96+
DOCKER_PLATFORM: linux/amd64,linux/arm64
8497
run: ./hooks/push

hooks/build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ from plumbum.cmd import date, docker
44

55
# Check environment variables are present
66
DOCKER_TAG = local.env["DOCKER_TAG"]
7+
REPO = local.env.get("DOCKER_REPO", "tecnativa/postgres-autoconf")
78
COMMIT = local.env.get("GIT_SHA1")
89
DATE = date("--rfc-3339", "ns")
910

@@ -19,7 +20,7 @@ DATE = date("--rfc-3339", "ns")
1920
"--build-arg",
2021
"BASE_TAG={}".format(DOCKER_TAG),
2122
"--tag",
22-
"tecnativa/postgres-autoconf:{}".format(DOCKER_TAG),
23+
"{}:{}".format(REPO, DOCKER_TAG),
2324
".",
2425
]
2526
& FG

hooks/push

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#!/usr/bin/env python3
22
from plumbum import FG, local
3-
from plumbum.cmd import docker
3+
from plumbum.cmd import date, docker
44

55
REGISTRY = local.env.get("REGISTRY_HOST", "docker.io")
66
REPO = local.env["DOCKER_REPO"]
77
SUFFIX = local.env.get("DOCKER_REPO_SUFFIX", "")
88
VERSION = local.env["DOCKER_TAG"]
99

10+
PLATFORMS = local.env.get("DOCKER_PLATFORM")
11+
PG_VERSION = local.env.get("PG_VERSION", VERSION.split("-")[0])
12+
GIT_SHA1 = local.env.get("GIT_SHA1", "")
13+
1014
# Log all locally available images; will help to pin images
1115
docker["image", "ls", "--digests", REPO] & FG
1216

@@ -20,13 +24,39 @@ docker(
2024
REGISTRY,
2125
)
2226

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
27+
28+
def public_image(tag):
29+
return "%s/%s%s:%s" % (REGISTRY, REPO, SUFFIX, tag)
30+
31+
32+
def tags_to_push():
33+
tags = [VERSION]
34+
if VERSION == local.env.get("LATEST_RELEASE"):
35+
latest_version = "alpine" if VERSION.endswith("-alpine") else "latest"
36+
tags.append(latest_version)
37+
return tags
38+
39+
40+
if PLATFORMS:
41+
BUILD_DATE = date("--rfc-3339", "ns")
42+
cmd = docker[
43+
"buildx",
44+
"build",
45+
"--platform",
46+
PLATFORMS,
47+
"--build-arg",
48+
"BASE_TAG={}-alpine".format(PG_VERSION),
49+
"--build-arg",
50+
"VCS_REF={}".format(GIT_SHA1),
51+
"--build-arg",
52+
"BUILD_DATE={}".format(BUILD_DATE),
53+
]
54+
for tag in tags_to_push():
55+
cmd = cmd["--tag", public_image(tag)]
56+
(cmd["--push", "."] & FG)
57+
else:
58+
local_image = "%s:%s" % (REPO, VERSION)
59+
for tag in tags_to_push():
60+
image = public_image(tag)
61+
docker["image", "tag", local_image, image] & FG
62+
docker["image", "push", image] & FG

tests/test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def setUpClass(cls):
2727
with local.cwd(local.cwd / ".."):
2828
print("Building image")
2929
local["./hooks/build"] & FG
30-
cls.image = f"tecnativa/postgres-autoconf:{local.env['DOCKER_TAG']}"
30+
repo = local.env.get("DOCKER_REPO", "tecnativa/postgres-autoconf")
31+
cls.image = "{}:{}".format(repo, local.env["DOCKER_TAG"])
3132
cls.cert_files = ("client.ca.cert.pem", "server.cert.pem", "server.key.pem")
3233
return super().setUpClass()
3334

0 commit comments

Comments
 (0)