pipeline {
    agent any

    environment {
        META_FOLDER = 'metadata/**'
        REMOTE_META_FOLDER = 'admintool'
        DOCKER_NAME = 'admintool-back-stage'
    }

    stages {
        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/branches/Product/DataAssets/MS2/JSON@HEAD'
                        ]
                    ],
                    quietOperation: true,
                    workspaceUpdater: [$class: 'UpdateUpdater']
                ])
            }
        }
        stage('SSH Tunneling Start') {
            steps {
                script {
                    sh 'rm -rf ${WORKSPACE}/metadata/.svn'
                    sh 'rm -rf ${WORKSPACE}/metadata/QuestScript'
                    sh '/home/admintool/ssh-tunneling-stage.sh start'
                }
            }
        }
        stage('Transfer MeataData'){
            steps{
                sshPublisher(
                    publishers: [
                        sshPublisherDesc(
                            configName: 'stage-backend',
                            transfers: [
                                sshTransfer(
                                    sourceFiles: "${META_FOLDER}",
                                    remoteDirectory: "${REMOTE_META_FOLDER}",
                                    execCommand: """
                                        echo 'metadata Transfer Complete'
                                    """,
                                    execTimeout: 120000
                                )
                            ],
                            usePromotionTimestamp: false,
                            verbose: true
                        )
                    ]
                )
            }
        }
        stage('Docker Container Restart'){
            steps{
                // aws command
                sshPublisher(
                    publishers: [
                        sshPublisherDesc(
                            configName: 'stage-backend',
                            transfers: [
                                sshTransfer(
                                    execCommand: """
                                        docker restart ${DOCKER_NAME}
                                    """,
                                    execTimeout: 120000
                                )
                            ],
                            usePromotionTimestamp: false,
                            verbose: true
                        )
                    ]
                )
            }
        }
        stage('SSH Tunneling Stop') {
            steps {
                script {
                    sh '/home/admintool/ssh-tunneling-stage.sh stop; /home/admintool/ssh-tunneling-stage.sh status'
                }
            }
        }
    }
}