name: Push to Docker Hub

on: [workflow_dispatch]

jobs:
  backend-image:
    name: Build backend image
    runs-on: ubuntu-latest

    steps:
      - name: ☁️ Checkout source
        uses: actions/checkout@v3
      - name: 🔧 Set up QEMU
        uses: docker/setup-qemu-action@v2
      - name: 🔧 Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: 🐋 Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: 📦 Build backend and export to Docker
        uses: docker/build-push-action@v3
        with:
          load: true
          context: backend
          tags: infisical/backend:test
      - name: ⏻ Spawn backend container and dependencies
        run: |
          docker compose -f .github/resources/docker-compose.be-test.yml up --wait --quiet-pull
      - name: 🧪 Test backend image
        run: |
          ./.github/resources/healthcheck.sh infisical-backend-test
      - name: ⏻ Shut down backend container and dependencies
        run: |
          docker compose -f .github/resources/docker-compose.be-test.yml down
      - name: 🏗️ Build backend and push
        uses: docker/build-push-action@v3
        with:
          push: true
          context: backend
          tags: infisical/backend:latest
          platforms: linux/amd64,linux/arm64

  frontend-image:
    name: Build frontend image
    runs-on: ubuntu-latest

    steps:
      - name: ☁️ Checkout source
        uses: actions/checkout@v3
      - name: 🔧 Set up QEMU
        uses: docker/setup-qemu-action@v2
      - name: 🔧 Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: 🐋 Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: 📦 Build frontend and export to Docker
        uses: docker/build-push-action@v3
        with:
          load: true
          context: frontend
          tags: infisical/frontend:test
          build-args: |
            POSTHOG_API_KEY=${{ secrets.PUBLIC_POSTHOG_API_KEY }}
      - name: ⏻ Spawn frontend container
        run: |
          docker run -d --rm --name infisical-frontend-test infisical/frontend:test
      - name: 🧪 Test frontend image
        run: |
          ./.github/resources/healthcheck.sh infisical-frontend-test
      - name: ⏻ Shut down frontend container
        run: |
          docker stop infisical-frontend-test
      - name: 🏗️ Build frontend and push
        uses: docker/build-push-action@v3
        with:
          push: true
          context: frontend
          tags: infisical/frontend:latest
          platforms: linux/amd64,linux/arm64
          build-args: |
            POSTHOG_API_KEY=${{ secrets.PUBLIC_POSTHOG_API_KEY }}
