name: Docker Build

on:
  push:
    branches:
      - main
      - develop
    paths:
      - 'services/iam-service/**'
      - 'apps/web-*/**'
  workflow_dispatch:

jobs:
  build-iam-service:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      
      - name: Build and push IAM Service
        uses: docker/build-push-action@v5
        with:
          context: ./services/iam-service
          push: true
          tags: |
            goodgo/iam-service:latest
            goodgo/iam-service:${{ github.sha }}
          cache-from: type=registry,ref=goodgo/iam-service:buildcache
          cache-to: type=registry,ref=goodgo/iam-service:buildcache,mode=max
  
  build-web-admin:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      
      - name: Build and push Web Admin
        uses: docker/build-push-action@v5
        with:
          context: ./apps/web-admin
          push: true
          tags: |
            goodgo/web-admin:latest
            goodgo/web-admin:${{ github.sha }}
          cache-from: type=registry,ref=goodgo/web-admin:buildcache
          cache-to: type=registry,ref=goodgo/web-admin:buildcache,mode=max

  build-web-client:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      
      - name: Build and push Web Client
        uses: docker/build-push-action@v5
        with:
          context: ./apps/web-client
          push: true
          tags: |
            goodgo/web-client:latest
            goodgo/web-client:${{ github.sha }}
          cache-from: type=registry,ref=goodgo/web-client:buildcache
          cache-to: type=registry,ref=goodgo/web-client:buildcache,mode=max
