name: Release the backend on Production
on:
  release:
    types:
      - created

jobs:
  deploy:
    name: Release the backend
    runs-on: ubuntu-latest
    timeout-minutes: 20
    defaults:
      run:
        shell: bash
        working-directory: ./
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js 
        uses: actions/setup-node@v3
        with:
          node-version: 14.x
      - name: Increase max number of open files
        run: ulimit -n 65535
      - name: Cache
        uses: actions/cache@v3
        id: cache-node_modules
        with:
          path: |
            node_modules
            serverless/users/node_modules
          key: ${{ runner.os }}-yarn-cache-${{ hashFiles('**/yarn.lock') }}
      - name: Install dependencies
        if: steps.cache-node_modules.outputs.cache-hit != 'true'
        run: yarn install --frozen-lockfile --non-interactive
      - name: Apply hasura metadata
        uses: tibotiber/hasura-action@master
        with:
          args: metadata apply
        env:
          HASURA_ENDPOINT: ${{ secrets.HASURA_ENDPOINT_PRODUCTION }}
          HASURA_ADMIN_SECRET: ${{ secrets.HASURA_ADMIN_SECRET_PRODUCTION }}
          HASURA_WORKDIR: hasura
          HASURA_ENGINE_VERSION: v2.8.0
      - name: Apply hasura migrations
        uses: tibotiber/hasura-action@master
        with:
          args: migrate apply --all-databases
        env:
          HASURA_ENDPOINT: ${{ secrets.HASURA_ENDPOINT_PRODUCTION }}
          HASURA_ADMIN_SECRET: ${{ secrets.HASURA_ADMIN_SECRET_PRODUCTION }}
          HASURA_WORKDIR: hasura
          HASURA_ENGINE_VERSION: v2.8.0
      - name: Reload hasura metadata
        uses: tibotiber/hasura-action@master
        with:
          args: metadata reload
        env:
          HASURA_ENDPOINT: ${{ secrets.HASURA_ENDPOINT_PRODUCTION }}
          HASURA_ADMIN_SECRET: ${{ secrets.HASURA_ADMIN_SECRET_PRODUCTION }}
          HASURA_WORKDIR: hasura
          HASURA_ENGINE_VERSION: v2.8.0
      - name: Serverless deploy
        run: ENV=prod yarn run backend:deploy
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID_PROD }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_PROD }}  
      - name: Slack Notification Success
        if: success()
        uses: rtCamp/action-slack-notify@v2
        env:
          SLACK_CHANNEL: deploy
          SLACK_COLOR: '#2E7D32'
          SLACK_ICON: https://github.com/N2BBrasil.png?size=48
          SLACK_MESSAGE: ':rocket:'
          SLACK_TITLE: Backend migrations applied to Production successfully
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}  
      - name: Slack Notification Failure
        if: failure()
        uses: rtCamp/action-slack-notify@v2
        env:
          SLACK_CHANNEL: deploy
          SLACK_COLOR: '#FD5E53'
          SLACK_ICON: https://github.com/N2BBrasil.png?size=48
          SLACK_MESSAGE: ':sob:'
          SLACK_TITLE: Backend migrations can't be applied to Production
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
