name: build

on:
  pull_request:
    types: [opened, reopened, synchronize]
  push:
    branches: ["master"]

jobs:
  build_yarn:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14.x, 16.x, 18.x]

    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
          registry-url: https://registry.npmjs.org
          scope: "@payloadcms"
          always-auth: true
      - name: Cache node modules
        uses: actions/cache@v1
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-
      - run: yarn
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
      - run: yarn build

      - name: Component Tests
        run: yarn test:components
      - name: Integration Tests
        run: yarn test:int

      - name: Generate Payload Types
        run: yarn dev:generate-types fields

      - name: Generate GraphQL schema file
        run: yarn dev:generate-graphql-schema graphql-schema-gen

      - name: Install Playwright Browsers
        run: npx playwright install --with-deps
      - name: E2E Tests
        run: yarn test:e2e --bail

      - uses: actions/upload-artifact@v3
        if: always()
        with:
          name: test-results
          path: test-results/
          retention-days: 30

  install_npm:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16.x]

    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
          registry-url: https://registry.npmjs.org
          scope: "@payloadcms"
          always-auth: true
      - name: Cache node modules
        uses: actions/cache@v1
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-npm-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-npm-${{ env.cache-name }}-
            ${{ runner.os }}-npm-
            ${{ runner.os }}-
      - run: npm install
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
