<?xml version="1.1" encoding="UTF-8"?>
<project>
  <description>Fzx平台-开发环境自动部署流水线</description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <hudson.model.ParametersDefinitionProperty>
      <parameterDefinitions>
        <hudson.model.StringParameterDefinition>
          <name>BRANCH</name>
          <description>代码分支</description>
          <defaultValue>develop</defaultValue>
        </hudson.model.StringParameterDefinition>
        <hudson.model.StringParameterDefinition>
          <name>SERVICES</name>
          <description>需要构建的服务（逗号分隔）</description>
          <defaultValue>api-gateway</defaultValue>
        </hudson.model.StringParameterDefinition>
      </parameterDefinitions>
    </hudson.model.ParametersDefinitionProperty>
  </properties>
  <scm class="hudson.scm.NullSCM"/>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition">
    <script>pipeline {
    agent any
    parameters {
        string(name: "BRANCH", defaultValue: "develop")
        string(name: "SERVICES", defaultValue: "api-gateway")
    }
    environment {
        REGISTRY = "registry:5000"
        IMAGE_ENV = "aliyun-dev"
        COMPOSE_FILE = "/opt/fzx/infrastructure/compose/podman-compose.aliyun-dev.yml"
        WORKSPACE = "/opt/fzx"
    }
    stages {
        stage("拉取代码") {
            steps {
                dir(WORKSPACE) {
                    git url: "https://codeup.aliyun.com/69c68bacfa2a62bc85952a67/FzxBdsh.git", branch: "\${BRANCH}"
                }
            }
        }
        stage("Maven构建") {
            steps {
                dir(WORKSPACE) {
                    sh "mvn clean package -DskipTests -q"
                }
            }
        }
        stage("构建镜像") {
            steps {
                script {
                    def services = params.SERVICES.split(",")
                    services.each { service ->
                        def imageName = "\${REGISTRY}/\${service}-\${IMAGE_ENV}:latest"
                        dir("\${WORKSPACE}/services/\${service}") {
                            sh "podman build -t \${imageName} -f Containerfile ."
                            sh "podman push \${imageName}"
                        }
                    }
                }
            }
        }
        stage("重启容器") {
            steps {
                dir("/opt/fzx/infrastructure/compose") {
                    sh "podman-compose -f \${COMPOSE_FILE} up -d \${params.SERVICES}"
                }
            }
        }
        stage("验证服务") {
            steps {
                sh "sleep 30"
                sh "podman-compose -f /opt/fzx/infrastructure/compose/podman-compose.aliyun-dev.yml ps"
            }
        }
    }
    post {
        success { echo "部署成功" }
        failure { echo "部署失败" }
    }
}</script>
    <sandbox>true</sandbox>
  </definition>
  <publishers/>
  <buildWrappers/>
</project>