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."