name: Build and push Doorman UI / API

on:
  push:
    branches: [main]

jobs:
  docker:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
        name: Check out code
      
      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: install dependencies
        run: bun install
      
      - name: run local integration test
        run: bun integ-test:local
        env:
          ACCOUNT_SID: ${{ secrets.TWILIO_ACCOUNT_SID }}
          AUTH_TOKEN: ${{ secrets.TWILIO_AUTH_TOKEN }}
          AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          DISCORD_BOT_TOKEN: ${{ secrets.DISCORD_BOT_TOKEN }}
          DISCORD_CLIENT_SECRET: ${{ secrets.DISCORD_CLIENT_SECRET }}

      - name: Deploy Doorman UI and API to staging
        run: bun run deploy-serverless:staging 
        env:
          ACCOUNT_SID: ${{ secrets.TWILIO_ACCOUNT_SID }}
          AUTH_TOKEN: ${{ secrets.TWILIO_AUTH_TOKEN }}
          AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          DISCORD_BOT_TOKEN: ${{ secrets.DISCORD_BOT_TOKEN }}
          DISCORD_CLIENT_SECRET: ${{ secrets.DISCORD_CLIENT_SECRET }}

      - name: Deploy Doorman Buzzer Client to staging
        run: bun run deploy-buzzer-client:staging 
        env:
          ACCOUNT_SID: ${{ secrets.TWILIO_ACCOUNT_SID }}
          AUTH_TOKEN: ${{ secrets.TWILIO_AUTH_TOKEN }}
      
      - name: run staging integration test
        run: bun integ-test:staging

      - name: Promote Doorman UI and API staging to prod
        run: bun run deploy-serverless
        env:
          ACCOUNT_SID: ${{ secrets.TWILIO_ACCOUNT_SID }}
          AUTH_TOKEN: ${{ secrets.TWILIO_AUTH_TOKEN }}

      - name: Promote Doorman Buzzer Client staging to prod
        run: bun run deploy-buzzer-client
        env:
          ACCOUNT_SID: ${{ secrets.TWILIO_ACCOUNT_SID }}
          AUTH_TOKEN: ${{ secrets.TWILIO_AUTH_TOKEN }}
