name: Portal CI

on:
  push:
    branches: [ main ]
    paths:
      - 'portal/**'
      - 'portal-backend/**'
  pull_request:
    branches: [ main ]
    paths:
      - 'portal/**'
      - 'portal-backend/**'

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
    
    - name: Build Portal Frontend
      run: |
        podman build \
          -f build/Dockerfile.portal \
          -t ghcr.io/${{ github.repository_owner }}/container-mom-portal:${{ github.sha }} \
          .

    - name: Build Portal Backend
      run: |
        podman build \
          -f build/Dockerfile.api \
          -t ghcr.io/${{ github.repository_owner }}/container-mom-api:${{ github.sha }} \
          .

    - name: Login to GitHub Container Registry
      if: github.event_name != 'pull_request'
      run: |
        echo "${{ secrets.GITHUB_TOKEN }}" | podman login ghcr.io -u ${{ github.actor }} --password-stdin

    - name: Push Images
      if: github.event_name != 'pull_request'
      run: |
        podman push ghcr.io/${{ github.repository_owner }}/container-mom-portal:${{ github.sha }}
        podman push ghcr.io/${{ github.repository_owner }}/container-mom-api:${{ github.sha }}
        podman tag ghcr.io/${{ github.repository_owner }}/container-mom-portal:${{ github.sha }} ghcr.io/${{ github.repository_owner }}/container-mom-portal:latest
        podman tag ghcr.io/${{ github.repository_owner }}/container-mom-api:${{ github.sha }} ghcr.io/${{ github.repository_owner }}/container-mom-api:latest
        podman push ghcr.io/${{ github.repository_owner }}/container-mom-portal:latest
        podman push ghcr.io/${{ github.repository_owner }}/container-mom-api:latest 