pipeline {
    agent any

    environment {
        DOCKER_IMAGE = 'caliverse/admintool-back-dev'
        DOCKER_TAG = '1.0.0'
        DOCKER_TAG_PRE = '1.0.0'
        FILE_NAME = 'admintool_back_dev.tar'
        DOCKER_NAME = 'admintool-back-dev'
        DOCKER_PORT = '23450'
        DOCKERFILE_NAME = 'Dockerfile.dev'
        TMPDIR = '/tmp/jenkins'
        BUILD_DIR = '/home/admintool/admintool-back'
    }

    stages {
        stage('Gradle Build') {
            steps {
                script {
                    sh 'chmod +x gradlew'  //gradle 권한 설정
                    sh './gradlew clean build -x test -Pprofile=dev'     //빌드 test는 하지않는다
                }
            }
        }
        stage('MetaData Checkout'){
            steps{
                checkout([$class: 'SubversionSCM',
                    additionalCredentials: [],
                    excludedCommitMessages: '',
                    excludedRegions: '',
                    excludedRevprop: '',
                    excludedUsers: '',
                    filterChangelog: false,
                    ignoreDirPropChanges: false,
                    includedRegions: '',
                    locations: [
                        [
                            cancelProcessOnExternalsFail: true,
                            credentialsId: 'jenkins-build',
                            depthOption: 'infinity',
                            ignoreExternalsOption: true,
                            local: 'metadata',
                            remote: 'svn://10.20.20.9/trunk/Caliverse/DataAssets/MS2/JSON@HEAD'
                        ]
                    ],
                    quietOperation: true,
                    workspaceUpdater: [$class: 'UpdateUpdater']
                ])
            }
        }
        stage('MeataData Transfer'){
            steps{
                dir(env.TMPDIR){
                    sh "rsync -av --exclude='.svn' ${WORKSPACE}/metadata/* /home/admintool/admintool-back/metadata/"
                    sh 'rm -rf ${WORKSPACE}/metadata'
                }
            }
        }
        stage('Docker Image Build & Deploy') {
            steps {
                script {
                    sh 'docker rmi $DOCKER_IMAGE:$DOCKER_TAG_PRE || true'   //이전 이미지 삭제
                    sh 'docker rmi $DOCKER_IMAGE:$DOCKER_TAG || true'
                    sh 'docker build -f $DOCKERFILE_NAME -t $DOCKER_IMAGE:$DOCKER_TAG .'
                    echo 'Docker Image Create'
                    sh 'docker stop ${DOCKER_NAME} || true'
                    sh 'docker rm ${DOCKER_NAME} || true'
                    sh 'docker run -d \
                        -p $DOCKER_PORT:$DOCKER_PORT \
                        --name $DOCKER_NAME \
                        --restart=always \
                        --log-opt max-size=10m \
                        -e TZ=\${TZ:-Asia/Seoul} \
                        -v /home/admintool/admintool-back/metadata:/metadata:ro,cached \
                        -v /home/admintool/admintool-back/log:/logs \
                        -v /home/admintool/admintool-back/upload:/upload \
                        $DOCKER_IMAGE:$DOCKER_TAG'
                    echo 'Docker Run Complete'
                }
            }
        }
    }
}