name: test

on: [push, pull_request]

jobs:
  unit:
    name: unit tests, node ${{ matrix.node }}
    runs-on: ubuntu-18.04
    strategy:
      matrix:
        node: [12, 14, 15, 16]
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Install Node ${{ matrix.node }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node }}
      - name: Install NPM credentials
        run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
        env:
          NPM_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
      - name: Get yarn cache
        id: yarn-cache
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - uses: actions/cache@v3
        with:
          path: ${{ steps.yarn-cache.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - name: Install NPM Packages
        run: yarn install
      - name: Check licenses
        run: yarn build:license-checker
      - name: Test
        run: yarn test
      - uses: 8398a7/action-slack@v2
        with:
          text: ${{ github.repository }} unit test
          status: ${{ job.status }}
          author_name: ${{ github.repository }} ${{ github.workflow }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
        if: always() && github.actor != 'dependabot[bot]'
      - name: Build
        run: yarn build
      - name: Publish
        run: npm publish
        if: startsWith(github.event.ref, 'refs/tags')
      - name: Upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: upload-artifacts
          path: var
        if: always()
