From 053fd36e900781c76e3a83e389538003cb8a2e51 Mon Sep 17 00:00:00 2001
From: Hannes Feldt <feldt@astron.nl>
Date: Wed, 22 Nov 2023 13:07:02 +0100
Subject: [PATCH] Add docker image build

---
 .dockerignore                                |  4 ++++
 docker/ci-runner/Dockerfile                  |  2 +-
 docker/package/Dockerfile                    | 18 ++++++++++++++++++
 {{cookiecutter.project_slug}}/.gitlab-ci.yml | 17 +++++++++++++++--
 {{cookiecutter.project_slug}}/setup.cfg      |  4 ++--
 {{cookiecutter.project_slug}}/tox.ini        |  3 ++-
 6 files changed, 42 insertions(+), 6 deletions(-)
 create mode 100644 .dockerignore
 create mode 100644 docker/package/Dockerfile

diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000..67114cc
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,4 @@
+.tox
+build
+*.egg-info
+venv
diff --git a/docker/ci-runner/Dockerfile b/docker/ci-runner/Dockerfile
index 937fe86..7e9722a 100644
--- a/docker/ci-runner/Dockerfile
+++ b/docker/ci-runner/Dockerfile
@@ -1,4 +1,4 @@
 FROM python:3.11
 
 RUN python -m pip install --upgrade pip
-RUN pip install --upgrade cookiecutter tox twine
+RUN python -m pip install --upgrade cookiecutter tox twine
diff --git a/docker/package/Dockerfile b/docker/package/Dockerfile
new file mode 100644
index 0000000..86c87ac
--- /dev/null
+++ b/docker/package/Dockerfile
@@ -0,0 +1,18 @@
+ARG BUILD_ENV=no_copy
+
+FROM python:3.11 AS build_no_copy
+ADD requirements.txt .
+COPY . /work
+RUN rm -r /work/dist | true
+RUN python -m pip install --user tox
+WORKDIR /work
+RUN python -m tox -e build
+
+FROM python:3.11 AS build_copy
+COPY dist /work/dist
+
+FROM build_${BUILD_ENV} AS build
+
+FROM python:3.11-slim
+COPY --from=build /work/dist /dist
+RUN python -m pip install /dist/*.whl
diff --git a/{{cookiecutter.project_slug}}/.gitlab-ci.yml b/{{cookiecutter.project_slug}}/.gitlab-ci.yml
index 49fdfb6..45a5b3a 100644
--- a/{{cookiecutter.project_slug}}/.gitlab-ci.yml
+++ b/{{cookiecutter.project_slug}}/.gitlab-ci.yml
@@ -14,6 +14,7 @@ stages:
   # - build_extensions
   - test
   - package
+  - images
   - integration
   - publish # publish instead of deploy
 
@@ -77,7 +78,7 @@ secret_detection:
   before_script:
     - python --version # For debugging
     - python -m pip install --upgrade pip
-    - pip install --upgrade tox twine
+    - python -m pip install --upgrade tox twine
 
 # Run all unit tests for Python versions except the base image
 run_unit_tests:
@@ -88,7 +89,7 @@ run_unit_tests:
     - tox -e py3${PY_VERSION}
   parallel:
     matrix: # use the matrix for testing
-      - PY_VERSION: [7, 8, 9, 10]
+      - PY_VERSION: [8, 9, 10, 11, 12]
 
 # Run code coverage on the base image thus also performing unit tests
 run_unit_tests_coverage:
@@ -122,6 +123,18 @@ package_docs:
   script:
     - tox -e docs
 
+docker_build:
+  stage: images
+  image: docker:latest
+  needs:
+    - package_files
+  services:
+    - name: docker:dind
+  script:
+    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
+    - docker build . --build-arg BUILD_ENV=copy --tag $CI_REGISTRY_IMAGE/package_name:$CI_COMMIT_REF_SLUG
+    # - docker push $CI_REGISTRY_IMAGE/package_name:$CI_COMMIT_REF_SLUG
+
 run_integration_tests:
   stage: integration
   allow_failure: true
diff --git a/{{cookiecutter.project_slug}}/setup.cfg b/{{cookiecutter.project_slug}}/setup.cfg
index 906b08e..ac0ae10 100644
--- a/{{cookiecutter.project_slug}}/setup.cfg
+++ b/{{cookiecutter.project_slug}}/setup.cfg
@@ -15,11 +15,11 @@ classifiers =
     Programming Language :: Python
     Programming Language :: Python :: 3
     Programming Language :: Python :: 3 :: Only
-    Programming Language :: Python :: 3.7
     Programming Language :: Python :: 3.8
     Programming Language :: Python :: 3.9
     Programming Language :: Python :: 3.10
     Programming Language :: Python :: 3.11
+    Programming Language :: Python :: 3.12
     Topic :: Internet :: WWW/HTTP
     Topic :: Internet :: WWW/HTTP :: Dynamic Content
     Topic :: Scientific/Engineering
@@ -28,7 +28,7 @@ classifiers =
 [options]
 include_package_data = true
 packages = find:
-python_requires = >=3.7
+python_requires = >=3.8
 install_requires = file: requirements.txt
 
 [flake8]
diff --git a/{{cookiecutter.project_slug}}/tox.ini b/{{cookiecutter.project_slug}}/tox.ini
index a469b2d..0801d56 100644
--- a/{{cookiecutter.project_slug}}/tox.ini
+++ b/{{cookiecutter.project_slug}}/tox.ini
@@ -1,6 +1,6 @@
 [tox]
 # Generative environment list to test all supported Python versions
-envlist = py3{7,8,9,10},black,pep8,pylint
+envlist = py3{8,9,10,11,12},black,pep8,pylint
 minversion = 3.18.0
 
 [testenv]
@@ -55,4 +55,5 @@ commands =
 
 [testenv:build]
 usedevelop = False
+deps = build
 commands = {envpython} -m build
-- 
GitLab