pipeline {
    agent none
    stages {
        stage('Build Image With localhost') {
            agent { label 'LocalHost' } // 在标签为'linux-build'的节点上执行
            steps {
                script {
                    try {
                        echo 'Start Build'
                        sh "docker ps -a --format '{{.ID}} {{.Names}}' | awk '{if (\$2 !~ /^1Panel-jenkins/) print \$1}' | xargs -r docker stop"
                        sh 'docker compose -f ./deploy/docker-compose.yml up -d --build'
                        sh 'docker start \$(docker ps -a -q)'
                        sh "docker restart \$(docker ps -a | grep kami_ | awk '{print \$1}')"
                        echo 'Build success'
                    } catch (err) {
                        echo err.getMessage()
                    }
                }
            }
            post { // 添加post指令处理阶段完成后的情况
                failure {
                    echo 'Build failed, but continuing to the next stage...'
                }
            }
        }
    }
}