name: Push results of react build

on:
  push:
    branches: [  main ]
    paths-ignore:
      - label_sleuth/**

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
      - uses: wow-actions/use-app-token@v1
        id: generate_token
        with:
          app_id: ${{ secrets.APP_ID_PUSH_BOT }}
          private_key: ${{ secrets.PRIVATE_KEY_PUSH_BOT }}

      - name: Checkout project
        uses: actions/checkout@v3
        with:
          token: ${{ steps.generate_token.outputs.token }}

      - name: Setup Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
          cache-dependency-path: ./frontend/package-lock.json

      - name: Install our dependencies
        run: npm ci
        working-directory: ./frontend

      - name: Run js tests
        run: npm run test --if-present
        working-directory: ./frontend

      - name: Build static web site
        run: npm run build
        working-directory: ./frontend
        env:
          CI: false

      - name: remove old build
        run: rm -r label_sleuth/build

      - name: Move static web site inside label_sleuth
        run: mv frontend/build label_sleuth

      - name: Commit changes
        uses: EndBug/add-and-commit@v8
        with:
          author_name: ${{ github.actor }}
          author_email: ${{ github.actor }}@users.noreply.github.com
          message: 'Pushed latest compiled by github action bot following changes in /fronted'
          add: 'label_sleuth/build/'
