# This is the workflow to trigger a release
#
name: Semantic release

on:
  # Allows you to call this workflow from other workflows
  workflow_call:
    secrets:
      GH_TOKEN:
        required: false

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

permissions:
  contents: read

jobs:
  release:
    name: '🏷️ Release'
    runs-on: ubuntu-latest

    permissions:
      contents: write
      issues: write
      pull-requests: write
      id-token: write

    environment:
      name: production
      url: https://github.com/${{ github.repository }}/releases/tag/v${{ steps.release.outputs.version }}

    outputs:
      version: ${{ steps.release.outputs.version }}

    steps:
      - name: '☁️ Checkout repository'
        uses: actions/checkout@v6
        with:
          ref: ${{ github.event.workflow_run.head_branch || github.event.pull_request.base.ref }}
          fetch-depth: 0
          persist-credentials: false

      - name: '⚙️ Use Node.js'
        uses: actions/setup-node@v6
        with:
          node-version-file: '.nvmrc'
          check-latest: true
          cache: 'npm'
          registry-url: 'https://registry.npmjs.org'

      - name: '⛓️ Install dependencies'
        run: npm ci --audit=false --prefer-offline --progress=false

      - name: '📦 Release'
        id: release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GH_TOKEN: ${{ secrets.GH_TOKEN || secrets.GITHUB_TOKEN }} # Needed to push to GitHub
          HUSKY: 0 # disable pre-commit hooks
        run: |
          npm run release
          echo "version=$(npm run env | grep npm_package_version | cut -d '=' -f 2)" >> $GITHUB_OUTPUT
