# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Build

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

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

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'yarn'
    - run: yarn
    - run: yarn lint
    - run: yarn build-all
    - run: yarn test

  release:
    if: ${{ github.event_name == 'push' }}
    needs: build

    runs-on: ubuntu-latest
    steps:
      - uses: GoogleCloudPlatform/release-please-action@v3
        id: release
        with:
          release-type: node

      - uses: actions/checkout@v3
        if: ${{ steps.release.outputs.release_created }}

      - uses: actions/setup-node@v3
        with:
          node-version: 14
          registry-url: 'https://registry.npmjs.org'
        if: ${{ steps.release.outputs.release_created }}

      - run: yarn
        if: ${{ steps.release.outputs.release_created }}

      - run: yarn build-all
        if: ${{ steps.release.outputs.release_created }}

      - run: yarn publish --access public
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NODE_AUTH_TOKEN: ${{secrets.NPM_AUTOMATION_TOKEN}}
        if: ${{ steps.release.outputs.release_created }}
          