name: Push to GitHub Container Registry

on:
  workflow_dispatch:
  push:
    branches:
      - main

env:
  BACKEND_IMAGE_NAME: agenta-backend
  WEB_IMAGE_NAME: agenta-web
  IMAGE_OWNER_NAME: agenta-ai
  REPOSITORY_USERNAME: ${{ github.actor }}

jobs:
  build-and-publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Clear Docker build cache
        run: docker builder prune --all --force

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ env.REPOSITORY_USERNAME }}
          password: ${{ secrets.DOCKER_GITHUB_SECRETS }}

      - name: Build, tag and push Backend image to Github Container Registry
        id: build-backend-image
        run: |
          cd agenta-backend
          docker build -t ghcr.io/${{ env.IMAGE_OWNER_NAME }}/${{ env.BACKEND_IMAGE_NAME }} -f Dockerfile.gh .
          docker push ghcr.io/${{ env.IMAGE_OWNER_NAME }}/${{ env.BACKEND_IMAGE_NAME }}

      - name: Build, tag and push Web image to Github Container Registry
        id: build-web-image
        run: |
          cd agenta-web
          docker build --no-cache -t ghcr.io/${{ env.IMAGE_OWNER_NAME }}/${{ env.WEB_IMAGE_NAME }} -f prod.gh.Dockerfile .
          docker push ghcr.io/${{ env.IMAGE_OWNER_NAME }}/${{ env.WEB_IMAGE_NAME }}

