name: release-tools

on:
  push:
    tags:
      - "v*"
  workflow_dispatch:
    inputs:
      version:
        description: "Version without v prefix (e.g. 2.8.8). Leave empty for current Cargo.toml."
        required: false
        type: string

permissions:
  contents: write
  packages: write

jobs:
  verify-version:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.version.outputs.version }}
      tag: ${{ steps.version.outputs.tag }}
      has_dockerfile: ${{ steps.version.outputs.has_dockerfile }}
    steps:
      - uses: actions/checkout@v4
      - id: version
        shell: bash
        run: |
          set -euo pipefail
          INPUT_VERSION="$(jq -r '.inputs.version // empty' "${GITHUB_EVENT_PATH}" 2>/dev/null || true)"
          CARGO_VERSION=$(grep -E '^version = ' Cargo.toml | head -1 | cut -d'"' -f2)
          if [[ "${GITHUB_REF_TYPE}" == "tag" ]]; then
            TAG="${GITHUB_REF_NAME}"
            TAG_VERSION="${TAG#v}"
            [[ "${TAG_VERSION}" == "${CARGO_VERSION}" ]] || (echo "Tag/Cargo version mismatch" && exit 1)
          elif [[ -n "${INPUT_VERSION}" ]]; then
            TAG="v${INPUT_VERSION}"
            [[ "${INPUT_VERSION}" == "${CARGO_VERSION}" ]] || (echo "Input/Cargo version mismatch" && exit 1)
          else
            TAG="v${CARGO_VERSION}"
          fi
          echo "version=${CARGO_VERSION}" >> "${GITHUB_OUTPUT}"
          echo "tag=${TAG}" >> "${GITHUB_OUTPUT}"
          if [[ -f Dockerfile ]]; then
            echo "has_dockerfile=true" >> "${GITHUB_OUTPUT}"
          else
            echo "has_dockerfile=false" >> "${GITHUB_OUTPUT}"
          fi

  publish-crate:
    runs-on: ubuntu-latest
    needs: verify-version
    env:
      CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - name: Publish crate
        if: ${{ env.CRATES_IO_TOKEN != '' }}
        env:
          CARGO_REGISTRY_TOKEN: ${{ env.CRATES_IO_TOKEN }}
        run: cargo publish
      - name: Skip publish (no token)
        if: ${{ env.CRATES_IO_TOKEN == '' }}
        run: echo "CRATES_IO_TOKEN is not set; skipping crate publish."

  publish-image:
    runs-on: ubuntu-latest
    needs: verify-version
    if: ${{ needs.verify-version.outputs.has_dockerfile == 'true' }}
    steps:
      - uses: actions/checkout@v4
      - uses: docker/setup-buildx-action@v3
      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/build-push-action@v5
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: |
            ghcr.io/noetl/tools:${{ needs.verify-version.outputs.version }}
            ghcr.io/noetl/tools:latest

  github-release:
    runs-on: ubuntu-latest
    needs: [verify-version, publish-image]
    if: ${{ always() && needs.verify-version.result == 'success' && (needs.publish-image.result == 'success' || needs.publish-image.result == 'skipped') }}
    steps:
      - uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ needs.verify-version.outputs.tag }}
          generate_release_notes: true
          body: |
            - crate: `noetl-tools`
