# https://github.com/changesets/action
name: Release
on:
  push:
    branches: [ main ]

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          # This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
          fetch-depth: 0

      - name: Use Node.js 16.x
        uses: actions/setup-node@v2
        with:
          node-version: "16.x"

      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: Cache node_modules
        uses: actions/cache@v2
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
            ${{ runner.os }}-yarn-
      - name: Install dependencies
        run: yarn --frozen-lockfile --network-timeout 300000

      - name: Build package
        run: yarn build

      - name: Create Release Pull Request or Publish to npm
        uses: actions/checkout@v2
        with:
          publish: yarn publish --access public
          version: yarn changeset version
          commit: "ci: release packages"
          title: "ci: release packages"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_AUTH }}
      
