Skip to content

Commit

Permalink
Merge pull request #38 from max-pfeiffer/feature/optimise_image_publi…
Browse files Browse the repository at this point in the history
…sh_workflow

Feature/optimise image publish workflow
  • Loading branch information
max-pfeiffer authored Sep 26, 2023
2 parents b71fac3 + 46b4902 commit 7b340be
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 103 deletions.
4 changes: 4 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
USE_LOCAL_CACHE_STORAGE_BACKEND=1
PYTHON_VERSION=3.9.18
OS_VARIANT=slim-bookworm
POETRY_VERSION=1.6.1
25 changes: 17 additions & 8 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-20.04
steps:
- name: Checkout Repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v4
with:
Expand Down Expand Up @@ -44,7 +44,7 @@ jobs:
poetry_version: ["1.4.2", "1.5.1", "1.6.1"]
steps:
- name: Checkout Repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Expose GitHub Runtime
uses: crazy-max/ghaction-github-runtime@v2
- name: Setup Python
Expand Down Expand Up @@ -86,12 +86,12 @@ jobs:
strategy:
fail-fast: false
matrix:
python_version: ["3.11.5"]
os_variant: ["slim-bookworm"]
poetry_version: ["1.6.1"]
python_version: ["3.9.18", "3.10.13", "3.11.5"]
os_variant: ["bookworm", "slim-bookworm"]
poetry_version: ["1.4.2", "1.5.1", "1.6.1"]
steps:
- name: Checkout Repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Expose GitHub Runtime
uses: crazy-max/ghaction-github-runtime@v2
- name: Setup Python
Expand Down Expand Up @@ -133,9 +133,15 @@ jobs:
- run-publish-image-tests
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
python_version: ["3.9.18", "3.10.13", "3.11.5"]
os_variant: ["bookworm", "slim-bookworm"]
poetry_version: ["1.4.2", "1.5.1", "1.6.1"]
steps:
- name: Checkout Repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Expose GitHub Runtime
uses: crazy-max/ghaction-github-runtime@v2
- name: Get Git Commit Tag Name
Expand All @@ -159,10 +165,13 @@ jobs:
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: |
poetry install --no-interaction --no-root
- name: Publish Image to Docker Hub
- name: Publish Images to Docker Hub
env:
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
DOCKER_HUB_PASSWORD: ${{ secrets.DOCKER_HUB_PASSWORD }}
PYTHON_VERSION: ${{ matrix.python_version }}
OS_VARIANT: ${{ matrix.os_variant }}
POETRY_VERSION: ${{ matrix.poetry_version }}
run: |
source .venv/bin/activate
python -m build.publish
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ repos:
rev: 23.9.1
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
- repo: local
hooks:
- id: pylint
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
[![codecov](https://codecov.io/gh/max-pfeiffer/python-poetry/branch/main/graph/badge.svg?token=WQI2SJJLZN)](https://codecov.io/gh/max-pfeiffer/python-poetry)
![pipeline workflow](https://github.com/max-pfeiffer/python-poetry/actions/workflows/pipeline.yml/badge.svg)
![Docker Image Size (latest semver)](https://img.shields.io/docker/image-size/pfeiffermax/python-poetry?sort=semver)
Expand Down
40 changes: 0 additions & 40 deletions build/docker-bake.hcl

This file was deleted.

8 changes: 5 additions & 3 deletions build/publish.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click

from build.utils import get_context
from build.utils import get_context, get_image_reference
from pathlib import Path
from python_on_whales import DockerClient, Builder

Expand Down Expand Up @@ -58,7 +58,9 @@ def main(
) -> None:
context: Path = get_context()

tag: str = f"{registry}/pfeiffermax/python-poetry:{version_tag}-poetry{poetry_version}-python{python_version}-{os_variant}"
image_reference: str = get_image_reference(
registry, version_tag, poetry_version, python_version, os_variant
)

platforms: list[str] = ["linux/amd64", "linux/arm64/v8"]
cache_to: str = "type=gha,mode=max"
Expand Down Expand Up @@ -86,7 +88,7 @@ def main(
"POETRY_VERSION": poetry_version,
"OFFICIAL_PYTHON_IMAGE": f"python:{python_version}-{os_variant}",
},
tags=tag,
tags=image_reference,
platforms=platforms,
builder=builder,
cache_to=cache_to,
Expand Down
12 changes: 9 additions & 3 deletions build/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ def get_context() -> Path:
return Path(__file__).parent.resolve()


def get_docker_bake_file() -> Path:
docker_bake_file: Path = Path(__file__).parent.resolve() / "docker-bake.hcl"
return docker_bake_file
def get_image_reference(
registry: str,
image_version: str,
poetry_version: str,
python_version: str,
os_variant: str,
) -> str:
reference: str = f"{registry}/pfeiffermax/python-poetry:{image_version}-poetry{poetry_version}-python{python_version}-{os_variant}"
return reference
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions tests/build_image/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from os import getenv
from time import sleep

import pytest
from python_on_whales import Builder, DockerClient

from tests.constants import (
CONTEXT,
REGISTRY_PASSWORD,
REGISTRY_USERNAME,
)
from build.utils import get_image_reference
from tests.constants import CONTEXT, REGISTRY_PASSWORD, REGISTRY_USERNAME
from tests.registry_container import DockerRegistryContainer
from os import getenv


@pytest.fixture(scope="package")
Expand Down Expand Up @@ -43,7 +41,9 @@ def image_reference(
os_variant: str = getenv("OS_VARIANT")
poetry_version: str = getenv("POETRY_VERSION")

tag: str = f"{registry}/pfeiffermax/python-poetry:{image_version}-poetry{poetry_version}-python{python_version}-{os_variant}"
image_reference: str = get_image_reference(
registry, image_version, poetry_version, python_version, os_variant
)
cache_scope: str = (
f"{image_version}-{poetry_version}-{python_version}-{os_variant}"
)
Expand All @@ -63,11 +63,11 @@ def image_reference(
"POETRY_VERSION": poetry_version,
"OFFICIAL_PYTHON_IMAGE": f"python:{python_version}-{os_variant}",
},
tags=tag,
tags=image_reference,
platforms=platforms,
builder=pow_buildx_builder,
cache_to=cache_to,
cache_from=cache_from,
push=True,
)
yield tag
yield image_reference
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from python_on_whales import DockerClient, Builder
from random import randrange

import pytest
from python_on_whales import Builder, DockerClient
from semver import VersionInfo


Expand Down
3 changes: 1 addition & 2 deletions tests/constants.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from pathlib import Path

from build.utils import get_context, get_docker_bake_file
from build.utils import get_context

SLEEP_TIME: float = 3.0
REGISTRY_USERNAME: str = "foo"
REGISTRY_PASSWORD: str = "bar"
BAKE_FILE: Path = get_docker_bake_file()
CONTEXT: Path = get_context()
Empty file added tests/publish_image/__init__.py
Empty file.
3 changes: 2 additions & 1 deletion tests/publish_image/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from os import getenv

import pytest
from click.testing import CliRunner
from os import getenv


@pytest.fixture(scope="package")
Expand Down
2 changes: 0 additions & 2 deletions tests/test.txt

This file was deleted.

33 changes: 1 addition & 32 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from dataclasses import dataclass
from pathlib import Path

from docker_image import reference
from python_on_whales import DockerClient
from pathlib import Path


@dataclass
Expand Down Expand Up @@ -51,34 +51,3 @@ def create_from_reference(cls, tag: str):
poetry_version=poetry_version,
python_version=python_version,
)


def create_version_tag_for_example_images(version: str, target: str) -> str:
version_tag: str = f"{version}-{target}"
return version_tag


def extract_image_references_from_build_config(build_config: dict) -> list[str]:
image_tags: list[str] = []
targets: dict = build_config["target"]
for value in targets.values():
image_tags.extend(value["tags"])
return image_tags


def generate_image_references(
bake_file: Path, context: Path, version: str
) -> list[str]:
pow_docker_client: DockerClient = DockerClient()
build_config: dict = pow_docker_client.buildx.bake(
targets=["python-poetry"],
files=[bake_file],
variables=dict(
REGISTRY="localhost:5000",
CONTEXT=str(context),
IMAGE_VERSION=version,
),
print=True,
)
images_tags = extract_image_references_from_build_config(build_config)
return images_tags

0 comments on commit 7b340be

Please sign in to comment.