diff --git a/.gitignore b/.gitignore index 4ee58f0..c9ca684 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,9 @@ ENV/ output/ .coverage +# Build artifacts +git_info.json + # Django *.log # db.sqlite3 diff --git a/Dockerfile b/Dockerfile index 77f6d41..96dc576 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,21 @@ -# Use the official Python runtime image FROM python:3.13-slim +# Git info passed as build args (no .git directory needed) +ARG GIT_COMMIT_SHA="" +ARG GIT_COMMIT_MSG="" +ARG GIT_BRANCH="" + # Create the app directory RUN mkdir /app # Set the working directory inside the container WORKDIR /app -# Set environment variables +# Set environment variables # Prevents Python from writing pyc files to disk ENV PYTHONDONTWRITEBYTECODE=1 #Prevents Python from buffering stdout and stderr -ENV PYTHONUNBUFFERED=1 +ENV PYTHONUNBUFFERED=1 # Install system dependencies RUN apt-get update && \ @@ -19,17 +23,21 @@ RUN apt-get update && \ rm -rf /var/lib/apt/lists/* # Upgrade pip -RUN pip install --upgrade pip +RUN pip install --upgrade pip # Copy the Django project and install dependencies COPY requirements.txt /app/ -# run this command to install all dependencies +# run this command to install all dependencies RUN pip install --no-cache-dir -r requirements.txt # Copy the Django project to the container COPY . /app/ +# Write git info (outside /app so volume mounts don't hide it) +RUN printf '{"commit_sha": "%s", "commit_message": "%s", "branch": "%s"}\n' \ + "$GIT_COMMIT_SHA" "$GIT_COMMIT_MSG" "$GIT_BRANCH" > /etc/git_info.json + # Expose the Django port EXPOSE 8000 diff --git a/docker-compose.coolify.yml b/docker-compose.coolify.yml index adf8b81..8949868 100644 --- a/docker-compose.coolify.yml +++ b/docker-compose.coolify.yml @@ -12,7 +12,12 @@ services: - web web: - build: . + build: + context: . + args: + GIT_COMMIT_SHA: ${GIT_COMMIT_SHA:-} + GIT_COMMIT_MSG: ${GIT_COMMIT_MSG:-} + GIT_BRANCH: ${GIT_BRANCH:-} container_name: stashcast_web environment: STASHCAST_DATA_DIR: /app/data_docker @@ -23,7 +28,12 @@ services: - ./data_docker:/app/data_docker worker: - build: . + build: + context: . + args: + GIT_COMMIT_SHA: ${GIT_COMMIT_SHA:-} + GIT_COMMIT_MSG: ${GIT_COMMIT_MSG:-} + GIT_BRANCH: ${GIT_BRANCH:-} container_name: stashcast_worker command: python manage.py run_huey hostname: worker diff --git a/docker-compose.yml b/docker-compose.yml index 553c85a..0079a30 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,7 +13,12 @@ services: - web web: - build: . + build: + context: . + args: + GIT_COMMIT_SHA: ${GIT_COMMIT_SHA:-} + GIT_COMMIT_MSG: ${GIT_COMMIT_MSG:-} + GIT_BRANCH: ${GIT_BRANCH:-} container_name: stashcast_web environment: STASHCAST_DATA_DIR: /app/data_docker @@ -23,7 +28,12 @@ services: - ./data_docker:/app/data_docker worker: - build: . + build: + context: . + args: + GIT_COMMIT_SHA: ${GIT_COMMIT_SHA:-} + GIT_COMMIT_MSG: ${GIT_COMMIT_MSG:-} + GIT_BRANCH: ${GIT_BRANCH:-} container_name: stashcast_worker command: python manage.py run_huey depends_on: diff --git a/media/apps.py b/media/apps.py index acab66c..105d7ab 100644 --- a/media/apps.py +++ b/media/apps.py @@ -1,13 +1,72 @@ +import json +import logging +import subprocess +from pathlib import Path + from django.apps import AppConfig from django.utils import timezone +logger = logging.getLogger(__name__) + DEPLOY_TIME = timezone.now() +def _get_git_info(): + """Capture git info at startup for display on the About page. + + Tries two sources in order: + 1. git_info.json written during Docker build (for container deploys) + 2. Live git commands (for local development) + """ + info = {'commit_sha': '', 'commit_sha_short': '', 'commit_message': '', 'branch': ''} + + # Try reading from the build-time JSON file (Docker deployments) + # Check /etc first (survives volume mounts), then app dir (local builds) + for json_path in [Path('/etc/git_info.json'), Path(__file__).resolve().parent.parent / 'git_info.json']: + try: + data = json.loads(json_path.read_text()) + if data.get('commit_sha'): + info['commit_sha'] = data['commit_sha'] + info['commit_sha_short'] = data['commit_sha'][:7] + info['commit_message'] = data.get('commit_message', '') + info['branch'] = data.get('branch', '') + return info + except Exception: + pass + + # Fall back to live git commands (local development) + try: + info['commit_sha'] = subprocess.check_output( + ['git', 'rev-parse', 'HEAD'], stderr=subprocess.DEVNULL, text=True + ).strip() + info['commit_sha_short'] = info['commit_sha'][:7] + info['commit_message'] = subprocess.check_output( + ['git', 'log', '-1', '--pretty=%s'], stderr=subprocess.DEVNULL, text=True + ).strip() + info['branch'] = subprocess.check_output( + ['git', 'rev-parse', '--abbrev-ref', 'HEAD'], stderr=subprocess.DEVNULL, text=True + ).strip() + except Exception: + pass + return info + + +GIT_INFO = _get_git_info() + + class MediaConfig(AppConfig): name = 'media' default_auto_field = 'django.db.models.BigAutoField' def ready(self): """Import signals when the app is ready""" + if GIT_INFO['commit_sha_short']: + logger.info( + "StashCast commit=%s branch=%s message=%s", + GIT_INFO['commit_sha_short'], + GIT_INFO.get('branch', ''), + GIT_INFO.get('commit_message', ''), + ) + else: + logger.info("StashCast git info not available") diff --git a/media/templates/admin/preferences.html b/media/templates/admin/preferences.html index 5eca12f..bdabeec 100644 --- a/media/templates/admin/preferences.html +++ b/media/templates/admin/preferences.html @@ -244,6 +244,15 @@

{% trans "About" %}

{{ deploy_time|timesince }} {% trans "ago" %} ({{ deploy_time }}) + {% if git_info.commit_sha_short %} + — + + {{ git_info.commit_sha_short }} + + {% if git_info.branch %} + on {{ git_info.branch }} + {% endif %} + {% endif %}