pipeline {
    agent any
    environment {
        DOCKER_RELEASE_CONTAINER = 'acamate-front-build-release'
		DOCKER_DEBUG_CONTAINER = 'acamate-front-build-debug'
		APP_VOLUME = '/src'
    }
    stages {
        stage('Clear Repository') {
            steps {
                script {
                        sh """
                            echo 'Clearing Front directory'
                            docker run --rm -v /volume1/AcaMate/PROJECT/Application/Front:/front alpine \
                            sh -c "find /front -mindepth 1 -maxdepth 1 \\
                                ! -name 'privacy' \\
                                ! -name 'publish' \\
                                -exec rm -rf {} +"
                            echo 'Clean complete'
                        """
                }
            }
        }
        stage('Clone Repository') {
            steps {
				git url: 'https://git.ipstein.myds.me/AcaMate/AcaMate_Web.git', branch: env.GIT_BRANCH
            }
        }
        stage('Deploy') {
            steps {
                script {
					sh "docker create --name temp-container -v /volume1/AcaMate/PROJECT/Application/Front:/src mcr.microsoft.com/dotnet/sdk:8.0"
					sh "docker cp ${WORKSPACE}/. temp-container:/src"
					sh "docker rm temp-container"
					if (env.GIT_BRANCH == 'main') {
                        // main 브랜치용 작업
						sh "docker start ${DOCKER_RELEASE_CONTAINER}"
                    } else if (env.GIT_BRANCH == 'debug') {
                        // debug 브랜치용 작업
						sh "docker start ${DOCKER_DEBUG_CONTAINER}"
                    }
                }
            }
        }
    }

    post {
        always {
            script {
                def containerId = sh(script: "docker ps -qf 'name=${env.GIT_BRANCH == 'main' ? DOCKER_RELEASE_CONTAINER : DOCKER_DEBUG_CONTAINER}'", returnStdout: true).trim()
                if (containerId) {
                    sh "docker logs ${containerId}"
                } else {
                    echo "Docker container not found"
                }
            }
        }
        failure {
            echo "Build failed. Check the console output for details."
        }
    }
}