pipeline {
    agent any
    
    environment {
        DOCKER_IMAGE = "caliverse/admintool-front-qa"
        DOCKER_TAG = "1.0.0"
        DOCKER_TAG_PRE = "1.0.0"
        FILE_NAME = 'admintool_front-qa.tar'
        DOCKER_NAME = 'admintool-front-qa'
        DOCKER_PORT = '8080'
        DOCKERFILE_NAME = 'Dockerfile.qa'
    }

    stages {
        stage('Docker Build') {
            steps {
                sh 'docker rmi $DOCKER_IMAGE:$DOCKER_TAG_PRE || true'   //이전 이미지 삭제
                sh 'docker build -f $DOCKERFILE_NAME -t $DOCKER_IMAGE:$DOCKER_TAG .'   //Dockerfile 기준으로 이미지 생성
                echo 'Docker Image Create'
                sh 'docker save -o $FILE_NAME $DOCKER_IMAGE:$DOCKER_TAG'   //이미지 .tar 로 생성
                echo 'Docker Image > .tar File Create'
            }
        }
        stage('Transfer Docker Image') {
            steps {
                // aws .tar transfer
                sshPublisher(
                    publishers: [
                        sshPublisherDesc(
                            configName: 'qa-frontend',
                            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: 'qa-frontend',
                            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 \
                                            -v ./admintool/log:/var/log/nginx \
                                            -e TZ=\${TZ:-Asia/Seoul} \
                                            ${DOCKER_IMAGE}:${DOCKER_TAG} &&
                                        rm ${FILE_NAME}
                                        echo 'Docker Container Create Complete'
                                    """,
                                    execTimeout: 120000
                                )
                            ],
                            usePromotionTimestamp: false,
                            verbose: true
                        )
                    ]
                )
            }
        }
    }
}