## references:
# audit: https://github.com/actions-rs/audit-check
# "needs": https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idneeds

name: Release
on:
  push:
    tags:
      - "v[0-9]+.[0-9]+.[0-9]+"
      - "v[0-9]+.[0-9]+.[0-9]-alpha.[0-9]+"
      - "v[0-9]+.[0-9]+.[0-9]-beta.[0-9]+"
    paths-ignore:
      - "**/docs/**"
      - "**.md"

# NOTE: needs to stay in sync with ./build.yml
jobs:
  # call out to build.yml
  doing-a-build:
    uses: cargo-generate/cargo-generate/.github/workflows/build.yml@main
    secrets: inherit

  publish:
    name: post / cargo publish
    needs: doing-a-build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: setup | rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          default: true
          profile: minimal
      - uses: katyo/publish-crates@v2
        with:
          registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  # https://github.com/marketplace/actions/changelog-reader
  release:
    name: post / github release
    needs: publish
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: setup | rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          default: true
          profile: minimal
      - name: Get version from tag
        id: tag_name
        run: |
          echo ::set-output name=current_version::${GITHUB_REF#refs/tags/v}
        shell: bash
      - name: Get Changelog Entry
        id: changelog_reader
        uses: mindsers/changelog-reader-action@v2
        with:
          validation_depth: 10
          version: ${{ steps.tag_name.outputs.current_version }}
          path: ./CHANGELOG.md
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.CI_CARGO_GENERATE_RELEASE_GH_PAT }}
        with:
          # This pulls from the "Get Changelog Entry" step above, referencing it's ID to get its outputs object.
          # See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
          tag_name: ${{ github.ref }}
          release_name: Release ${{ steps.changelog_reader.outputs.version }}
          body: ${{ steps.changelog_reader.outputs.changes }}
          prerelease: ${{ steps.changelog_reader.outputs.status == 'prereleased' }}
          draft: ${{ steps.changelog_reader.outputs.status == 'unreleased' }}
