name: release-on-tag
run-name: Build and release on tag
permissions:
  contents: write
on:
  push:
    tags:
      - v**
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v3
        with:
          node-version: 18
      - name: Build card
        run: yarn && yarn build
      - name: Check if tag is a prerelease
        id: check-tag
        run: |
          if [[ ${{ github.event.ref }} =~ ^refs/tags/v[0-9]+\.[0-9]+\.[0-9]+\-.+$ ]]; then
              echo "prerelease=true" >> $GITHUB_OUTPUT
          fi

      - name: Get latest non-prerelease
        uses: cardinalby/git-get-release-action@v1
        id: get-latest-non-prerelease
        env:
          GITHUB_TOKEN: ${{ github.token }}
        with:
          latest: 1
          draft: false
          prerelease: false
      - name: Get latest release
        uses: cardinalby/git-get-release-action@v1
        id: get-latest-release
        env:
          GITHUB_TOKEN: ${{ github.token }}
        with:
          latest: 1
          draft: false

      - name: Generate CHANGELOG for release
        if: steps.check-tag.outputs.prerelease != true
        run: npx -y git-changelog-command-line -fr refs/tags/${{ steps.get-latest-non-prerelease.outputs.tag_name }} -of CHANGELOG-release.md

      - name: Generate CHANGELOG for prerelease
        if: steps.check-tag.outputs.prerelease
        run: npx -y git-changelog-command-line -fr refs/tags/${{ steps.get-latest-release.outputs.tag_name }} -of CHANGELOG-release.md

      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          body_path: CHANGELOG-release.md
          append_body: true
          generate_release_notes: true
          prerelease: "${{ steps.check-tag.outputs.prerelease }}"
          files: |
            dist/*
