image: atlassian/default-image:4

pipelines:
    branches:
        master:
            - step:
                  name: Deploy Symfony to shared
                  deployment: production
                  script:
                      - apt-get update && apt-get install -y rsync openssh-client

                      - mkdir -p ~/.ssh
                      - chmod 700 ~/.ssh
                      - ssh-keyscan -H "$DEPLOY_HOST" >> ~/.ssh/known_hosts

                      - |
                          rsync -az --delete --omit-dir-times --no-perms --no-owner --no-group --force \
                            --exclude=".env" \
                            --exclude=".env.local" \
                            --exclude=".env.local.php" \
                            --exclude="var/" \
                            --exclude="vendor/" \
                            --exclude="node_modules/" \
                            --exclude=".git/" \
                            --exclude=".idea/" \
                            --exclude=".htaccess" \
                            --exclude="public/.htaccess" \
                            --exclude="config/jwt/" \
                            ./ "$DEPLOY_USER@$DEPLOY_HOST:$DEPLOY_BASE/"

                      - |
                          ssh "$DEPLOY_USER@$DEPLOY_HOST" "set -e;
                            cd '$DEPLOY_BASE';

                            export APP_ENV=prod;
                            export APP_DEBUG=0;
                            export SYMFONY_DEPRECATIONS_HELPER=disabled;

                            echo '=== PHP ===';
                            $PHP_BIN -v;

                            echo '=== var/ perms ===';
                            mkdir -p var;
                            chmod -R 775 var || true;

                            echo '=== composer ===';
                            $COMPOSER_BIN install --no-dev --prefer-dist --no-interaction --optimize-autoloader --no-scripts;

                            echo '=== assets ===';
                            $PHP_BIN bin/console assets:install public --env=\${APP_ENV:-prod} --no-interaction;

                            echo '=== dotenv dump (optional) ===';
                            if [ -f .env.local ]; then
                              $PHP_BIN bin/console dotenv:dump --env=${APP_ENV:-prod} || true;
                            fi

                            echo '=== migrations ===';
                            $PHP_BIN bin/console doctrine:migrations:migrate --no-interaction --allow-no-migration --env=${APP_ENV:-prod};

                            echo '=== cache ===';
                            $PHP_BIN bin/console cache:clear -vvv --no-interaction --env=${APP_ENV:-prod};
                            $PHP_BIN bin/console cache:warmup -vvv --no-interaction --env=${APP_ENV:-prod};

                            echo '✅ Deploy OK';
                          "
