name: Main Master Build

on:
  push:
    branches: [master]

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [12.x, 14.x]

    env:
      CI: true
      NODE_OPTIONS: --max-old-space-size=4096

    steps:
      - uses: actions/checkout@v2

      # Beginning of yarn setup, keep in sync between all workflows, see ci.yml
      - name: use node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
          registry-url: https://registry.npmjs.org/ # Needed for auth
      - name: cache all node_modules
        id: cache-modules
        uses: actions/cache@v2
        with:
          path: '**/node_modules'
          key: ${{ runner.os }}-v${{ matrix.node-version }}-node_modules-${{ hashFiles('yarn.lock', '**/package.json') }}
      - name: find location of global yarn cache
        id: yarn-cache
        if: steps.cache-modules.outputs.cache-hit != 'true'
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - name: cache global yarn cache
        uses: actions/cache@v2
        if: steps.cache-modules.outputs.cache-hit != 'true'
        with:
          path: ${{ steps.yarn-cache.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - name: yarn install
        run: yarn install --frozen-lockfile
      # End of yarn setup

      - name: lint
        run: yarn lerna -- run lint

      - name: type checking and declarations
        run: yarn tsc:full

      - name: build
        run: yarn build

      - name: verify type dependencies
        run: yarn lint:type-deps

      - name: test (and upload coverage)
        run: |
          yarn lerna -- run test -- --coverage
          bash <(curl -s https://codecov.io/bash)
          # Upload code coverage for some specific flags. Also see .codecov.yml
          bash <(curl -s https://codecov.io/bash) -f packages/core/coverage/* -F core
          bash <(curl -s https://codecov.io/bash) -f packages/core-api/coverage/* -F core-api

      # Publishes current version of packages that are not already present in the registry
      - name: publish
        if: matrix.node-version == '12.x'
        run: yarn lerna -- publish from-package --yes
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      # Tags the commit with the version in the core package if the tag doesn't exist
      - uses: Klemensas/action-autotag@1.2.3
        if: matrix.node-version == '12.x'
        with:
          GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
          package_root: 'packages/core'
          tag_prefix: 'v'

      - name: Discord notification
        if: ${{ failure() }}
        uses: Ilshidur/action-discord@0.2.0
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
        with:
          args: 'Master build failed https://github.com/{{GITHUB_REPOSITORY}}/actions/runs/{{GITHUB_RUN_ID}}'
