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

    stages {
        stage('Docker Build & Deploy') {
            steps {
                sh 'docker stop $DOCKER_NAME || true'
                sh 'docker rm $DOCKER_NAME || true'
                sh 'docker rmi $DOCKER_IMAGE:$DOCKER_TAG_PRE || true'   //이전 이미지 삭제
                sh 'docker rmi $DOCKER_IMAGE:$DOCKER_TAG || true'
                echo 'Docker Image Create'
                sh 'docker build -f $DOCKERFILE_NAME -t $DOCKER_IMAGE:$DOCKER_TAG .'   //Dockerfile 기준으로 이미지 생성
                sh 'docker run -d \
                    -p $DOCKER_PORT:$DOCKER_PORT\
                    --name $DOCKER_NAME \
                    --restart=always \
                    -v /home/admintool/admintool-front/log:/var/log/nginx \
                    -e TZ=\${TZ:-Asia/Seoul} \
                    $DOCKER_IMAGE:$DOCKER_TAG'
                echo 'Docker Container Create Complete'
            }
        }
    }
}