name: release

on:
  push:
    branches:
      - master
jobs: 
  version:
    runs-on: ubuntu-latest
    outputs:
      tag: ${{ env.tag }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Get Version Tag
        run: |
          npm i -g --quiet standard-version

          git config user.name "GitHub Actions Bot"
          git config user.email "<>"

          # tag commit
          standard-version

          # get the tag
          tag=$(git describe --tags --abbrev=0)

          # output
          echo "tag=$tag" >> $GITHUB_ENV
          echo "next tag: $tag" >> $GITHUB_STEP_SUMMARY
  
  release:
    needs: version
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GH_ACTION_TADEPLOY_PAT }}
          ref: ${{ github.ref }}
          fetch-depth: 0

      - name: Release
        env:
          GH_TOKEN: ${{ secrets.GH_ACTION_TADEPLOY_PAT }}
        run: |
          git config user.name "GitHub Actions Bot"
          git config user.email "<>"

          tag="${{ needs.version.outputs.tag }}"

          # without 'v' prefix
          version=${tag#v}

          # Update package.json
          npm version ${version} --git-tag-version false
          
          git add package.json package-lock.json
          
          git commit -m "cd: release ${version}" -m "[skip ci]"

          # push changes
          git push origin --follow-tags

          # create release
          gh release create $tag --generate-notes

          echo "tagged released: $tag" >> $GITHUB_STEP_SUMMARY