.jenkinsfile

pipeline {
    agent any

    environment {
        SLACK_CHANNEL = '#2_admin-cloud-notification'
    }

    stages {
        stage('Build Start') {
            steps {
                slackSend (channel: SLACK_CHANNEL, color: '#f5e4d5', message: "STARTED: ${env.JOB_NAME} [${env.BUILD_NUMBER}]")
            }
        }
        stage('Git Pull') {
            steps {
                git branch: 'main', credentialsId: 'gitlab-account', url: '<http://gitlab/admin-page/admin-page-renew.git>'
            }
        }
        stage('Gradle Build') {
            steps {
//                sh 'chmod +x gradlew'
//                sh './gradlew clean build'
                sh 'mvn clean install compile package'
            }
        }
        stage('Deploy') {
            steps {
                script {
                    withCredentials([
                            sshUserPrivateKey(credentialsId: 'ssh-nfttown-dev', keyFileVariable: 'identity', usernameVariable: 'userName')
                    ]) {

                        def remote = [:]
                        remote.name = "ssh-nfttown-portal-admin-dev"
                        remote.host = "172.16.8.217"
                        remote.allowAnyHosts = true
                        remote.user = userName
                        remote.identityFile = identity

//                         stage("Deploy - Backup") {
//                             sshCommand remote: remote, command: 'mv nfttown-0.0.1-SNAPSHOT.jar nfttown-0.0.1-SNAPSHOT-back.jar'
//                             // sshCommand remote: remote, command: "n=0; while [[ -e snapshot_backup/nfttown-0.0.1-SNAPSHOT-\\$(date +'%Y-%m-%d')_\\$n.jar ]]; do n=\\$((n+1)); done; mv nfttown-0.0.1-SNAPSHOT.jar snapshot_backup/nfttown-0.0.1-SNAPSHOT-\\$(date +'%Y-%m-%d')_\\$n.jar"
//                         }
                        stage("Deploy - Upload") {
                            sshPut remote: remote, from: "target/adminpage-0.0.1-SNAPSHOT.jar", into: 'deploy'
                        }
                        stage("Deploy - Image Build"){
                            sshCommand remote: remote, command: 'docker tag adminpage:latest adminpage-backup:latest'
                            sshCommand remote: remote, command: 'docker build -t adminpage:latest /home/ubuntu/deploy'
                        }
                        stage("Deploy - Remove Container, Image"){
                            sshCommand remote: remote, command: 'docker rm -f adminpage-con'
                            def images = sshCommand remote: remote, command: 'docker images -f dangling=true -q'
                            println(images == null)
                            if (images == null) {
                                sshCommand remote: remote, command: 'docker rmi $(docker images -f dangling=true -q)'
                            }
                        }
                        stage("Deploy - Container Run"){
                            sshCommand remote: remote, command: 'docker run -d --name adminpage-con -e ENVIRONMENT_VALUE=-Dspring.profiles.active=dev -p 80:80 adminpage:latest'
                        }
                        sleep time:30, unit: 'SECONDS' // 30초 대기
                        stage("Health Check"){
                            for (int i = 0; i < 10; i++){
                                response = sshCommand remote: remote, command: 'curl -X GET -L -s -o - localhost:80/health'
                                if(response != 'UP'){
                                    sshCommand remote: remote, command: 'docker run -d --name adminpage-con -e ENVIRONMENT_VALUE=-Dspring.profiles.active=dev -p 80:80 adminpage-backup:latest'
                                    sshCommand remote: remote, command: 'docker rmi -f adminpage' //에러가 발생한 admin page image를 삭제
                                    sshCommand remote: remote, command: 'docker tag adminpage-backup:latest adminpage:latest' //에러가 발생하지 않은 백업을 admin page image로 변경
                                    error 'backup deploy'
                                    slackSend (channel: SLACK_CHANNEL, color: '#ff7f00', message: "BACKUP DEPLOY : ${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
                                }
                            }
                        }
                    }
                }
            }
        }

    }
    post {
        success {
            slackSend (channel: SLACK_CHANNEL, color: '#008000', message: "SUCCESSFUL: ${env.JOB_NAME} [${env.BUILD_NUMBER}]")
        }
        failure {
            slackSend (channel: SLACK_CHANNEL, color: '#FF0000', message: "FAILED: ${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
        }
    }
}