#!/bin/bash
# 重新创建Fzx-Deploy任务（修复XML格式问题）

JOBS_DIR="/var/lib/containers/storage/volumes/fzx-aliyun-jenkins-data/_data/jobs"

echo "Creating Fzx-Deploy job..."
mkdir -p "$JOBS_DIR/Fzx-Deploy"

cat > "$JOBS_DIR/Fzx-Deploy/config.xml" << 'XMLEOF'
<?xml version='1.1' encoding='UTF-8'?>
<project>
  <description>Fzx Platform Deploy Job</description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <jenkins.model.BuildDiscarderProperty>
      <daysToKeep>7</daysToKeep>
      <numToKeep>5</numToKeep>
    </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>aliyun-codeup-https</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/>
  <concurrentBuild>false</concurrentBuild>
  <builders>
    <hudson.tasks.Shell>
      <command>set -e

echo "========================================"
echo "Deploying to Aliyun Dev Environment"
echo "========================================"

DEPLOY_HOST="8.140.221.49"
DEPLOY_USER="root"
SSH_KEY="/var/lib/jenkins/.ssh/id_rsa"

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

echo "Deploying services..."
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=no "$DEPLOY_USER@$DEPLOY_HOST" \
    "cd /opt/Fzxpodman/infrastructure/compose && podman-compose -f podman-compose.aliyun-dev.yml restart nginx nacos"

echo "Deployment completed"
</command>
    </hudson.tasks.Shell>
  </builders>
  <publishers/>
  <buildWrappers/>
</project>
XMLEOF

chown -R jenkins:jenkins "$JOBS_DIR/Fzx-Deploy"
echo "Fzx-Deploy created"
