name: Publish Packages

on:
  push:
    branches: [main]

jobs:
    version:
        runs-on: ubuntu-latest
        permissions:
            id-token: write # required for provenance https://docs.npmjs.com/generating-provenance-statements#publishing-packages-with-provenance-via-github-actions
            contents: write
            pull-requests: write

        steps:
            - name: Checkout
              uses: actions/checkout@v3

            - name: Install Node.js
              uses: actions/setup-node@v3
              with:
                node-version-file: ".nvmrc"

            - name: Cache yarn dependencies
              uses: actions/cache@v3
              id: cache-dependencies
              with:
                path: node_modules
                key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
                restore-keys: |
                    ${{ runner.os }}-yarn-

            - name: Install Dependencies
              if: steps.cache-dependencies.outputs.cache-hit != 'true'
              run: |
                yarn install --frozen-lockfile

            - name: Create release Pull Request or publish to NPM
              uses: changesets/action@v1
              with:
                  commit: "ci(changesets): version packages"
                  title: "Changeset: New Version"
                  publish: yarn publish-ci
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
                  NPM_TOKEN: ${{ secrets.npm_token }}
                  NODE_AUTH_TOKEN: ${{ secrets.npm_token }}
