# This workflow will do a clean install of node dependencies, 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: CI/CD

on:
  push:
    branches: ['develop', 'master']

env:
  SLACK_WEBHOOK:  ${{ secrets.SLACK_WEBHOOK }}
  SLACK_MESSAGE: ${{ github.repository }}

jobs:
  install:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Cache node modules
      uses: actions/cache@v2
      env:
        cache-name: cache-node-modules
      with:
        # npm cache files are stored in `~/.npm` on Linux/macOS
        path: node_modules
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-
    - name: Use Node.js
      uses: actions/setup-node@v1
      with:
        always-auth: true        
        node-version: '10.15.3'
        registry-url: https://registry.npmjs.org
    - name: Install dependencies
      run: npm ci
      env:
        NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
    - name: Notify Slack
      if: failure()
      uses: rtCamp/action-slack-notify@v2.0.2
      env:
        SLACK_COLOR: '#FF0000'
        SLACK_TITLE: '❌ Failed Install'
        MSG_MINIMAL: true
      
  test:
    runs-on: ubuntu-latest
    
    needs: [install]
    steps:
      - uses: actions/checkout@v2
      - name: Cache node modules
        uses: actions/cache@v2
        env:
          cache-name: cache-node-modules
        with:
          # npm cache files are stored in `~/.npm` on Linux/macOS
          path: node_modules
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-
      - name: Use Node.js
        uses: actions/setup-node@v1
        with:
         node-version: '10.15.3'
      - run: npm test
      - name: Notify Slack
        if: failure()
        uses: rtCamp/action-slack-notify@v2.0.2
        env:
          SLACK_COLOR: '#FF0000'
          SLACK_TITLE: '❌ Failed Install'
          MSG_MINIMAL: true
 
  build:
    runs-on: ubuntu-latest
    needs: [test]
    env:
      NODE_OPTIONS: --max_old_space_size=4096
    steps:
      - uses: actions/checkout@v2
      - name: Cache node modules
        uses: actions/cache@v2
        env:
          cache-name: cache-node-modules
        with:
          # npm cache files are stored in `~/.npm` on Linux/macOS
          path: node_modules
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-
      - name: Use Node.js
        uses: actions/setup-node@v1
        with:
          node-version: '10.15.3'
      - run: npm run build:prod
        if: ${{github.ref == 'refs/heads/master'}}
      - run: npm run build:dev
        if: ${{github.ref == 'refs/heads/develop'}}
      - name: Archive Production Artifact
        uses: actions/upload-artifact@master
        with:
          name: build
          path: build
      - name: Notify Slack
        if: failure()
        uses: rtCamp/action-slack-notify@v2.0.2
        env:
          SLACK_COLOR: '#FF0000'
          SLACK_TITLE: '❌ Failed Build'
          MSG_MINIMAL: true
  
  deploy:
    runs-on: ubuntu-latest
    needs: [build]
    steps:
      - uses: actions/checkout@v2
      - name: Download Artifact
        uses: actions/download-artifact@master
        with:
          name: build
          path: build
      - name: Deploy to Firebase (Prod)
        uses: w9jds/firebase-action@master
        with:
          args: deploy --only hosting --project=master
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
        if: ${{github.ref == 'refs/heads/master'}}
      - name: Deploy to Firebase (Stag)
        uses: w9jds/firebase-action@master
        with:
          args: deploy --only hosting --project=develop
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
        if: ${{github.ref == 'refs/heads/develop'}}
      - name: Notify Slack Fail
        if: failure()
        uses: rtCamp/action-slack-notify@v2.0.2
        env:
          SLACK_COLOR: '#FF0000'
          SLACK_TITLE: '❌ Failed Deploy'
          MSG_MINIMAL: true
      - name: Notify Slack Success
        if: success()
        uses: rtCamp/action-slack-notify@v2.0.2
        env:
          SLACK_TITLE: '✅ Successful Deploy'
          MSG_MINIMAL: true
