name: Build Upload Site

on:
  push:
  pull_request_target:

jobs:
  publish:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        if: github.event_name == 'push'

      - uses: actions/checkout@v2
        if: github.event_name != 'push'
        with:
          ref: ${{ github.event.pull_request.head.sha }}

      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: Cache Packages
        id: yarn-cache
        uses: actions/cache@v2
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Install and build
        run: |
          yarn install
          yarn build

      - name: Get Branch Name
        run: echo "BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_ENV

      - name: Get Deploy Name
        if: github.event_name == 'push'
        run: |
          echo "DEPLOY_NAME=${{ env.BRANCH_NAME }}" >> $GITHUB_ENV
          echo "PRODUCTION=${{ env.BRANCH_NAME == 'master' }}" >> $GITHUB_ENV


      - name: Get Deploy Name
        if: github.event_name != 'push'
        run: |
          echo "DEPLOY_NAME=deploy-preview-${{ github.event.number }}" >> $GITHUB_ENV
          echo "PRODUCTION=false" >> $GITHUB_ENV

      - name: Deploy to Netlify
        uses: nwtgck/actions-netlify@v1.1
        with:
          publish-dir: './docs/.vuepress/dist'
          production-deploy: ${{ env.PRODUCTION }}
          github-token: ${{ secrets.GITHUB_TOKEN }}
          deploy-message: 'Deploy ${{ env.DEPLOY_NAME }}@${{ github.sha }}'
          enable-commit-comment: false
          alias: ${{ env.DEPLOY_NAME }}
        env:
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
          NETLIFY_SITE_ID: ${{ secrets.SITE_ID }}