pipeline {
    agent any

    environment {
        DOCKER_IMAGE = 'caliverse/admintool-back'
        DOCKER_TAG = '1.0.0'
        DOCKER_TAG_PRE = '1.0.0'
        FILE_NAME = 'admintool_back.tar'
        DOCKER_NAME = 'admintool-back'
        DOCKER_PORT = '23450'
    }

    stages {
        stage('Gradle Build') {
            steps {
                script {
                    sh 'chmod +x gradlew'  //gradle 권한 설정
                    sh './gradlew clean build -x test -Pprofile=stage'     //추후
//                     sh './gradlew clean build -x test'     //빌드 test는 하지않는다
                }
            }
        }
        stage('Docker Image Build') {
            steps {
                script {
                    sh 'docker rmi $DOCKER_IMAGE:$DOCKER_TAG_PRE || true'   //이전 이미지 삭제
                    sh 'rm $FILE_NAME || true'   //이전 .tar 파일 삭제
                    sh 'docker build -t $DOCKER_IMAGE:$DOCKER_TAG .'
                    echo 'Docker Image Create'
                    sh 'docker save -o $FILE_NAME $DOCKER_IMAGE:$DOCKER_TAG'
                    echo 'Docker Image > .tar File Create'
                }
            }
        }
        stage('SSH Tunneling') {
            steps {
                script {
                    sh 'sudo lsof -ti @127.30.148.164:2211 | xargs -r sudo kill -9; sudo ssh -f -N -L 127.30.148.164:2211:172.30.148.164:2211 ubuntu@52.32.111.3 -p 2211 -i /home/admintool/USWest2-KeyPair.pem -o StrictHostKeyChecking=no'
                }
            }
        }
        stage('Transfer Docker Image') {
            steps {
                // aws .tar transfer
                sshPublisher(
                    publishers: [
                        sshPublisherDesc(
                            configName: 'stage-backend',
                            transfers: [
                                sshTransfer(
                                    sourceFiles: "${FILE_NAME}",
                                    remoteDirectory: '',
                                    execCommand: """
                                        echo '.tar Transfer Complete'
                                    """,
                                    execTimeout: 120000
                                )
                            ],
                            usePromotionTimestamp: false,
                            verbose: true
                        )
                    ]
                )
            }
        }
        stage('Deploy to Remote'){
            steps{
                // aws command
                sshPublisher(
                    publishers: [
                        sshPublisherDesc(
                            configName: 'stage-backend',
                            transfers: [
                                sshTransfer(
                                    execCommand: """
                                        docker stop ${DOCKER_NAME} || true &&
                                        docker rm ${DOCKER_NAME} || true &&
                                        docker rmi ${DOCKER_IMAGE}:${DOCKER_TAG_PRE} || true &&
                                        docker load -i ${FILE_NAME} &&
                                        docker run -d \
                                            -p ${DOCKER_PORT}:${DOCKER_PORT} \
                                            --name ${DOCKER_NAME} \
                                            --restart=always \
                                            -e TZ=\${TZ:-Asia/Seoul} \
                                            -v ./admintool/log:/logs \
                                            ${DOCKER_IMAGE}:${DOCKER_TAG} &&
                                        rm ${FILE_NAME}
                                    """,
                                    execTimeout: 120000
                                )
                            ],
                            usePromotionTimestamp: false,
                            verbose: true
                        )
                    ]
                )
            }
        }
    }
}