image: php:8.2-cli


options:
  max-time: 30
  size: 2x

pipelines:
  branches:
    main:
      - step:
          name: Build & Deploy to VPS
          caches:
            - composer
            - node
          script:
            - echo ">>> Install system tools"
            - apt-get update
            - apt-get install -y curl unzip git lftp sshpass ca-certificates

            - echo ">>> Install PHP & Composer in pipeline"
            - apt-get install -y php php-cli php-mbstring php-xml php-curl php-gd php-intl php-zip php-mysql
            - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
            - composer --version
            - php -v

            - echo ">>> Install Node.js (for Vite/Laravel Mix if present)"
            - curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
            - apt-get install -y nodejs
            - node -v
            - npm -v

            - echo ">>> Composer install (no-dev, optimized)"
            - COMPOSER_MEMORY_LIMIT=-1 composer install --no-dev --prefer-dist --optimize-autoloader

            - echo ">>> Build frontend if package.json exists"
            - |
              if [ -f package.json ]; then
                if [ -f package-lock.json ]; then npm ci; else npm install; fi
                npm run build || echo "No build script or build not required."
              else
                echo "No package.json found, skipping frontend build."
              fi

            - echo ">>> Create .env from Bitbucket variables"
            - |
              cat > .env <<'EOF'
              APP_NAME=${APP_NAME}
              APP_ENV=${APP_ENV}
              APP_DEBUG=false
              APP_URL=${APP_URL}

              LOG_CHANNEL=stack
              LOG_LEVEL=info

              DB_CONNECTION=${DB_CONNECTION}
              DB_HOST=${DB_HOST}
              DB_PORT=${DB_PORT}
              DB_DATABASE=${DB_DATABASE}
              DB_USERNAME=${DB_USERNAME}
              DB_PASSWORD=${DB_PASSWORD}

              APP_KEY=${APP_KEY}
              EOF

            - echo ">>> Deploy via SFTP"
            - lftp -c "
                set net:max-retries 2;
                set net:timeout 20;
                set sftp:auto-confirm yes;
                open -u ${SFTP_USER},${SFTP_PASS} sftp://${SFTP_HOST};
                mirror -R \
                  --delete \
                  --parallel=4 \
                  --exclude-glob .git* \
                  --exclude-glob .github \
                  --exclude-glob .bitbucket \
                  --exclude-glob node_modules/ \
                  --exclude-glob tests/ \
                  --exclude-glob storage/logs/*.log \
                  ./ ${SFTP_PATH}
              "

            - echo ">>> Run post-deploy Artisan commands on server"
            - |
              sshpass -p "${SFTP_PASS}" ssh -o StrictHostKeyChecking=no ${SFTP_USER}@${SFTP_HOST} "
                set -e
                cd ${SFTP_PATH}

                if ! grep -q '^APP_KEY=base64' .env; then
                  php artisan key:generate --force
                fi

                php artisan storage:link || true
                php artisan migrate --force || exit 1
                php artisan config:cache
                php artisan route:cache || true
                php artisan view:cache || true

                if [ -d storage ] && [ -d bootstrap/cache ]; then
                  find storage -type d -exec chmod 775 {} \; 2>/dev/null || true
                  find storage -type f -exec chmod 664 {} \; 2>/dev/null || true
                  find bootstrap/cache -type d -exec chmod 775 {} \; 2>/dev/null || true
                  find bootstrap/cache -type f -exec chmod 664 {} \; 2>/dev/null || true
                fi
              "
