name: Storage Limit Email Notifications

on:
  schedule:
    # Every 6 hours
    - cron: '0 */6 * * *'
  # Including 'workflow_dispatch' here allows the job to be triggered manually,
  # as well as on the schedule.
  workflow_dispatch:

jobs:
  send-notifications:
    name: Send notifications
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        env: ['staging', 'production']
    timeout-minutes: 340
    steps:
      - uses: actions/checkout@v2
        with: 
          fetch-depth: 0
      - name: Checkout latest cron release tag
        run: |
          LATEST_TAG=$(git describe --tags --abbrev=0 --match='cron-*')
          git checkout $LATEST_TAG
      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: 16
      - name: Install dependencies
        uses: bahmutov/npm-install@v1
      - name: Run job
        env:
          DEBUG: '*'
          ENV: ${{ matrix.env }}
          STAGING_RO_PG_CONNECTION: ${{ secrets.STAGING_PG_CONNECTION }} # no replica for staging
          PROD_RO_PG_CONNECTION: ${{ secrets.PROD_RO_PG_CONNECTION }}
          PROD_PG_REST_JWT: ${{ secrets.PROD_PG_REST_JWT }}
          STAGING_PG_REST_JWT: ${{ secrets.STAGING_PG_REST_JWT }}
          PROD_PG_REST_URL: ${{ secrets.PROD_PG_REST_URL }}
          STAGING_PG_REST_URL: ${{ secrets.STAGING_PG_REST_URL }}
          MAILCHIMP_API_KEY: ${{ secrets.MAILCHIMP_API_KEY }}
          EMAIL_PROVIDER: ${{ (matrix.env == 'staging' && 'dummy') || '' }} # use dummy provider in staging, while using the default provider in prod.
        run: npm run start:storage -w packages/cron

