name: Release
run-name: Release ${{ github.event.inputs.tag }}

on:
  workflow_dispatch:
    inputs:
      tag:
        description: 'Tag to be released'
        required: true

jobs:
  check:
    name: preliminary-checks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Ensure version is properly set
        run: |
          test "$(npm -s run get-version)" = "${{ github.event.inputs.tag }}"
      - name: Give user instruction
        if: failure()
        run: echo "::error file=package.json,line=6::The tag ${{ github.event.inputs.tag }} must match the version"
  publish-github:
    runs-on: ubuntu-latest
    needs: check
    steps:
      - uses: actions/checkout@v6
      # Setup .npmrc file to publish to GitHub Packages
      - uses: actions/setup-node@v6
        with:
          node-version: '22'
          registry-url: 'https://npm.pkg.github.com'
      - run: npm install
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  publish-npm:
    runs-on: ubuntu-latest
    needs: check
    permissions:
      id-token: write
      contents: read
    environment: npmjs
    steps:
      - uses: actions/checkout@v6
      # Setup .npmrc file to publish to npmjs.org
      - uses: actions/setup-node@v6
        with:
          node-version: '24'
          registry-url: 'https://registry.npmjs.org'
      - run: npm install
      - run: npm publish --access public
  release-github:
    runs-on: ubuntu-latest
    needs:
    - publish-github
    - publish-npm
    steps:
      - name: Create Release
        uses: softprops/action-gh-release@v2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.event.inputs.tag }}
          name: Release ${{ github.event.inputs.tag }}
