pipeline {
    environment {
        REGISTRY_URL = "https://proxy.docker.dataekb.ru/local_cache"
        REGISTRY = "proxy.docker.dataekb.ru/local_cache"
        BOT_IMAGE_NAME = "bot_open_sesam"
        TUNNEL_IMAGE_NAME = "tunnel_open_sesam"
        BOT_IMAGE_TAG = "latest"
        TUNNEL_IMAGE_TAG = "latest"
        IMAGE_TAG = "${env.BUILD_NUMBER}"
    }

    agent { label 'agent_smith'}

   options {
        buildDiscarder(logRotator(numToKeepStr: '10'))
        disableConcurrentBuilds()  // Prevent cleanup conflicts
        timeout(time: 30, unit: 'MINUTES')
    }

    stages {
    
        stage ('build bot image and push') {
            steps {
                    script {
                        docker.withRegistry("${REGISTRY_URL}", 'jenkins_harbor') {
                            def BotImage = docker.build(
                                "${REGISTRY}/${BOT_IMAGE_NAME}:${env.BUILD_NUMBER}",
                                "--label build_number=${IMAGE_TAG} " +
                                "--label git_commit=${env.GIT_COMMIT} " +
                                "."                                
                                )
                            BotImage.push()
                        }
                    }
                }
            }
        stage ('build tunnel image and push') {
            steps {
                dir('tunnel'){
                    script {
                        docker.withRegistry("${REGISTRY_URL}", 'jenkins_harbor') {
                            def TunnelImage = docker.build(
                                "${REGISTRY}/${TUNNEL_IMAGE_NAME}:${env.BUILD_NUMBER}",
                                "--label build_number=${IMAGE_TAG} " +
                                "--label git_commit=${env.GIT_COMMIT} " +
                                "."
                                )
                            TunnelImage.push()
                        }
                    }
                }
            }
        }        
        
        stage ('clear after build and push') {
            steps {
                script {
                            sh "docker image prune --filter label=stage=builder"
                }
            }
        }

        stage("Deploy") {
            steps {
                script {
                    sh '''
                        export BOT_IMAGE_TAG=${IMAGE_TAG}
                        export TUNNEL_IMAGE_TAG=${IMAGE_TAG}
                        
                        # Pull new images explicitly
                        docker-compose pull bot_open_sesam tunnel_open_sesam
                        
                        # Deploy with specific project name
                        docker-compose up -d --remove-orphans
                                                
                    '''
                }
            }
        }
    }
    post {
        success {
            echo "Оба образа собраны и задеплоены успешно"
        }
        failure {
            echo "Ошибка сборки!"
        }
    }
    
}



// Old_version
// pipeline {
//   agent { label 'agent_smith' }
// 
//   stages {
//     stage('Stop and Remove Existing Container') {
//       steps {
//         sh '''
//           docker stop open_sesam || true
//           docker rm open_sesam || true
//           docker-compose down || true
//           '''
//       }
//     }
// 
//     stage('Build and Run Container') {
//       steps {
//         sh '''
//           docker-compose up --build -d
//           '''
//       }
//     }
//   }
// }
