From a3fd5f212e602573a2998463ba7c992f254fe03d Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Tue, 18 Aug 2026 18:22:00 +0200 Subject: [PATCH] ci(docker): containerize application and add forgejo release workflow --- .dockerignore | 9 ++++ .forgejo/workflows/build-push.yml | 80 +++++++++++++++++++++++++++++++ .gitignore | 7 +++ Dockerfile | 18 +++++++ docker-compose.yml | 27 +++++++++++ 5 files changed, 141 insertions(+) create mode 100644 .dockerignore create mode 100644 .forgejo/workflows/build-push.yml create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..187f25c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,9 @@ +.git +.gitignore +.agents +.idea +__pycache__ +*.pyc +*.pyo +*.pyd +.pytest_cache diff --git a/.forgejo/workflows/build-push.yml b/.forgejo/workflows/build-push.yml new file mode 100644 index 0000000..3ee1c48 --- /dev/null +++ b/.forgejo/workflows/build-push.yml @@ -0,0 +1,80 @@ +name: Build, Push, and Release + +on: + push: + branches: + - main + - master + - 'ci/*' + tags: + - 'v*' + +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to Forgejo Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ gitea.server_url }} + username: ${{ gitea.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract Metadata for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ gitea.server_url }}/${{ gitea.repository }} + tags: | + type=raw,value=latest,enable=${{ gitea.ref == 'refs/heads/main' || gitea.ref == 'refs/heads/master' }} + type=ref,event=branch + type=ref,event=tag + type=sha,prefix= + + - name: Build and Push Docker Image + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max + + create-release: + needs: build-and-push + if: (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') || startsWith(github.ref, 'refs/tags/v') + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Create Release in Forgejo + run: | + if [ "${{ startsWith(github.ref, 'refs/tags/v') }}" = "true" ]; then + TAG_NAME="${GITHUB_REF#refs/tags/}" + RELEASE_NAME="Release ${TAG_NAME}" + else + TAG_NAME="v2.0.${{ github.run_number }}" + RELEASE_NAME="Phikipathia Release ${TAG_NAME}" + fi + + echo "Creating release ${RELEASE_NAME} with tag ${TAG_NAME}..." + + curl -s -X POST "${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/releases" \ + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ + -H "Content-Type: application/json" \ + -d "{ + \"tag_name\": \"${TAG_NAME}\", + \"target_commitish\": \"${{ github.sha }}\", + \"name\": \"${RELEASE_NAME}\", + \"body\": \"Phikipathia release for commit ${{ github.sha }}. Docker container images are published to the Forgejo Container Registry.\", + \"draft\": false, + \"prerelease\": false + }" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a8eb067 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.idea/ +__pycache__/ +*.pyc +*.pyo +*.pyd +.pytest_cache/ +.agents/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b2e0e21 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM python:3.11-slim + +WORKDIR /app + +# Prevent Python from writing .pyc files & buffer stdout/stderr +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + PORT=8000 \ + USER_AGENT="Phikipathia/1.0 (https://git.depeuter.dev/tdpeuter/2022ST-project-Phikipathia; tibo@depeuter.dev)" + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +EXPOSE 8000 + +CMD ["python", "server.py"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..fbe4ee7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,27 @@ +services: + phikipathia: + build: + context: . + dockerfile: Dockerfile + image: ${DOCKER_REGISTRY:-phikipathia}:${DOCKER_TAG:-latest} + container_name: phikipathia-app + restart: unless-stopped + ports: + - "${PORT:-8000}:8000" + environment: + - PORT=8000 + - USER_AGENT=${USER_AGENT:-Phikipathia/1.0 (https://git.depeuter.dev/tdpeuter/2022ST-project-Phikipathia; tibo@depeuter.dev)} + healthcheck: + test: ["CMD-SHELL", "python -c 'import urllib.request; urllib.request.urlopen(\"http://localhost:8000/\")'"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 5s + deploy: + resources: + limits: + cpus: '1.0' + memory: 512M + reservations: + cpus: '0.25' + memory: 128M