[CowAPI] 33. Jenkins Pipeline

준돌·2022년 7월 14일
1

오늘의 Cow

목록 보기
38/45

1. 문제

  • freestyle로 레포지토리를 생성하고 script로 clone 하는 부분까지는 성공했다.
  • 하지만, 단계별로 실행하고 확인하기 어려웠고 매번 clone을 한다면 .ignore된 파일들을 추가하기 어렵다는 문제가 발생했다.

2. 파이프 라인 짜기

  • JenkinsFile을 생성했고 다음과 같은 파이프라인을 구성했다.
node {
	# Jenkins의 WorkDirectory에 git Clone (JenkinsFile이 있는 git)
    stage('Clone') {
        echo '>>> Git repository Clone'
        git branch: 'main', credentialsId: 'qawesdxc@naver.com' , url: 'https://github.com/Oh-JunYoung/private_CowAPI.git'
    }
	
    # 나의 WorkDirectory로 이동 (ex. /home/ec2-user)
    stage('Move to Path') {
        echo '>>> Move to Deploy Path'
        sh 'sudo ./jenkins/MovePath.sh'
    }

    # Git pull
    stage('Pull') {
        echo '>>> Git repository Clone'
        sh 'sudo ./jenkins/GitPull.sh'
    }
	
    # Build
    stage('Build') {
        echo '>>> Gradle project Build'
        sh 'sudo ./jenkins/Build.sh'
    }
	
    # Test
    stage('Test') {
        echo '>>> Gradle project Test'
        sh 'sudo ./jenkins/Test.sh'
    }
	
    # 기존의 실행 중이던 server kill
    stage('Kill') {
        echo '>>> Gradle project Deploy'
        sh 'sudo ./jenkins/KillProcess.sh'
    }
	
    # 서버 재실행
    stage('Deploy') {
        echo '>>> Gradle project Deploy'
        sh 'sudo ./jenkins/Deploy.sh'
    }
}

3. 이슈

  • permission deined
    • 매번 비밀번호를 입력하지 않고 SSH로 git clone을 하고 pull, push 한다.
      1. /home/ec2-user/.ssh에 rsa key 생성 (ex. id_rsa, id_rsa.pub)
      2. Github -> Setting -> SSH 등록
      3. vi /home/ec2-user/.ssh/config

      Host github.com
        AddKeysToAgent yes
        User git
        IdentityFile ~/.ssh/id_rsa
      1. 키 등록

        eval "$(ssh-agent -s)" # agent 활성화
        
        ssh-add -K ~/.ssh/id_rsa # 키 등록
        
        ssh -T git@github.com # 테스트
      2. Jenkins 권한

      sudo visudo # jenkins ALL=(ALL) NOPASSWD: ALL
      
      sudo usermod -a -G ec2-user jenkins
      sudo usermod -aG jenkins $USER
      1. root에 key 추가
      cp /home/ec2-user/.ssh/id_rsa* /root/.ssh
  • 위 과정을 거치고 Jenkins가 git을 pull, push 할 수 있게 되었다.

4. 결과

profile
눈 내리는 겨울이 좋아!

0개의 댓글