name: Build
on:
  push:
    branches:
      - master
      - staging
  pull_request:

jobs:
  build:
    name: Build and test on node ${{ matrix.node }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node: ['14', '16']
    env:
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
      NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v2
        with:
          fetch-depth: '0'
      - name: Setup Node
        uses: actions/setup-node@v2-beta
        with:
          node-version: ${{ matrix.node }}
          registry-url: 'https://registry.npmjs.org'
      - name: Get yarn cache dir
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: Cache Dependencies
        uses: actions/cache@v2
        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
        with:
          path: |
            ${{ steps.yarn-cache-dir-path.outputs.dir }}
            node_modules
            */*/node_modules
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Install dependencies
        run: yarn install  --frozen-lockfile

      - name: Build
        run: yarn build

      - name: Run Lint and Tests
        run: yarn test:full

  release:
    name: Release
    needs: build
    if: github.event_name == 'push' && github.ref == 'refs/heads/master'
    runs-on: ubuntu-latest
    env:
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v2
        with:
          fetch-depth: '0'

      - name: Setup Node
        uses: actions/setup-node@v2-beta
        with:
          node-version: '14'
          registry-url: 'https://registry.npmjs.org'

      - name: Get yarn cache dir
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: Cache Dependencies
        uses: actions/cache@v2
        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
        with:
          path: |
            ${{ steps.yarn-cache-dir-path.outputs.dir }}
            node_modules
            */*/node_modules
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Install dependencies
        run: yarn install --frozen-lockfile

      - name: Build
        run: yarn build

      - name: Run Lint and Tests
        run: yarn test:full

      - name: Upload coverage reports
        uses: codecov/codecov-action@v1

      - name: Configure CI Git User
        run: |
          git config --global user.name '@hirezio'
          git config --global user.email 'hirezio@users.noreply.github.com'

      - name: Check Authentication with Registry
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: npm whoami

      - name: Update Version and Publish to NPM
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: yarn semantic-release
