name: Publish
on:
  push:
    branches:
      - main

concurrency: ${{ github.workflow }}-${{ github.ref }}

permissions:
  contents: write
  pull-requests: write

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: 10
      - uses: actions/setup-node@v4
        with:
          node-version: 18.x
          cache: "pnpm"
      - name: Get current version
        id: get-version
        run: echo "version=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT
      - run: pnpm install --frozen-lockfile
      - run: pnpm build
      - name: Create Release Pull Request or Publish
        id: changesets
        uses: changesets/action@v1
        with:
          publish: pnpm run ci:publish
          version: pnpm run ci:version
          title: "[Release] ${{ steps.changesets.outputs.version || 'Next' }}"
          commit: "release: Release ${{ steps.changesets.outputs.version || 'Next' }}"
          createGithubReleases: false # this is handled inside ci:publish script
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}