name: Build and Push Docker Image
run-name: ${{ gitea.actor }}
on:
  push:
    branches:
      - main

jobs:
  tests:
    uses: ./.gitea/workflows/test.yaml
  build:
    needs: [tests]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Use Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: 1.1.29

      - name: Install dependencies
        run: bun install

      - name: Build
        run: bun run build
        env:
          NODE_ENV: production
          VITE_CLERK_PUBLISHABLE_KEY: ${{ vars.CLERK_KEY }}
          SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}

      # - name: Create Sentry API
      #   uses: getsentry/action-release@v1
      #   env:
      #     SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
      #     SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
      #     SENTRY_PROJECT: api
      #   with:
      #     environment: production
      #     sourcemaps: ./build/api/

      # - name: Create Sentry App
      #   uses: getsentry/action-release@v1
      #   env:
      #     SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
      #     SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
      #     SENTRY_PROJECT: app
      #   with:
      #     environment: production
      #     sourcemaps: ./build/app/

      - name: Upload CLI to R2
        uses: shallwefootball/s3-upload-action@master
        with:
          aws_key_id: ${{ secrets.R2_ACCESS_ID }}
          aws_secret_access_key: ${{ secrets.R2_ACCESS_SECRET }}
          aws_bucket: ${{ secrets.R2_BUCKET }}
          endpoint: ${{ secrets.R2_URL }}
          source_dir: "build/cli"
          destination_dir: "cli"


      - name: Set docker chmod (temp solution)
        run: sudo chmod 666 /var/run/docker.sock

      - name: Docker - Login
        uses: docker/login-action@v1
        with:
          registry: git.hashdot.co
          username: ${{ secrets.DOCKER_USER }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build and Push Website
        run: bun docker:website

      - name: Build and Push API
        run: bun docker:api

      - name: Build and Push App
        run: bun docker:app

      - name: Build and Push Page
        run: bun docker:page