From c44f7ec72229dc14adb833c3152e8d45e6d94980 Mon Sep 17 00:00:00 2001 From: Matthew Daubney Date: Fri, 25 Sep 2026 14:29:49 +0100 Subject: [PATCH] Publish images, assets and the Python package from CI - Pushes to main and v* tags publish only after every check passes: API and UI images to the ProGet Docker feed, the OpenAPI spec, dashboards and Prometheus config to the asset feed - v* tags also publish the wheel/sdist to the ProGet PyPI feed - The version comes from the tag (main builds: +g) and __version__ reads it from package metadata - One ntfy notification per run summarising every job that ran and the image tags published Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW --- .gitea/workflows/ci.yml | 131 +++++++++++++++++++++++++++++++++++--- README.md | 15 +++-- src/scrobbler/__init__.py | 7 +- 3 files changed, 139 insertions(+), 14 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 9b2c6ba..1fc548e 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -1,12 +1,17 @@ name: CI +# Checks run on every push and PR. Pushes to main and v* tags then publish, but only +# once every check has passed: container images and the monitoring/API-docs bundle +# on both, the Python package on tags. Every run is reported to ntfy. on: push: branches: ["**"] - tags-ignore: ["**"] + tags: ["v*"] pull_request: workflow_dispatch: - workflow_call: + +env: + UV_VERSION: "0.7.21" jobs: lint: @@ -16,7 +21,7 @@ jobs: - uses: https://github.com/astral-sh/setup-uv@v6 with: # Exact version: avoids an unauthenticated GitHub API lookup for "latest". - version: "0.7.21" + version: ${{ env.UV_VERSION }} - run: uv sync --locked - run: uv run ruff check - run: uv run ruff format --check @@ -43,7 +48,7 @@ jobs: - uses: https://github.com/astral-sh/setup-uv@v6 with: # Exact version: avoids an unauthenticated GitHub API lookup for "latest". - version: "0.7.21" + version: ${{ env.UV_VERSION }} - run: uv sync --locked - name: Migrations are in sync with the models run: | @@ -72,17 +77,127 @@ jobs: - name: Dashboards are valid JSON run: for f in deploy/grafana/dashboards/*.json; do python3 -m json.tool "$f" > /dev/null; done - notify: + # ---- Publishing (main and v* tags only) ------------------------------------- + + images: needs: [lint, test, validate-monitoring] + if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) + runs-on: ubuntu-latest + outputs: + tags: ${{ steps.version.outputs.image_tags }} + steps: + - uses: actions/checkout@v4 + - uses: https://github.com/astral-sh/setup-uv@v6 + with: + version: ${{ env.UV_VERSION }} + - id: version + name: Work out the version and image tags + run: | + short="${GITHUB_SHA::7}" + if [[ "$GITHUB_REF" == refs/tags/v* ]]; then + version="${GITHUB_REF_NAME#v}" + tags="$version latest sha-$short" + else + version="$(uv version --short)+g$short" + tags="main sha-$short" + fi + uv version "$version" # baked into the image (scrobbler_build_info) + echo "version=$version" >> "$GITHUB_OUTPUT" + echo "image_tags=$tags" >> "$GITHUB_OUTPUT" + - name: Log in to ProGet + run: echo "${{ secrets.PKGS_PASSWORD }}" | docker login "${{ vars.PKGS_HOST }}" -u "${{ secrets.PKGS_USER }}" --password-stdin + - name: Build and push + env: + REGISTRY: ${{ vars.PKGS_HOST }}/${{ vars.PKGS_DOCKER_FEED }} + TAGS: ${{ steps.version.outputs.image_tags }} + run: | + for image in api ui; do + args=() + for tag in $TAGS; do args+=(-t "$REGISTRY/scrobbler-$image:$tag"); done + docker build -f "docker/$image.Dockerfile" "${args[@]}" \ + --label "org.opencontainers.image.revision=$GITHUB_SHA" \ + --label "org.opencontainers.image.version=${{ steps.version.outputs.version }}" . + for tag in $TAGS; do docker push "$REGISTRY/scrobbler-$image:$tag"; done + done + - if: always() + run: docker logout "${{ vars.PKGS_HOST }}" || true + + assets: + needs: [lint, test, validate-monitoring] + if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Upload API spec, dashboards and Prometheus config + env: + API_KEY: ${{ secrets.PKGS_API_KEY }} + BASE: https://${{ vars.PKGS_HOST }}/endpoints/${{ vars.PKGS_ASSET_FEED }}/content/scrobbler + run: | + if [[ "$GITHUB_REF" == refs/tags/v* ]]; then dirs="${GITHUB_REF_NAME#v}"; else dirs="main sha-${GITHUB_SHA::7}"; fi + files="docs/openapi.json docker-compose.yml deploy/prometheus/prometheus.yml deploy/prometheus/alerts.yml + deploy/grafana/provisioning/datasources/prometheus.yml deploy/grafana/provisioning/dashboards/dashboards.yml + $(ls deploy/grafana/dashboards/*.json)" + for dir in $dirs; do + for file in $files; do + curl -fsS -X PUT -H "X-ApiKey: $API_KEY" --data-binary "@$file" "$BASE/$dir/$file" + echo "uploaded $dir/$file" + done + done + + python-package: + needs: [lint, test, validate-monitoring] + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: https://github.com/astral-sh/setup-uv@v6 + with: + version: ${{ env.UV_VERSION }} + - run: uv version "${GITHUB_REF_NAME#v}" + - run: uv build + - name: Publish to ProGet + run: >- + uv publish + --publish-url "https://${{ vars.PKGS_HOST }}/pypi/${{ vars.PKGS_PYPI_FEED }}/legacy" + --username api --password "${{ secrets.PKGS_API_KEY }}" + + notify: + needs: [lint, test, validate-monitoring, images, assets, python-package] if: always() runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - id: summary + name: Summarise the run + env: + NEEDS: ${{ toJSON(needs) }} + IMAGE_TAGS: ${{ needs.images.outputs.tags }} + run: | + python3 - <<'PY' + import json, os + needs = json.loads(os.environ["NEEDS"]) + results = {job: info["result"] for job, info in needs.items()} + if "failure" in results.values(): + status = "failure" + elif "cancelled" in results.values(): + status = "cancelled" + else: + status = "success" + ran = [f"{job}: {result}" for job, result in results.items() if result != "skipped"] + lines = [" · ".join(ran)] + if os.environ.get("IMAGE_TAGS"): + lines.append("images: " + ", ".join(os.environ["IMAGE_TAGS"].split())) + ref = os.environ["GITHUB_REF"] + title = f"Release {os.environ['GITHUB_REF_NAME']}" if ref.startswith("refs/tags/") else f"CI {os.environ['GITHUB_REF_NAME']}" + with open(os.environ["GITHUB_OUTPUT"], "a") as out: + out.write(f"status={status}\ntitle={title}\n") + out.write("message<`; tags are tagged ``, `latest` and `sha-`. + - the OpenAPI spec, dashboards and Prometheus config are uploaded to the `scrobbler-assets` feed under `scrobbler//` or `scrobbler/main/`. +- **`v*` tags** also publish the wheel and sdist to the `scrobbler-python` PyPI feed. +- **Every run** is posted to the `ScrobblingService` topic on notify.daubney.dev. -The workflows need these repository secrets and variables: +The workflow needs these repository secrets and variables: - **Secrets**: `PKGS_USER`, `PKGS_PASSWORD`, `PKGS_API_KEY`, `NTFY_USER`, `NTFY_PASSWORD` - **Variables**: `PKGS_HOST`, `PKGS_DOCKER_FEED`, `PKGS_PYPI_FEED`, `PKGS_ASSET_FEED`, `NTFY_URL`, `NTFY_TOPIC` -To release, tag `vX.Y.Z` on `main`. The tag sets the package version. +To release, tag `vX.Y.Z` on `main` and push the tag. The tag sets the package and image version. diff --git a/src/scrobbler/__init__.py b/src/scrobbler/__init__.py index fe13d67..7949621 100644 --- a/src/scrobbler/__init__.py +++ b/src/scrobbler/__init__.py @@ -1,6 +1,11 @@ """Self-hosted, Last.fm-compatible music scrobbling service.""" -__version__ = "0.1.0" +from importlib.metadata import PackageNotFoundError, version + +try: + __version__ = version("scrobbler") # set from the release tag at build time +except PackageNotFoundError: # running from a source tree without installing + __version__ = "0.0.0+unknown" def create_app(config_object=None):