pipeline {
    agent any

    tools {
        maven 'Maven-3.5.0'
        jdk 'OpenJdk-11.0.2'
    }

    environment {
        JAVA_HOME = '/opt/jdks/jdk-11.0.2'
    }

    stages {
        stage('Configuration') {
            steps {
                dir('minager-properties') {
                    git url:'https://gogs.takiguchi.ovh/Minager/minager-properties.git', branch: 'master', credentialsId: 'a6494064-8130-42fd-9d3d-e7734518c79e'
                }
                script {
                    sh 'cp ./minager-properties/application-prod.yml ./src/main/resources/application.yml'
                }
            }
        }

        stage('Angular build') {
            steps {
                script {
                    sh '''
                        echo "Construction de l'application Angular..."

                        cd $WORKSPACE/src/main/ts && npm install && ng build --configuration=production && cd -
                        
                        echo "Application générée avec succès."
                    '''
                }
            }
        }

        stage('SpringBoot build') {
            steps {
                sh 'mvn clean package'
            }
        }

        stage('Publish and restart') {
            steps {
                script {
                    sh 'mv target/minager*.jar target/minager.jar'
                }
                sshPublisher(
                    publishers: [
                        sshPublisherDesc(
                            configName: 'DebianServer [minager]',
                            transfers: [
                                sshTransfer(
                                    cleanRemote: false,
                                    excludes: '',
                                    execCommand: '',
                                    execTimeout: 120000,
                                    flatten: false,
                                    makeEmptyDirs: false,
                                    noDefaultExcludes: false,
                                    patternSeparator: '[, ]+',
                                    remoteDirectory: '',
                                    remoteDirectorySDF: false,
                                    removePrefix: 'target/',
                                    sourceFiles: 'target/minager.jar'
                                ),
                                sshTransfer(
                                    cleanRemote: false,
                                    excludes: '',
                                    execCommand: """
                                        mv /home/minecraft/minager.jar /opt/minecraft/minager/bin/minager.jar
                                        chmod 500 /opt/minecraft/minager/bin/minager.jar
                                        sudo service minager restart
                                    """,
                                    execTimeout: 120000,
                                    flatten: false,
                                    makeEmptyDirs: false,
                                    noDefaultExcludes: false,
                                    patternSeparator: '[, ]+',
                                    remoteDirectory: '',
                                    remoteDirectorySDF: false,
                                    removePrefix: '',
                                    sourceFiles: ''
                                )
                            ],
                            usePromotionTimestamp: false,
                            useWorkspaceInPromotion: false,
                            verbose: false
                        )
                    ]
                )
            }
        }
    }
}