diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..2d16dd9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,21 @@ +.git/ +.gitea/ +.github/ +.idea/ +.vscode/ +.venv/ +venv/ +.pytest_cache/ +node_modules/ +storage/ +static/ +dist/ +build/ +*.egg-info/ +**/__pycache__ +*.pyc +*.sqlite3 +*.db +privatestudio/_version.py +privatestudio/settings/local.py +privatestudio/static/bundle/ diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml new file mode 100644 index 0000000..388e760 --- /dev/null +++ b/.gitea/workflows/build.yml @@ -0,0 +1,215 @@ +name: Build and publish + +on: + push: + branches: [ main ] + pull_request: + types: [opened, synchronize, reopened, closed] + workflow_dispatch: {} + +permissions: + contents: write + packages: write + +env: + REGISTRY: git.krugerprojects.com + IMAGE_NAME: skruger/privatestudio + +jobs: + test: + if: github.event.action != 'closed' + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + uses: astral-sh/setup-uv@v6 + with: + python-version: "3.13" + activate-environment: true + + - name: Install package + run: uv pip install -e ".[dev]" + + - name: Run tests + run: pytest + + build: + if: github.event.action != 'closed' + needs: test + runs-on: ubuntu-latest + outputs: + tag: ${{ steps.tag.outputs.tag }} + version: ${{ steps.tag.outputs.version }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Determine image tags + id: tag + run: | + set -euo pipefail + git fetch --tags + current_tag=$(git tag --points-at HEAD | head -n1) + if [ "$GITHUB_EVENT_NAME" = "pull_request" ]; then + PR_NUMBER=$(echo "$GITHUB_REF" | cut -d'/' -f3) + TAG="pr-${PR_NUMBER}" + VERSION="0.0.0.dev${PR_NUMBER}" + tags="${TAG} ${GITHUB_SHA::7}" + elif [ -n "$current_tag" ]; then + echo "Head already tagged: $current_tag" + TAG="$current_tag" + VERSION=${TAG#v} + tags="latest ${VERSION} ${GITHUB_SHA::7}" + else + latest_tag=$(git tag --sort=-v:refname | head -n1 || true) + if [ -z "$latest_tag" ]; then + latest_tag="v0.0.0" + fi + sem=${latest_tag#v} + IFS='.' read -r major minor patch <<< "${sem}" + major=${major:-0} + minor=${minor:-0} + patch=${patch:-0} + patch=$((patch+1)) + TAG="v${major}.${minor}.${patch}" + git config user.name "gitea-actions" + git config user.email "gitea-actions@git.krugerprojects.com" + git tag -a "$TAG" -m "Release $TAG" + git push origin "$TAG" + VERSION=${TAG#v} + tags="latest ${VERSION} ${GITHUB_SHA::7}" + fi + echo "Determined tag: $TAG, version: $VERSION, image tags: $tags" + echo "tag=$TAG" >> $GITHUB_OUTPUT + echo "version=$VERSION" >> $GITHUB_OUTPUT + echo "tags=$tags" >> $GITHUB_OUTPUT + + - name: Install dependencies + run: apt-get update && apt-get install -y podman + + - name: Login to registry + run: | + echo -e "${{ secrets.PACKAGE_ACCESS_TOKEN }}" | \ + podman login ${REGISTRY} -u "${{ vars.PACKAGE_USERNAME }}" --password-stdin + + - name: Build image + run: | + set -euo pipefail + IMAGE="${REGISTRY}/${IMAGE_NAME}" + buildah bud --userns=host -f Dockerfile \ + --build-arg VERSION="${{ steps.tag.outputs.version }}" \ + -t "${IMAGE}:build" . + for t in ${{ steps.tag.outputs.tags }}; do + echo "Tag: ${IMAGE}:${t}" + buildah tag "${IMAGE}:build" "${IMAGE}:${t}" + done + + - name: Push image + run: | + set -euo pipefail + IMAGE="${REGISTRY}/${IMAGE_NAME}" + for t in ${{ steps.tag.outputs.tags }}; do + echo "Push: ${IMAGE}:${t}" + buildah push "${IMAGE}:${t}" + done + + pypi: + # Only publish releases from main, and only when a PyPI token is configured. + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + needs: build + runs-on: ubuntu-latest + env: + PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }} + + steps: + - name: Check for PyPI token + run: | + if [ -z "$PYPI_TOKEN" ]; then + echo "PYPI_TOKEN secret is not set; skipping PyPI publish." + fi + + - name: Checkout repository + if: env.PYPI_TOKEN != '' + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Node + if: env.PYPI_TOKEN != '' + uses: actions/setup-node@v4 + with: + node-version: "24" + + - name: Build static bundle + if: env.PYPI_TOKEN != '' + run: | + mkdir -p privatestudio/static/bundle + npm ci + npm run build + + - name: Set up Python + if: env.PYPI_TOKEN != '' + uses: astral-sh/setup-uv@v6 + with: + python-version: "3.13" + activate-environment: true + + - name: Build distributions + if: env.PYPI_TOKEN != '' + env: + SETUPTOOLS_SCM_PRETEND_VERSION: ${{ needs.build.outputs.version }} + run: | + uv pip install build twine + python -m build + twine check dist/* + + - name: Publish to PyPI + if: env.PYPI_TOKEN != '' + env: + TWINE_USERNAME: __token__ + TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} + run: twine upload --non-interactive dist/* + + cleanup: + # Only run when a pull request is closed and merged + if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true + runs-on: ubuntu-latest + + steps: + - name: Install jq + run: | + apt-get update && apt-get install -y jq + + - name: Delete PR images from Gitea package registry + env: + GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SERVER: ${{ gitea.server_url }} + OWNER: skruger + PACKAGE: privatestudio + PR_NUMBER: ${{ github.event.pull_request.number }} + run: | + set -euo pipefail + echo "Cleaning up Gitea registry images for PR #$PR_NUMBER" + AUTH_HEADER="Authorization: token ${GITEA_TOKEN}" + LIST_URL="${SERVER}/api/v1/packages/${OWNER}?type=container&q=${PACKAGE}&limit=50" + VERSIONS_JSON=$(curl -s -H "$AUTH_HEADER" "$LIST_URL") + MATCHES=$(echo "$VERSIONS_JSON" | jq -r --arg pr "pr-$PR_NUMBER" \ + '.[] | select(.version == $pr) | .version') + if [ -z "$(echo "$MATCHES" | tr -d '\n')" ]; then + echo "No image versions found for PR #$PR_NUMBER" + exit 0 + fi + for VERSION in $MATCHES; do + echo "Deleting package version $VERSION" + curl -s -X DELETE -H "$AUTH_HEADER" \ + "${SERVER}/api/v1/packages/${OWNER}/container/${PACKAGE}/${VERSION}" \ + || echo "Failed to delete package version $VERSION" + done + echo "Cleanup complete." diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e1e63d9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,50 @@ +FROM node:24 AS nodebuild + +WORKDIR /build + +COPY package.json package-lock.json ./ +RUN npm ci + +COPY webpack.config.js ./ +COPY src ./src +COPY privatestudio ./privatestudio +RUN mkdir -p privatestudio/static/bundle && npm run build + + +FROM python:3.13-slim-trixie + +# Version used by setuptools_scm, since .git is not copied into the image. +ARG VERSION=0.0.0 + +RUN useradd --create-home privatestudio + +EXPOSE 8000 + +ENV PYTHONUNBUFFERED=1 \ + PORT=8000 \ + DJANGO_SETTINGS_MODULE=privatestudio.settings.production \ + STORAGE_PATH=/data/storage + +RUN pip install --no-cache-dir "gunicorn~=23.0" "psycopg2-binary~=2.9.11" + +WORKDIR /app + +COPY --chown=privatestudio:privatestudio . . +COPY --from=nodebuild --chown=privatestudio:privatestudio /build/privatestudio/static/bundle ./privatestudio/static/bundle + +RUN SETUPTOOLS_SCM_PRETEND_VERSION="${VERSION}" pip install --no-cache-dir . \ + && mkdir -p /data/storage && chown -R privatestudio:privatestudio /data /app + +USER privatestudio + +RUN python manage.py collectstatic --noinput --clear + +VOLUME ["/data"] + +# Migrating at startup keeps "docker run" simple. Use a release step instead if +# you run more than one container against the same database. +CMD set -xe; \ + python manage.py migrate --noinput; \ + exec gunicorn privatestudio.wsgi:application \ + --bind 0.0.0.0:${PORT} \ + -k gthread --workers 2 --threads 8 --timeout 600 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cc9b9e9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,28 @@ +BSD 3-Clause License + +Copyright (c) 2026, Shaun Kruger + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..a87c27a --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,3 @@ +# The JS/CSS bundle is built by npm and is not tracked in git, so +# setuptools_scm will not pick it up on its own. +graft privatestudio/static/bundle diff --git a/package.json b/package.json index 51b1788..4c288d3 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "build:digitalocean": "npm install --production=false && npm run build" }, "author": "Shaun Kruger", - "license": "ISC", + "license": "BSD-3-Clause", "private": true, "engines": { "node": "v24.22.x", diff --git a/privatestudio/settings/production.py b/privatestudio/settings/production.py index c7b85a6..b338d54 100644 --- a/privatestudio/settings/production.py +++ b/privatestudio/settings/production.py @@ -14,6 +14,13 @@ if os.environ.get("DATABASE_URL"): DATABASES = { "default": dj_database_url.config(), } +else: + DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": os.path.join("/data", "db.sqlite3"), + } + } CORS_ALLOWED_ORIGINS = [o for o in os.environ.get("CORS_ALLOWED_ORIGINS", "").split(",") if o] diff --git a/privatestudio/studio/templates/privatestudio/nav_header.html b/privatestudio/studio/templates/privatestudio/nav_header.html index e001428..707fc01 100644 --- a/privatestudio/studio/templates/privatestudio/nav_header.html +++ b/privatestudio/studio/templates/privatestudio/nav_header.html @@ -1,4 +1,4 @@ -