name: Release

on:
  push:
    branches: [master]

permissions:
  # `id-token` and `contents` are required for NPM Trusted Publishing with OIDC
  # @see https://docs.npmjs.com/trusted-publishers
  id-token: write
  # need write so CI can push tags / branches / create PRs
  contents: write
  pull-requests: write

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    env:
      CI: true
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GH_TOKEN }}

      - name: Setup environment
        id: setup
        run: |-
          echo "npm-cache-dir=$(npm config get cache)" >> ${GITHUB_OUTPUT}

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20
          check-latest: true
          registry-url: 'https://registry.npmjs.org' # required for NPM Trusted Publishing

      # Ensure npm >=11.5.1 installed for NPM Trusted Publishing
      - name: Updated npm
        run: npm install -g npm@latest

      - name: Install packages
        run: npm ci

      - name: Test All
        run: npm run test

      - name: Create Release or Publish
        id: changesets
        uses: changesets/action@v1
        with:
          publish: npm run release
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
