name: Deploy Minter on push/merge

# Only trigger on PR/push and only for frontend files
on:
  # Trigger on PR close
  pull_request:
    types: [ closed ]
    paths:
      - 'minter/*.json'
      - 'minter/*.js'
      - 'minter/**'
      - '.github/workflows/deploy-minter.yml'
    branches:
      - main
  
  # Trigger on push to master (edge case, local merge)
  push:
    branches:
      - main
    paths:
      - 'minter/*.json'
      - 'minter/*.js'
      - 'minter/**'
      - '.github/workflows/deploy-minter.yml'

jobs:
  # Build the frontend giles
  build:
    name: Compile frontend

    runs-on: ubuntu-latest

    steps:
      # Get the repo files from current commit
      - name: Cloning repository
        uses: actions/checkout@v1

      - name: Read .nvmrc
        run: echo "##[set-output name=NVMRC;]$(cat .nvmrc)"
        id: nvm
        working-directory: minter

      - name: Set Node.js (.nvmrc)
        uses: actions/setup-node@v1
        with:
          node-version: "${{ steps.nvm.outputs.NVMRC }}"

      - name: Install dependencies
        run: npm i
        working-directory: minter

      - name: Build website files
        env:
          NODE_ENV: production
          CI: false
        run: npm run build
        working-directory: minter

      - name: Deploy to Firebase
        uses: w9jds/firebase-action@master
        with:
          args: deploy --only hosting:minter
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
