name: RELEASE

on:
  workflow_dispatch:
  repository_dispatch:
    types: [make_release]
    branches:
      - main

permissions:
  contents: write

jobs:
  release:
    runs-on: ubuntu-latest
    timeout-minutes: 40
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Fetch full history
          ref: main
          clean: true  # Ensures a fresh checkout

      - name: Use Node.js 20.x
        uses: actions/setup-node@v4
        with:
          node-version: 20.x

      - name: Cache Node Modules
        uses: actions/cache@v4
        with:
          path: node_modules
          key: semantic-config-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            semantic-config-

      - name: Debug Git Branch
        run: |
          git status
          git branch -vv
          git remote -v
          git log --oneline -5

      - name: Ensure latest remote state
        run: |
          git fetch origin main --prune
          git status

      - name: Build Release
        env:
          CI: true
          GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
        run: |
          yarn install
          yarn release
