From 9818072bee22394b2667a539917448e309b097e4 Mon Sep 17 00:00:00 2001 From: mdefende <mdefende@uab.edu> Date: Thu, 3 Oct 2024 15:47:28 -0500 Subject: [PATCH] overhaul to create a test package, publish it, and containerize it --- .gitlab-ci.yml | 61 +++++++++++++++++++++++++++------------------ Dockerfile | 17 ++++++++----- pak/__init__.py | 0 pak/hello.py | 2 ++ pyproject.toml | 12 +++++++++ tests/test_hello.py | 9 +++++++ 6 files changed, 71 insertions(+), 30 deletions(-) create mode 100644 pak/__init__.py create mode 100644 pak/hello.py create mode 100644 pyproject.toml create mode 100644 tests/test_hello.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ed08dec..546bcba 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,26 +1,39 @@ -docker-build: - # Use the official docker image. - image: docker:stable +stages: + - build + - test + - publish + +variables: + PACKAGE_NAME: "pak" + DOCKER_IMAGE: "$CI_REGISTRY_IMAGE/$PACKAGE_NAME" + +before_script: + - pip install setuptools + +build_package: stage: build - services: - - docker:26.0.1-dind - variables: - DOCKER_IMAGE_NAME: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG - DOCKER_TLS_CERTDIR: "/certs" - before_script: - - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY - # All branches are tagged with $DOCKER_IMAGE_NAME (defaults to commit ref slug) - # Default branch is also tagged with `latest` script: - - docker build --pull -t "$DOCKER_IMAGE_NAME" . - - docker push "$DOCKER_IMAGE_NAME" - - | - if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then - docker tag "$DOCKER_IMAGE_NAME" "$CI_REGISTRY_IMAGE:latest" - docker push "$CI_REGISTRY_IMAGE:latest" - fi - # Run this job in a branch where a Dockerfile exists - rules: - - if: $CI_COMMIT_BRANCH - exists: - - Dockerfile + - python setup.py sdist bdist_wheel + artifacts: + paths: + - dist/* + +test_package: + stage: test + script: + - pip install . # Install the package + - pytest # Run tests + +publish_package: + stage: publish + script: + - pip install twine # Install twine to publish the package + - twine upload dist/* # Publish the package to the GitLab package registry + +build_and_push_docker_image: + stage: publish + script: + - docker build -t $DOCKER_IMAGE:$CI_COMMIT_SHORT_SHA . + - docker push $DOCKER_IMAGE:$CI_COMMIT_SHORT_SHA + only: + - main # Only run on the main branch diff --git a/Dockerfile b/Dockerfile index 21ba29c..87e038d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,13 @@ -# Use a base image -FROM ubuntu:22.04 +FROM python:3.12-slim -RUN apt update && apt upgrade -y -RUN apt install -y python3.11 +# Set working directory +WORKDIR /app -# Print "Hello, World!" when the container starts -CMD echo "Hello, World!" && echo "Hello again!" \ No newline at end of file +# Copy the package files +COPY . . + +# Install the package +RUN pip install . + +# Command to run the package (adjust as needed) +CMD ["python", "-c", "from pak.hello import hello; print(hello())"] \ No newline at end of file diff --git a/pak/__init__.py b/pak/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pak/hello.py b/pak/hello.py new file mode 100644 index 0000000..b3219b6 --- /dev/null +++ b/pak/hello.py @@ -0,0 +1,2 @@ +def hello(): + return "Hello, World!" \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..c132e8f --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,12 @@ +[build-system] +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "pak" +version = "0.1.0" +description = "A simple Python package." +authors = [{ name = "Your Name", email = "your.email@example.com" }] +dependencies = [ + "unittest" +] \ No newline at end of file diff --git a/tests/test_hello.py b/tests/test_hello.py new file mode 100644 index 0000000..d78b64c --- /dev/null +++ b/tests/test_hello.py @@ -0,0 +1,9 @@ +import unittest +from mod.hello import hello + +class TestMyModule(unittest.TestCase): + def test_hello(self): + self.assertEqual(hello(), "Hello, World!") + +if __name__ == '__main__': + unittest.main() \ No newline at end of file -- GitLab