pipeline {
  agent any

  environment {
    FASTDL_CONTAINER = "fastdl"
    FASTDL_PATH = "/usr/share/nginx/html"
    TARGET_DIR = "/opt/game-servers/csdm"
    REPO_URL = "https://gitea.go-yasozdal.ru/yvnger/csdm"
    BRANCH = "main"
  }

  stages {
    stage("Prepare Deploy Directory") {
      steps {
        sh '''
          rm -rf $TARGET_DIR
          git clone --branch $BRANCH $REPO_URL $TARGET_DIR
        '''
      }
    }

    stage("Build Docker Image") {
      steps {
        dir("$TARGET_DIR") {
          sh 'docker build -t csserver:csdm .'
        }
      }
    }

    stage("Deploy with Compose") {
      steps {
        dir("$TARGET_DIR") {
          sh 'docker-compose down || true'
          sh 'docker-compose up -d'
        }
      }
    }
  }

  post {
    failure {
      echo 'Build failed. Check logs.'
    }
    success {
      echo 'Server deployed successfully.'
    }
  }
}