pipeline {
	agent any
		
	tools {
		maven 'Maven-3.5.0'
		jdk 'OpenJdk-1.8.0_181'
	}

	environment {
		JAVA_HOME = '/usr/lib/jvm/java-8-openjdk-amd64'
	}

	stages {
		stage('Configuration') {
			steps {
				script {
					env.MINAGER_VERSION = sh(script: "grep '<version>' pom.xml | head -n 1 | cut -d '>' -f 2 | cut -d '<' -f 1",
							returnStdout: true).toString().trim();
				}
			}
		}

		stage('Angular build') {
			steps {
				dir('src/main/ts') {
					sh '''
						npm install
						ng build --configuration=production
					'''
				}
			}
		}

		stage('SpringBoot build') {
			steps {
				sh 'mvn clean package -DskipTests=true'
			}
		}

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