설정

Pipeline

pipeline {
    agent any

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

    stages {
        // [Imsi]
        // stage('Build Start') {
        //     steps {
        //         slackSend (channel: SLACK_CHANNEL, color: '#f5e4d5', message: "STARTED: ${env.JOB_NAME} [${env.BUILD_NUMBER}]")
        //     }
        // }
        
        stage('Deploy') {
            steps {
                script {
                    withCredentials([
                            sshUserPrivateKey(credentialsId: 'ssh-nft-mobile-dev', keyFileVariable: 'identity', usernameVariable: 'userName')
                    ]) {

                        def remote = [:]
                        remote.name = "ssh-nft-museum-web-dev"
                        remote.host = "172.16.4.113"
                        remote.allowAnyHosts = true
                        remote.user = userName
                        remote.identityFile = identity

                        stage('Git Pull') {
                            sshCommand remote: remote, command: 'cd deploy/museum-react-fe/ && sudo git pull'
                        }
                        stage("Deploy - Image Build"){
                            sshCommand remote: remote, command: 'docker tag museum-react-fe:latest museum-react-fe-backup:latest'
                            sshCommand remote: remote, command: 'docker build -t museum-react-fe:latest /home/ubuntu/deploy/museum-react-fe/'
                        }
                        stage("Deploy - Remove Container, Image") {
                            def containerCommand = 'docker rm -f museum-react-fe-con'

                            try {
                                sshCommand remote: remote, command: containerCommand
                                println("Container removed successfully.")
                            } catch (Exception e) {
                                println("Error occurred while removing the container: ${e.getMessage()}")
                                println("Proceeding to the next step.")
                            }

                            def imagesCommand = 'docker images -f dangling=true -q'
                            def images = sshCommand remote: remote, command: imagesCommand

                            try {
                                sshCommand remote: remote, command: 'docker rmi $(docker images -f dangling=true -q)'
                                println("Dangling images removed successfully.")
                            } catch (Exception e) {
                                println("Error occurred while removing dangling images: ${e.getMessage()}")
                            }
                        }
                        stage("Deploy - Container Run") {
                            script {
                                def checkContainerCommand = 'docker ps -a --format "{{.Names}}" | grep -w museum-react-fe-con || true'
                                def containerExists = false

                                try {
                                    def checkResult = sshCommand remote: remote, command: checkContainerCommand, returnStdout: true
                                    if (checkResult.trim().equals("museum-react-fe-con")) {
                                        containerExists = true
                                    }
                                } catch (Exception e) {
                                    println("Error occurred while checking the container: ${e.getMessage()}")
                                }

                                if (containerExists) {
                                    try {
                                        sshCommand remote: remote, command: 'docker rm -f museum-react-fe-con'
                                        println("Container 'museum-react-fe-con' removed successfully.")
                                    } catch (Exception e) {
                                        println("Error occurred while removing the container: ${e.getMessage()}")
                                    }
                                } else {
                                    println("Container 'museum-react-fe-con' does not exist.")
                                }

                                sshCommand remote: remote, command: 'docker run -d --name museum-react-fe-con -p 80:80 museum-react-fe:latest'
                            }
                        }

                        sleep time:30, unit: 'SECONDS' // 30초 대기
                        stage("Health Check"){
                            sshCommand remote: remote, command: 'docker system prune -a --force'
                            for (int i = 0; i < 10; i++){
                                response = sshCommand remote: remote, command: 'curl -s -o /dev/null -w "%{http_code}" localhost:80'
                                if(response != "200"){
                                    sshCommand remote: remote, command: 'docker run -d --name museum-react-fe-con -p 80:80 museum-react-fe:latest'
                                    sshCommand remote: remote, command: 'docker rmi -f museum-react-fe' //에러가 발생한 admin page image를 삭제
                                    sshCommand remote: remote, command: 'docker tag museum-react-fe-backup:latest museum-react-fe:latest' //에러가 발생하지 않은 백업을 admin page image로 변경
                                    error 'backup deploy'
                                    // [Imsi] slackSend (channel: SLACK_CHANNEL, color: '#ff7f00', message: "BACKUP DEPLOY : ${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
                                }
                            }
                        }
                    }
                }
            }
        }

    }
    // [Imsi] 
    // 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})")
    //     }
    // }
}