name: ci-cd

on:
  push:
    branches:
      - '**/*'
  pull_request:
  release:
    types: [released]

jobs:
  unit:
    name: unit tests, node ${{ matrix.node }}
    runs-on: ubuntu-18.04
    strategy:
      matrix:
        node: [12, 14, 15, 16]
    timeout-minutes: 10
    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)"
      - name: Restore yarn cache
        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 --frozen-lockfile
      - name: Check licenses
        run: yarn build:license-checker
      - name: Test
        run: yarn test
      - name: Upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: var
          path: var
        if: always()
  publish:
    name: Publish
    runs-on: ubuntu-18.04
    needs: unit
    if: github.event_name == 'release'
    timeout-minutes: 10
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Install Node 14
        uses: actions/setup-node@v3
        with:
          node-version: 14
      - 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)"
      - name: Restore yarn cache
        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 --frozen-lockfile
      - name: Build
        run: yarn build
      - name: Publish
        run: npm publish
