image: php:8.3

definitions:
  services:
    docker:
      memory: 2048
  steps:
    - step: &test
        name: Run Tests
        caches:
          - composer
        script:
          - apt-get update
          - apt-get install -y git zip unzip libzip-dev libicu-dev
          - docker-php-ext-install zip intl
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer install
          - cp .env.example .env
          - php artisan key:generate
          - php artisan test
        artifacts:
          - vendor/**
          - .env

    - step: &build
        name: Build Application
        caches:
          - composer
        script:
          - apt-get update
          - apt-get install -y git zip unzip libzip-dev libicu-dev nodejs npm
          - docker-php-ext-install zip intl
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer install --no-dev --optimize-autoloader
          - npm install
          - npm run build
          - mkdir -p storage/framework/cache
          - mkdir -p storage/framework/sessions
          - mkdir -p storage/framework/views
          - chmod -R 775 storage
          - export CACHE_STORE=file
          - export SESSION_DRIVER=file
          - php artisan filament:install --panels
          - php artisan filament:assets
          - php artisan view:clear
          - php artisan cache:clear
          - php artisan config:clear
          - php artisan route:clear
          - php artisan config:cache
          - php artisan route:cache
          - php artisan view:cache
        artifacts:
          - public/**
          - vendor/**
          - .env
          - bootstrap/cache/**
          - storage/**

    - step: &deploy-staging
        name: Deploy to Staging
        deployment: staging
        script:
          - apt-get update
          - apt-get install -y lftp
          - lftp -c "
              set ssl:verify-certificate no;
              open -u $FTP_USER,$FTP_PASSWORD ftp://$FTP_SERVER;
              mirror --reverse --verbose --delete --exclude-glob .git* --exclude-glob .env* --exclude-glob storage/* --exclude-glob tests/* --exclude-glob node_modules/* --exclude-glob vendor/* --exclude-glob .DS_Store ./ /;
              quit;
            "

    - step: &deploy-production
        name: Deploy to Production
        deployment: production
        script:
          - apt-get update
          - apt-get install -y lftp
          - lftp -c "
              set ssl:verify-certificate no;
              open -u $FTP_USER,$FTP_PASSWORD ftp://$FTP_SERVER;
              mirror --reverse --verbose --delete --exclude-glob .git* --exclude-glob .env* --exclude-glob storage/* --exclude-glob tests/* --exclude-glob node_modules/* --exclude-glob vendor/* --exclude-glob .DS_Store ./ /;
              quit;
            "

pipelines:
  branches:
    develop:
      - step: *test
      - step: *build
      - step: *deploy-staging
    main:
      - step: *test
      - step: *build
      - step: *deploy-production
  pull-requests:
    '**':
      - step: *test