name: Portal CI

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

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    
    steps:
    - uses: actions/checkout@v3
    
    - name: Install Podman
      run: |
        sudo apt-get update
        sudo apt-get install -y podman

    - 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.portal-backend \
          -t ghcr.io/${{ github.repository_owner }}/container-mom-portal-backend:${{ 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-portal-backend:${{ 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-portal-backend:${{ github.sha }} ghcr.io/${{ github.repository_owner }}/container-mom-portal-backend:latest
        podman push ghcr.io/${{ github.repository_owner }}/container-mom-portal:latest
        podman push ghcr.io/${{ github.repository_owner }}/container-mom-portal-backend:latest 