name: Release

{% if publish_registry -%}
# Generated with --publish-registry: this project packs as a dotnet global
# tool and publishes to NuGet in addition to attaching a published
# framework-dependent build to a GitHub Release. Requires a NUGET_API_KEY
# repo secret. Fill in Project.csproj's Authors placeholder before the
# first real push.
{% else -%}
# Mirrors targets::python's/targets::rust's release.yml: this project is a
# generated application tied to one specific API, not a reusable library,
# so "release" means attaching a published framework-dependent build to a
# GitHub Release on a version tag rather than publishing to NuGet. Re-run
# mcpify with --publish-registry if you want this project published to
# NuGet too.
{% endif -%}

on:
  push:
    tags:
      - "v*"

jobs:
  release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      packages: write
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-dotnet@v5.4.0
        with:
          dotnet-version: "10.0.x"
      - run: dotnet restore
      - run: dotnet test
      - run: dotnet publish {{ namespace }}.csproj -c Release -o publish
      - run: (cd publish && zip -r ../{{ project_name }}.zip .)
      - uses: softprops/action-gh-release@v3.0.1
        with:
          files: {{ project_name }}.zip
{%- if publish_registry %}
      - run: dotnet pack {{ namespace }}.csproj -c Release -o nupkg
      - run: dotnet nuget push "nupkg/*.nupkg" --api-key {% raw %}${{ secrets.NUGET_API_KEY }}{% endraw %} --source https://api.nuget.org/v3/index.json
{%- endif %}
      # Publishes the Dockerfile's image to GHCR, versioned by the release
      # tag — a real, pullable artifact of the release, not just the CI
      # build-validation docker-build.yml does on every push/PR. Uses the
      # built-in GITHUB_TOKEN, so no extra secret is needed.
      - uses: docker/setup-buildx-action@v4.2.0
      - uses: docker/login-action@v4.4.0
        with:
          registry: ghcr.io
          username: {% raw %}${{ github.actor }}{% endraw %}
          password: {% raw %}${{ secrets.GITHUB_TOKEN }}{% endraw %}
      - name: Compute image name
        id: image
        # GHCR image names must be lowercase, but github.repository isn't
        # guaranteed to be (e.g. a mixed-case GitHub username/org).
        run: echo "name=ghcr.io/$(echo '{% raw %}${{ github.repository }}{% endraw %}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT"
      - uses: docker/build-push-action@v7.3.0
        with:
          context: .
          push: true
          tags: |
            {% raw %}${{ steps.image.outputs.name }}:${{ github.ref_name }}{% endraw %}
            {% raw %}${{ steps.image.outputs.name }}:latest{% endraw %}
