name: Release - Build & Push Containers

on:
  push:
    branches:
      - main
    paths:
      - 'apps/**'
      - '.containers/python/Dockerfile'
      - 'packages/**'
      - 'pyproject.toml'
      - 'uv.lock'

env:
  REGISTRY: gitea.ia-innovacion.work
  REGISTRY_PATH: innovacion/mayacontigo

jobs:
  detect-changes:
    runs-on: ubuntu-latest
    outputs:
      bursatil: ${{ steps.filter.outputs.bursatil }}
      egresos: ${{ steps.filter.outputs.egresos }}
      inversionistas: ${{ steps.filter.outputs.inversionistas }}
      normativa: ${{ steps.filter.outputs.normativa }}
      ocp: ${{ steps.filter.outputs.ocp }}
      pyme: ${{ steps.filter.outputs.pyme }}
      riesgos: ${{ steps.filter.outputs.riesgos }}
      voz-del-cliente: ${{ steps.filter.outputs.voz-del-cliente }}
    steps:
      - uses: actions/checkout@v4

      - name: Detect changed apps
        uses: dorny/paths-filter@v3
        id: filter
        with:
          base: main
          filters: |
            bursatil:
              - 'apps/bursatil/**'
            egresos:
              - 'apps/egresos/**'
            inversionistas:
              - 'apps/inversionistas/**'
            normativa:
              - 'apps/normativa/**'
            ocp:
              - 'apps/ocp/**'
            pyme:
              - 'apps/pyme/**'
            riesgos:
              - 'apps/riesgos/**'
            voz-del-cliente:
              - 'apps/voz-del-cliente/**'

  build-bursatil:
    needs: detect-changes
    if: needs.detect-changes.outputs.bursatil == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Log in to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ secrets.REGISTRY_USERNAME }}
          password: ${{ secrets.REGISTRY_PASSWORD }}
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: .containers/python/Dockerfile
          push: true
          tags: |
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-bursatil:latest
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-bursatil:${{ github.sha }}
          build-args: |
            PACKAGE=bursatil

  build-egresos:
    needs: detect-changes
    if: needs.detect-changes.outputs.egresos == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Log in to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ secrets.REGISTRY_USERNAME }}
          password: ${{ secrets.REGISTRY_PASSWORD }}
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: .containers/python/Dockerfile
          push: true
          tags: |
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-egresos:latest
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-egresos:${{ github.sha }}
          build-args: |
            PACKAGE=egresos

  build-inversionistas:
    needs: detect-changes
    if: needs.detect-changes.outputs.inversionistas == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Log in to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ secrets.REGISTRY_USERNAME }}
          password: ${{ secrets.REGISTRY_PASSWORD }}
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: .containers/python/Dockerfile
          push: true
          tags: |
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-inversionistas:latest
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-inversionistas:${{ github.sha }}
          build-args: |
            PACKAGE=inversionistas

  build-normativa:
    needs: detect-changes
    if: needs.detect-changes.outputs.normativa == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Log in to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ secrets.REGISTRY_USERNAME }}
          password: ${{ secrets.REGISTRY_PASSWORD }}
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: .containers/python/Dockerfile
          push: true
          tags: |
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-normativa:latest
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-normativa:${{ github.sha }}
          build-args: |
            PACKAGE=normativa

  build-ocp:
    needs: detect-changes
    if: needs.detect-changes.outputs.ocp == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Log in to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ secrets.REGISTRY_USERNAME }}
          password: ${{ secrets.REGISTRY_PASSWORD }}
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: .containers/python/Dockerfile
          push: true
          tags: |
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-ocp:latest
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-ocp:${{ github.sha }}
          build-args: |
            PACKAGE=ocp

  build-pyme:
    needs: detect-changes
    if: needs.detect-changes.outputs.pyme == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Log in to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ secrets.REGISTRY_USERNAME }}
          password: ${{ secrets.REGISTRY_PASSWORD }}
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: .containers/python/Dockerfile
          push: true
          tags: |
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-pyme:latest
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-pyme:${{ github.sha }}
          build-args: |
            PACKAGE=pyme

  build-riesgos:
    needs: detect-changes
    if: needs.detect-changes.outputs.riesgos == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Log in to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ secrets.REGISTRY_USERNAME }}
          password: ${{ secrets.REGISTRY_PASSWORD }}
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: .containers/python/Dockerfile
          push: true
          tags: |
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-riesgos:latest
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-riesgos:${{ github.sha }}
          build-args: |
            PACKAGE=riesgos

  build-voz-del-cliente:
    needs: detect-changes
    if: needs.detect-changes.outputs.voz-del-cliente == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Log in to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ secrets.REGISTRY_USERNAME }}
          password: ${{ secrets.REGISTRY_PASSWORD }}
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: .containers/python/Dockerfile
          push: true
          tags: |
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-voz-del-cliente:latest
            ${{ env.REGISTRY }}/${{ env.REGISTRY_PATH }}-voz-del-cliente:${{ github.sha }}
          build-args: |
            PACKAGE=voz-del-cliente
