DevOps/System&Tools
Jenkinsfile example using ssh and scp
Jacob_baek
2019. 11. 25. 11:30
gradle로 빌드 가능한 application을 ec2 instance 내 tomcat webapps directory에 복사하는 과정을 Jenkinsfile로 생성한 내용을 간단히 정리해보았다.
환경
- Git repository로 Bitbucket Server 사용
- Jenkins 2.235.1
- EC2 instance 상에 tomcat으로 Application 동작
사전 준비
사전에 credential 두개를 Jenkins 상에서 생성한다.
- jacobbaek : bitbucket에서의 credential ("username and password" type으로 credential 생성)
- ec2-user-credential : ec2 instance에 로그인할 ec2-user의 ("ssh username with private key" type으로 credential 생성)
Jenkinsfile
pipeline {
agent any
stages {
stage('clone the source codes') {
steps {
# credential
git(url: 'https://bitbucket-server.co.kr/scm/test.git', branch: 'master', poll: true, credentialsId: 'jacobbaek')
}
}
stage('build by gradle') {
steps {
sh 'ls -al && ./gradlew build'
}
}
stage('check war file existence') {
steps {
sh '''# check
ls -al
ls -al build'''
}
}
stage('deploy') {
steps {
sshagent (credentials: ['ec2-user-credential']) {
sh "scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -r build/libs/ROOT.war ec2-user@ec2-server-domain://tmp"
sh "ssh -vvv -o StrictHostKeyChecking=no -T ec2-user@ec2-server-domain sudo cp -f /tmp/ROOT.war /usr/local/tomcat/webapps/"
// sh "scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -r build/libs/ROOT.war ec2-user@ec2-server-domain://usr/local/tomcat/webapps/"
//sh "scp -o 'StrictHostKeyChecking no' -r build/libs/ROOT.war ec2-user@ec2-server-domain://usr/local/tomcat/webapps/ROOT/"
// sh "ssh -vvv -o StrictHostKeyChecking=no -T ec2-user@ec2-server-domain"
}
}
}
}
}