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: 'PRIV_KEY')]) {
    					script {

						echo "##################################"
						echo "### transfer files on npm node ###"
						echo "##################################"

						sh '''
							scp -v -o StrictHostKeyChecking=no -i $PRIV_KEY -r ./* ubuntu@13.48.201.185:/home/ubuntu/docker-compose/volumes/jenkins_node_00_npm/
						'''

						echo '################################'
						echo '### run building on npm node ###'
						echo '################################'

						sh '''
							ssh -v -o StrictHostKeyChecking=no -i $PRIV_KEY -tt ubuntu@13.48.201.185 "cd /home/ubuntu/docker-compose/volumes/jenkins_node_00_npm/ ; docker-compose up jenkins_node_00_npm"
						'''

						echo '########################################################'
						echo '### transfer files from npm node to jenkins pipeline ###'
						echo '########################################################'

						sh '''
							scp -v -o StrictHostKeyChecking=no -i $PRIV_KEY -r ubuntu@13.48.201.185:/home/ubuntu/docker-compose/volumes/jenkins_node_00_npm/public ./
						'''

						// echo '############################################'
						// echo '### remove all build files from npm node ###'
						// echo '############################################'

						// sh '''
						//	ssh -v -o StrictHostKeyChecking=no -i $PRIV_KEY -tt ubuntu@13.48.201.185 "cd /home/ubuntu/docker-compose/volumes/jenkins_node_00_npm/ ; rm -r *"
						// '''
					}
				}
			}
		}
		stage('#3 Deploy App') {
			steps {
				echo '#3 Deploy App'

				withCredentials([sshUserPrivateKey(credentialsId: 'cv-kamil-zuk', keyFileVariable: 'PRIV_KEY')]) {
                                        script {
				
						echo '######################################################################'
                                		echo '### transfer files from jenkins pipeline to remote nginx as static ###'
                                		echo '######################################################################'
						
						sh '''
							ssh -v -o StrictHostKeyChecking=no -i $PRIV_KEY -tt ubuntu@13.48.201.185 "sudo chown ubuntu:ubuntu -R /home/ubuntu/docker-compose/volumes/https-nginx-static/kamil-zuk-cv.tk ; "
						'''
						
                                		sh '''
                                			scp -v -o StrictHostKeyChecking=no -i $PRIV_KEY -r ./public/* ubuntu@13.48.201.185:/home/ubuntu/docker-compose/volumes/https-nginx-static/kamil-zuk-cv.tk
                                		'''				
						
						sh '''
                                                        ssh -v -o StrictHostKeyChecking=no -i $PRIV_KEY -tt ubuntu@13.48.201.185 "sudo chown root:root -R /home/ubuntu/docker-compose/volumes/https-nginx-sta>
                                                '''
					}
				}

				echo 'Deploy Success'
				
			}
		}
	}
}
