name: Release

{% if publish_registry -%}
# Generated with --publish-registry: this project publishes to crates.io
# in addition to attaching a built binary to a GitHub Release. Requires a
# CARGO_REGISTRY_TOKEN repo secret. Fill in Cargo.toml's `license`/
# `repository` placeholders before the first real publish — crates.io
# rejects a publish without a license.
{% else -%}
# Unlike targets::typescript's release.yml (semantic-release publishing to
# npm), this project sets `publish = false` in Cargo.toml — it's an
# application, not a library for reuse — so "release" means attaching a
# built binary to a GitHub Release on a version tag, mirroring how mcpify's
# own release.yml is triggered but skipping the crates.io-publish step that
# doesn't apply to an unpublished application binary. Re-run mcpify with
# --publish-registry if you want this project published to crates.io too.
{% endif -%}

on:
  push:
    tags:
      - "v*"

jobs:
  release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      packages: write
    steps:
      - uses: actions/checkout@v7
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2.9.1
      - run: cargo test
      - run: cargo build --release
      - uses: softprops/action-gh-release@v3.0.1
        with:
          files: target/release/{{ project_name }}
{%- if publish_registry %}
      - run: cargo publish --token {% raw %}${{ secrets.CARGO_REGISTRY_TOKEN }}{% endraw %}
{%- 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 %}
