1. 사전 준비

1) Jenkins 설치 : http://itraveler.tistory.com/13

2) rsync 설치 : yum install rsync

3) ssh 암호없이 Target 서버 접속하도록 설정 : http://itraveler.tistory.com/14

4) git 서버 / 클라이언트 설치 및 설정

(1) 원하는 위치에 배포할 소스 파일을 미리 checkout 해둔다.

(2) git clone [your_id]@[your_hostname]:[group_name]/[repository_name].git


2. shell script

1) 공통 사항

(1) 배포 대상 서버 리스트는 별도의 파일에 저장한다.

(2) 배포 대상 서버들을 차례로 ssh로 접속하면서 소스를 rsync로 배포한다.

(3) git pull 로 전체 소스를 가져온다. (git은 개별 파일만 가져오지 못하는 구조)

(4) cache, log 등 특정 디렉토리 및 환경별로 소스가 다른 것들은 배포에서 제외한다.

(5) 배포 대상 그룹명을 파일명으로 일치시키고 젠킨스를 통해 shell script의 argument로 받도록 한다.

(6) 배포를 위해 사용하는 shell script 는 별도 repository 에 저장 (ex : deploy)

2) 소스 전체 배포 하는 경우

(1) 전체 배포의 경우, 비상 상황에 현재 체크 아웃되어 있는 소스를 그대로 내보낸다고 가정하면 git pull 은 하지 않아야 한다.


[[ 전체 소스 배포용 스크립트 : myservice_deploy_all.sh ]

#!/bin/bash

svc_name='myservice'

sh_base_dir='/home/[배포용계정]/src/deploy/'

src_base_dir="/home/[배포용계정]/src/$svc_name/"

target_base_dir="/home/[배포용계정]/$svc_name/"

hostfile="$sh_base_dir$1.txt" # sh deploy.sh myservice_hosts


while read curhost;

do

  echo "[$curhost]"

  rsync -avrz --exclude=.git --exclude 'application/cache/*' --exclude 'application/logs/*' $src_base_dir [배포용계정]@$curhost:$target_base_dir

  echo "============================================================================"

done < $hostfile



[[ 배포 대상 서버 리스트 파일 : myservice_host.txt ]

 target1.yourdomain.com

 target2.yourdomain.com


3) 개별 파일 배포 하는 경우

(1) 젠킨스에서 입력한 파일 목록에 해당하는 파일만 배포한다.

(2) 디렉토리를 통채로 배포하는 경우 디렉토리 path 가 '/' 로 끝나지 않으면 taget directory 밑으로 복사되므로 '/'를 붙여주도록 한다.


[[ 지정 파일 배포용 스크립트 : myservice_deploy_file.sh ]

#!/bin/bash

svc_name='myservice'

sh_base_dir='/home/[배포용계정]/src/deploy/'

src_base_dir="/home/[배포용계정]/src/$svc_name/"

target_base_dir="/home/[배포용계정]/$svc_name/"

hostfile="$sh_base_dir$1.txt" # sh deploy.sh sampleapi_hosts


cd $src_base_dir

git pull


while read curhost;

do

  echo "[$curhost]"


  for curfile in $srcfiles

  do

    let tmplen=${#curfile}

    let tmplen=tmplen-1;

    lastchar=${curfile:$tmplen:1}


    # curfile is a directory but that's not ended with '/'

    if [ -d "/home/[배포용계정]/src/$svc_name/$curfile" -a $lastchar != '/' ]; then

      curfile="$curfile/"

    fi


    echo ">>> $curfile"

    tmpsrc="$src_base_dir$curfile"

    tmptarget="$target_base_dir$curfile"


    rsync -avrz --exclude=.git --exclude 'application/cache/*' --exclude 'application/logs/*' $tmpsrc [배포용계정]@$curhost:$tmptarget

  done


  echo "============================================================================"

done < $hostfile



3. jenkins 설정

1) 새로운 Item > Build a free-style software project 클릭하여 적절한 이름으로 item 생성

2) "이 빌드는 매개 변수가 있습니다" 선택

(1) srcfiles 라는 이름으로 text parameter 생성

(2) 이 곳에서 입력한 srcfile 라는 이름은 shell script 에서 $srcfiles 라는 변수에 할당됨      

3) build > Execute Shell 에 실행할 shell script 설정 입력


 sh /home/[배포용계정]/src/deploy/myservice_deploy_file.sh myservice_hosts


4) 생성 후 dash board 에서 해당 item 항목 Name 옆에 나오는 메뉴 버튼을 눌러 "Build with Parameters" 실행

     

5) srcfiles 입력란에 원하는 파일 목록을 new line 으로 분리하여 "빌드하기" 버튼을 클릭하면 배포 완료!

6) 디버깅은 "Build History" > "콘솔 출력" 확인하며 진행





Posted by 얼랄라