name: 'CI/CD Pipeline'

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - '**'

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

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

      - name: Cache node modules
        uses: actions/cache@v1
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: ${{ runner.os }}-node-

      - run: npm ci
      - run: npm test

      - uses: actions/upload-artifact@v2
        with:
          name: coverage
          path: coverage/

  coverage:
    runs-on: ubuntu-latest
    needs: [test]
    if: ${{ success() && github.actor != 'dependabot[bot]' }}
    steps:
      - uses: actions/checkout@v2
      - uses: actions/download-artifact@v2
        with:
          name: coverage

      - uses: paambaati/codeclimate-action@v2.7.5
        env:
          CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}

  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

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

      - name: Cache node modules
        uses: actions/cache@v1
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: ${{ runner.os }}-node-

      - name: Cache Gatsby build output
        uses: actions/cache@v1
        with:
          path: public
          key: ${{ runner.os }}-public

      - run: npm ci
      - run: npm run build
        env:
          GATSBY_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/upload-artifact@v1
        if: github.ref == 'refs/heads/main'
        with:
          name: public
          path: public

  deploy:
    needs: build
    if: success() && github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - uses: actions/download-artifact@v1
        with:
          name: public
      - name: Deploy to S3
        run: npm run deploy:s3
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
