pipeline {
	agent any
	stages {
		stage('#1 Fetch App') {
			steps {
				echo '#1 Fetch App'
				
				sh 'rm -r *'
				
				git credentialsId: 'git-tbs093a', url: 'git@github.com:TBS093A/curriculum_vitae.git'
				
				sh 'ls -la'
			}
		}
		stage('#2 Build App (npm node)') {
			steps {
				echo '#2 Build App (npm node)'
				
				withCredentials([sshUserPrivateKey(credentialsId: 'cv-kamil-zuk', keyFileVariable: 'keyfile')]) {
    					script {
						sh 'ssh -i ${keyfile} -t "cd /home/ubuntu/docker-compose/volumes/jenkins_node_00_npm/ ; rm -r *"'
						sh 'scp -i ${keyfile} -r ./ ubuntu@13.48.201.185:/home/ubuntu/docker-compose/volumes/jenkins_node_00_npm/'
						sh 'ssh -i ${keyfile} -t "cd /home/ubuntu/docker-compose/volumes/jenkins_node_00_npm/ ; docker-compose up -d jenkins_node_00_npm"'
						sh 'scp -i ${keyfile} -r ubuntu@13.48.201.185:/home/ubuntu/docker-compose/volumes/jenkins_node_00_npm/public ./public'
					}
				}
			}
		}
		stage('#3 Deploy App') {
			steps {
				echo '#3 Deploy App'
				
				sshPublisher(
					publishers: [
						sshPublisherDesc(
							configName: 'aws - kamil-zuk-cv.tk - 13.48.201.185', 
							transfers: [
								sshTransfer(
									cleanRemote: false, 
									excludes: '', 
									execCommand: 'ls -la', 
									execTimeout: 120000, 
									flatten: false, 
									makeEmptyDirs: false, 
									noDefaultExcludes: false, 
									patternSeparator: '[, ]+', 
									remoteDirectory: './docker-compose/volumes/https-nginx-static/kamil-zuk-cv.tk', 
									remoteDirectorySDF: false, 
									removePrefix: '', 
									sourceFiles: './public/*'
								)
							], 
							usePromotionTimestamp: false, 
							useWorkspaceInPromotion: false, 
							verbose: false
						)
					]
				)
				
				
				echo 'Deploy Success'
				
			}
		}
	}
}
