티스토리 뷰

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"
        }

      }
    }

  }
}

참고사이트

'DevOps > System&Tools' 카테고리의 다른 글

Use ssh with jenkinsfile as scripted pipeline  (0) 2019.12.11
How to use GitLab API  (0) 2019.11.27
variables in Jenkinsfile  (0) 2019.11.22
Jenkinsfile validator  (0) 2019.11.20
nexus  (0) 2019.11.12
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함