name: 'CI'
on:
  pull_request:
    branches:
      - main

env:
  NX_CLOUD_DISTRIBUTED_EXECUTION: true
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
  pr:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'pull_request' }}
    steps:
      - uses: actions/checkout@v2.3.5
        name: Checkout main
        with:
          ref: ${{ github.event.pull_request.head.ref }}
          fetch-depth: 0

      - name: Derive appropriate SHAs for base and head for `nx affected` commands
        uses: nrwl/nx-set-shas@v2

      - uses: actions/setup-node@v2
        with:
          node-version: '16'
          registry-url: 'https://registry.npmjs.org'

      - name: Cache node modules
        uses: actions/cache@v2
        env:
          cache-name: cache-node-modules
        with:
          # npm cache files are stored in `~/.npm` on Linux/macOS
          path: ~/.npm
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npx nx affected:lint --parallel --max-parallel=3 --base=origin/main

      - name: Test
        run: npx nx affected:test --parallel --max-parallel=3 --base=origin/main
