name: Release

on:
  push:
    branches:
      - main
    paths:
      - "packages/**"
  workflow_dispatch:

jobs:
  publish-gpr:
    name: Create Release and Publish to NPM
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Setup PNPM
        uses: pnpm/action-setup@v2
        with:
          version: 6.10.0

      - name: Setup Node
        uses: actions/setup-node@v2
        with:
          node-version: "16"
          cache: "pnpm"
          registry-url: "https://registry.npmjs.org"

      - name: Check access to NPM
        run: pnpm whoami
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

      - name: Config Git User
        run: |
          git config --global user.name "${{ github.actor }}"
          git config --global user.email "${{ github.actor }}@users.noreply.github.com"

      - name: Install dependencies
        run: pnpm install

      - name: Run test/lint/type-check and build
        run: |
          # We have to run build first because sometiems all we may have changed multiple packages
          # and want to use the latest versions (the ones available on this PR). As a result he
          # packages aren't in the NPM registry and are linked locally. It's not ideal but the flow
          # works. Worst case the build step fails because of type-checks being bad
          pnpm run build
          pnpm run check

      - name: Bump versions and publish packages
        run: |
          pnpm version-stable:ci
          pnpm publish:ci
        env:
          GH_TOKEN: ${{ secrets.GH_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
