name: Portal CI

on:
  push:
    paths:
      - 'portal/**'
      - '.github/workflows/portal.yml'
  pull_request:
    paths:
      - 'portal/**'
      - '.github/workflows/portal.yml'

jobs:
  build:
    runs-on: ubuntu-latest
    
    defaults:
      run:
        working-directory: ./portal

    steps:
      - uses: actions/checkout@v3

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'
          cache: 'npm'
          cache-dependency-path: './portal/package-lock.json'

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm test

      - name: Build
        run: npm run build

      - name: Build and push Docker image
        if: github.ref == 'refs/heads/main'
        env:
          DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
        run: |
          docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
          docker build -t yourusername/portal:latest .
          docker push yourusername/portal:latest 