image: node:18

definitions:
  services:
    docker:
      memory: 4096

pipelines:
  branches:
    devops:
      - step:
          name: SonarCloud Analysis
          size: 2x
          services:
            - docker
          caches:
            - node
          script:
            - npm install
            - pipe: sonarsource/sonarcloud-scan:2.0.0
              variables:
                SONAR_TOKEN: $SONAR_TOKEN
                SONAR_PROJECT_KEY: "blitzmarketing-backend"
                SONAR_ORGANIZATION: "dreamztech"
                SONAR_SOURCES: "."

      - step:
          name: Deploy to Server
          script:
            - echo "Starting deployment"

            # SSH setup
            - mkdir -p ~/.ssh
            - chmod 700 ~/.ssh
            - echo -e "Host *\n\tStrictHostKeyChecking no\n" > ~/.ssh/config

            # Deploy using correct SSH PORT (2242)
            - ssh -p 2242 sshuser_fuseAIworkforce-dev@fuseAIworkforce-dev.dreamztesting.com << 'EOF'
                set -e
                cd /var/www/html/fuseAIworkforce-dev/blitzmarketing-backend
                git pull origin devops
                npm install --production
                pm2 restart blitzmarketing-backend || pm2 start npm --name blitzmarketing-backend -- run start
                pm2 save
              EOF

            - echo "Deployment completed successfully"
