name: CI/CD Pipeline für michaelschiemer.de

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]

env:
  REGISTRY_URL: docker-registry:5000  # Via host-gateway mapping  
  IMAGE_NAME: michaelschiemer

jobs:
  test:
    runs-on: ubuntu-latest
    container:
      image: catthehacker/ubuntu:act-latest
      options: --add-host=gitea:host-gateway --add-host=docker-registry:host-gateway
    services:
      redis:
        image: redis:8-alpine
        options: --add-host=gitea:host-gateway --add-host=docker-registry:host-gateway
      mariadb:
        image: mariadb:latest
        env:
          MYSQL_ROOT_PASSWORD: test
          MYSQL_DATABASE: test
        options: --add-host=gitea:host-gateway --add-host=docker-registry:host-gateway

    steps:
    - name: Checkout Code
      uses: actions/checkout@v4

    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '18'

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.4'
        extensions: gd, zip, pdo, pdo_mysql, opcache, pcntl, posix, shmop, redis
        tools: composer

    - name: Install Dependencies
      run: composer install --no-progress --prefer-dist --optimize-autoloader

    - name: Build Frontend Assets
      run: npm install && npm run build

    - name: Run Tests
      run: ./vendor/bin/pest
      env:
        DB_HOST: mariadb
        DB_PORT: 3306
        DB_DATABASE: test
        DB_USERNAME: root
        DB_PASSWORD: test
        REDIS_HOST: redis
        REDIS_PORT: 6379

  build:
    needs: test
    runs-on: ubuntu-latest
    container:
      image: catthehacker/ubuntu:act-latest
      options: --add-host=gitea:host-gateway --add-host=docker-registry:host-gateway
    if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop'

    steps:
    - name: Checkout Code
      uses: actions/checkout@v4

    - name: Login to Private Registry
      run:  < /dev/null | 
        echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login ${{ env.REGISTRY_URL }} -u admin --password-stdin

    - name: Determine Image Tag
      id: tag
      run: |
        if [ "${{ github.ref }}" = "refs/heads/main" ]; then
          echo "tag=latest" >> $GITHUB_OUTPUT
        else
          echo "tag=develop" >> $GITHUB_OUTPUT
        fi

    - name: Build and Push Images
      run: |
        # Build and push PHP image
        if [ -f docker/php/Dockerfile ]; then
          docker build -t ${{ env.REGISTRY_URL }}/${{ env.IMAGE_NAME }}/php:${{ steps.tag.outputs.tag }} -f docker/php/Dockerfile .
          docker push ${{ env.REGISTRY_URL }}/${{ env.IMAGE_NAME }}/php:${{ steps.tag.outputs.tag }}
        fi

        # Build and push Nginx image
        if [ -f docker/nginx/Dockerfile ]; then
          docker build -t ${{ env.REGISTRY_URL }}/${{ env.IMAGE_NAME }}/nginx:${{ steps.tag.outputs.tag }} -f docker/nginx/Dockerfile .
          docker push ${{ env.REGISTRY_URL }}/${{ env.IMAGE_NAME }}/nginx:${{ steps.tag.outputs.tag }}
        fi

        # Build and push Worker image
        if [ -f docker/worker/Dockerfile ]; then
          docker build -t ${{ env.REGISTRY_URL }}/${{ env.IMAGE_NAME }}/worker:${{ steps.tag.outputs.tag }} -f docker/worker/Dockerfile .
          docker push ${{ env.REGISTRY_URL }}/${{ env.IMAGE_NAME }}/worker:${{ steps.tag.outputs.tag }}
        fi
