name: Deploy Dev Branch to AWS
on:
  push:
    branches: [ dev ]
  #pull_request:
    #branches: [ dev ]
jobs:
  deploy:
    name: Deploy to AWS
    runs-on: ubuntu-latest
    steps:
      - name: Check out code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14.x'

      - name: Build application
        run:
          CI=FALSE && yarn deploy-qa

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.QA_AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.QA_AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-2

      - name: Upload dist folder to S3
        run: |
          aws s3 sync --delete dist s3://web-qa-mongoose-br2wu78v1.rocketplantech.com --acl public-read
          aws s3 cp s3://web-qa-mongoose-br2wu78v1.rocketplantech.com/index.html s3://web-qa-mongoose-br2wu78v1.rocketplantech.com/index.html --cache-control "max-age=0, must-revalidate" --content-type "text/html" --metadata-directive REPLACE --acl public-read

      - name: Invalidate Cloudfront Distribution
        run:
          aws cloudfront create-invalidation --distribution-id=E1WZB3ALA7L1DR --paths /*

      - name: Upload artifacts
        uses: actions/upload-artifact@v2
        if: failure()
        with:
          name: Logs
          path: ./storage/logs
