name: TheGame CI

on:
  pull_request:
    branches:
      - master
      - develop
  push:
    branches:
      - master
      - develop

jobs:
  test:
    name: Run Tests
    runs-on: ubuntu-latest

    steps:
      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action@0.4.0
        with:
          access_token: ${{ github.token }}
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 14.x

      - uses: actions/cache@v2
        with:
          path: '**/node_modules'
          key: nodeModules-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}

      - name: Install packages
        run: yarn --frozen-lockfile
        env:
          CI: true

      - name: Typechecking
        run: yarn typecheck
        env:
          CI: true

      - name: Linting
        run: yarn lint
        env:
          CI: true

      - name: Testing
        run: yarn test --ci --coverage
        env:
          CI: true

  build:
    name: Build
    runs-on: ubuntu-latest

    steps:
      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action@0.4.0
        with:
          access_token: ${{ github.token }}

      - uses: actions/checkout@v2

      - uses: actions/setup-node@v1
        with:
          node-version: 14.x

      - uses: actions/cache@v2
        with:
          path: '**/node_modules'
          key: nodeModules-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}

      - name: Install packages
        run: yarn --frozen-lockfile
        env:
          CI: true

      - name: Build Backend
        run: yarn backend:build
        env:
          CI: true

      # Spin up the backend / hasura / DB for the web build to point to
      - name: Run docker-compose
        run: docker-compose up --build -d
        env:
          DATABASE_USER: metagame
          DATABASE_PASSWORD: postgres
          DATABASE_DB: metagame-db
          HASURA_GRAPHQL_ADMIN_SECRET: so-secretish
          HASURA_PORT: 8080

      - name: Debug docker containers
        run: docker ps -a

      - name: Wait for Hasura
        run: yarn wait-on --timeout 60000 http-get://localhost:8080/healthz || (docker-compose ps hasura && docker-compose logs --tail 20 hasura)

      - name: Build Web
        run: yarn web:build
        env:
          CI: true
          NEXT_PUBLIC_GRAPHQL_URL: http://localhost:8080/v1/graphql
