name: Release the backend on Staging
on:
  push:
    branches:
      - develop
    paths:
      - 'serverless/**'
      - 'hasura/**'
  workflow_dispatch:
    inputs:
      git-ref:
        description: Git Ref (Optional)
        required: false

jobs:
  setup:
    name: Install dependencies
    runs-on: ubuntu-latest
    timeout-minutes: 5
    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: 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
  deploy:
    name: Release the backend
    runs-on: ubuntu-latest
    timeout-minutes: 15
    needs: setup
    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: 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: Increase max number of open files
        run: ulimit -n 65535
      - name: Apply hasura metadata
        uses: tibotiber/hasura-action@master
        with:
          args: metadata apply --log-level DEBUG
        env:
          HASURA_ENDPOINT: ${{ secrets.HASURA_ENDPOINT }}
          HASURA_ADMIN_SECRET: ${{ secrets.HASURA_ADMIN_SECRET }}
          HASURA_WORKDIR: hasura
          HASURA_ENGINE_VERSION: v2.8.0
          HASURA_GRAPHQL_ENABLE_TELEMETRY: false
          NODE_OPTIONS: ''
      - name: Apply hasura migrations
        uses: tibotiber/hasura-action@master
        with:
          args: migrate apply --all-databases
        env:
          HASURA_ENDPOINT: ${{ secrets.HASURA_ENDPOINT }}
          HASURA_ADMIN_SECRET: ${{ secrets.HASURA_ADMIN_SECRET }}
          HASURA_WORKDIR: hasura
          HASURA_ENGINE_VERSION: v2.8.0
          HASURA_GRAPHQL_ENABLE_TELEMETRY: false
      - name: Reload hasura metadata
        uses: tibotiber/hasura-action@master
        with:
          args: metadata reload --log-level DEBUG
        env:
          HASURA_ENDPOINT: ${{ secrets.HASURA_ENDPOINT }}
          HASURA_ADMIN_SECRET: ${{ secrets.HASURA_ADMIN_SECRET }}
          HASURA_WORKDIR: hasura
          HASURA_ENGINE_VERSION: v2.8.0
          HASURA_GRAPHQL_ENABLE_TELEMETRY: false
      - name: Serverless deploy
        run: ENV=stag yarn run backend:deploy
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID_STAG }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_STAG }}
      - name: Slack Notification Success
        if: success()
        uses: rtCamp/action-slack-notify@v2
        env:
          SLACK_CHANNEL: dev-streaming
          SLACK_COLOR: '#2E7D32'
          SLACK_ICON: https://github.com/N2BBrasil.png?size=48
          SLACK_MESSAGE: ':rocket:'
          SLACK_TITLE: Backend migrations applied to Staging successfully
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
      - name: Slack Notification Failure
        if: failure()
        uses: rtCamp/action-slack-notify@v2
        env:
          SLACK_CHANNEL: dev-streaming
          SLACK_COLOR: '#FD5E53'
          SLACK_ICON: https://github.com/N2BBrasil.png?size=48
          SLACK_MESSAGE: ':sob:'
          SLACK_TITLE: Backend migrations can't be applied to Staging
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
