name: Test and deploy to GitHub Pages

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Read Node.js & NPM versions
        run: |
          echo "##[set-output name=NODE;]$(cat package.json | jq -r '.engines.node')"
          echo "##[set-output name=NPM;]$(cat package.json | jq -r '.engines.npm')"
        id: versions

      - name: Use correct Node.js version
        uses: actions/setup-node@v2
        with:
          node-version: "${{ steps.versions.outputs.NODE }}"

      - name: Use correct NPM version
        run: npm i -g npm@"${{ steps.versions.outputs.NPM }}"

      - name: Install dependencies
        run: npm ci

      - name: Test before production
        run: npm test

      - name: Build tool
        run: npm run build

      - name: Deploy master to GitHub Pages
        uses: JamesIves/github-pages-deploy-action@2.0.0
        env:
          ACCESS_TOKEN: ${{ secrets.DEV_GITHUB_TOKEN }}
          BASE_BRANCH: master
          BRANCH: gh-pages
          FOLDER: dist
