<?xml version="1.0" encoding="UTF-8"?>
<project>
  <description>Fzx Platform Deploy Job - Deploy to Aliyun Development Environment</description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <jenkins.model.BuildDiscarderProperty>
      <strategy class="hudson.tasks.LogRotator">
        <daysToKeep>7</daysToKeep>
        <numToKeep>5</numToKeep>
        <artifactDaysToKeep>7</artifactDaysToKeep>
        <artifactNumToKeep>3</artifactNumToKeep>
        <removeLastBuild>false</removeLastBuild>
      </strategy>
    </jenkins.model.BuildDiscarderProperty>
  </properties>
  <scm class="hudson.plugins.git.GitSCM">
    <configVersion>2</configVersion>
    <userRemoteConfigs>
      <hudson.plugins.git.UserRemoteConfig>
        <url>https://codeup.aliyun.com/69c68bacfa2a62bc85952a67/FzxBdsh.git</url>
        <credentialsId>6a269f08-e243-4f6a-843e-76244317e4a0</credentialsId>
      </hudson.plugins.git.UserRemoteConfig>
    </userRemoteConfigs>
    <branches>
      <hudson.plugins.git.BranchSpec>
        <name>*/develop</name>
      </hudson.plugins.git.BranchSpec>
    </branches>
  </scm>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers>
    <hudson.triggers.ReverseBuildTrigger>
      <spec/>
      <upstreamProjects>Fzx-Build</upstreamProjects>
    </hudson.triggers.ReverseBuildTrigger>
  </triggers>
  <concurrentBuild>false</concurrentBuild>
  <builders>
    <hudson.tasks.Shell>
      <command><![CDATA[#!/bin/bash
set -euo pipefail

echo "========================================"
echo "Fzx Platform Deployment - Aliyun Dev"
echo "========================================"

DEPLOY_HOST="8.140.221.49"
DEPLOY_USER="root"
SSH_KEY="/var/jenkins_home/.ssh/FzxBdsh-ECS.pem"
COMPOSE_DIR="/opt/fzx/deploy"
BACKUP_DIR="/opt/fzx/backup"

if [ ! -f "$SSH_KEY" ]; then
    echo "SSH key not found: $SSH_KEY"
    exit 1
fi

echo "Checking SSH connection..."
ssh -i "$SSH_KEY" -o ConnectTimeout=5 -o StrictHostKeyChecking=no "$DEPLOY_USER@$DEPLOY_HOST" "echo OK"

echo "Creating backup directory..."
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=no "$DEPLOY_USER@$DEPLOY_HOST" "mkdir -p $COMPOSE_DIR $BACKUP_DIR"

echo "Backing up current deployment..."
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=no "$DEPLOY_USER@$DEPLOY_HOST" "cp -r $COMPOSE_DIR $BACKUP_DIR/\$(date +%Y%m%d_%H%M%S) 2>/dev/null || true"

echo "Copying compose file..."
scp -i "$SSH_KEY" -o StrictHostKeyChecking=no "infrastructure/compose/podman-compose.aliyun-dev.yml" "$DEPLOY_USER@$DEPLOY_HOST:$COMPOSE_DIR/"

echo "Checking current containers..."
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=no "$DEPLOY_USER@$DEPLOY_HOST" "podman ps --filter 'name=fzx-aliyun' --format '{{.Names}} {{.Status}}'"

echo "Starting deployment..."
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=no "$DEPLOY_USER@$DEPLOY_HOST" "cd $COMPOSE_DIR && podman-compose -f podman-compose.aliyun-dev.yml up -d --remove-orphans"

echo "Waiting for services to start..."
sleep 30

echo "Verifying deployment..."
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=no "$DEPLOY_USER@$DEPLOY_HOST" "podman ps --filter 'name=fzx-aliyun' --format '{{.Names}} {{.Status}}'"

echo "========================================"
echo "Deployment completed successfully!"
echo "========================================"
]]></command>
    </hudson.tasks.Shell>
  </builders>
  <publishers/>
  <buildWrappers/>
</project>