# Cloonar TYPO3 Deployment Workflow
# 
# Reusable workflow for deploying TYPO3 projects with Deployer.
#
# Usage in project's .forgejo/workflows/deploy.yaml:
#   name: Deploy
#   on:
#     push:
#       branches: [main]
#   jobs:
#     deploy-stage:
#       uses: Cloonar/ci-templates/.forgejo/workflows/typo3-deploy.yaml@main
#       with:
#         target: stage
#         php_version: '8.3'
#       secrets:
#         deploy_key: ${{ secrets.STAGE_KEY }}

name: TYPO3 Deploy

on:
  workflow_call:
    inputs:
      target:
        description: 'Deployment target (stage/production)'
        required: true
        type: string
      task:
        description: 'Deployer task (release:create, release:switch, deploy)'
        required: false
        type: string
        default: 'deploy'
      php_version:
        description: 'PHP version'
        required: false
        type: string
        default: '8.3'
      node_version:
        description: 'Node.js version (for frontend builds)'
        required: false
        type: string
        default: '20'
      run_tests:
        description: 'Run static analysis before deploy'
        required: false
        type: boolean
        default: false
      build_frontend:
        description: 'Run npm build'
        required: false
        type: boolean
        default: false
      deployer_file:
        description: 'Path to deploy.php'
        required: false
        type: string
        default: './build/deploy.php'
    secrets:
      deploy_key:
        description: 'SSH private key for deployment'
        required: true

env:
  COMPOSER_ALLOW_SUPERUSER: 1

jobs:
  static-analysis:
    name: Static Analysis
    runs-on: ubuntu-latest
    if: ${{ inputs.run_tests }}
    
    steps:
      - uses: actions/checkout@v4

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ inputs.php_version }}
          tools: composer

      - name: Install dependencies
        run: composer install --prefer-dist --no-progress --ignore-platform-reqs

      - name: Run PHPStan
        run: composer test:phpstan || true
        continue-on-error: true

      - name: Run Psalm
        run: composer test:psalm || true
        continue-on-error: true

  build:
    name: Build
    runs-on: ubuntu-latest
    needs: [static-analysis]
    if: ${{ always() && (needs.static-analysis.result == 'success' || needs.static-analysis.result == 'skipped') }}

    steps:
      - uses: actions/checkout@v4

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ inputs.php_version }}
          tools: composer

      - name: Setup Node.js
        if: ${{ inputs.build_frontend }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ inputs.node_version }}
          cache: 'npm'

      - name: Install PHP dependencies
        run: composer install --prefer-dist --no-progress --no-dev --ignore-platform-reqs

      - name: Install Node dependencies
        if: ${{ inputs.build_frontend }}
        run: npm ci

      - name: Build frontend
        if: ${{ inputs.build_frontend }}
        run: npm run build

      - name: Create build artifact
        run: |
          tar -czf build.tar.gz \
            bin public packages config vendor build composer.json composer.lock \
            $([ -d "node_modules" ] && echo "node_modules") \
            $([ -d "dist" ] && echo "dist") \
            2>/dev/null || true

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: build-${{ github.sha }}
          path: build.tar.gz
          retention-days: 1

  deploy:
    name: Deploy to ${{ inputs.target }}
    runs-on: ubuntu-latest
    needs: [build]

    steps:
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ inputs.php_version }}

      - name: Download artifact
        uses: actions/download-artifact@v4
        with:
          name: build-${{ github.sha }}

      - name: Extract artifact
        run: |
          tar xf build.tar.gz
          rm build.tar.gz

      - name: Install SSH and rsync
        run: |
          apt-get update
          apt-get install -y openssh-client rsync

      - name: Deploy with Deployer
        uses: deployphp/action@v1
        with:
          deployer-binary: "./bin/dep"
          dep: --file=${{ inputs.deployer_file }} ${{ inputs.task }} ${{ inputs.target }}
          private-key: ${{ secrets.deploy_key }}
