git 저장소에 변화가 생기면, 서버에 자동으로 업데이트하는 스크립트.


소스가 변경될 때 마다, 라이브서버에 소스를 업데이트 하는 일은 참 귀찮은 일이다.
그래서 보통 cron을 이용해 서버에 소스 자동 업데이트를 하곤 한다.
이번에도 cron을 이용해 자동 업데이트를 구축하려다가 한가지 문제에 부딪혔다.
crontab에서 스크립트를 돌릴경우 상대경로를 쓰지 못한다는 거다.
그래서 비슷한 걸 찾아 헤매다가 shell-jobs를 발견했고,
이를 이용해 git 저장소가 갱신될 때마다 서버를 자동 업데이트하도록 구축했다.


작동 순서

  1. shell-jobs 데몬 실행.
  2. shell-jobs에서 일정 시간마다 ~/update.sh를 호출.
  3. ~/update.sh에서 원격 저장소를 업데이트하고 변경사항이 있다면 update_platform.sh를 호출.
  4. update_platform.sh에서 소스를 업데이트하고 서버를 재구동.

shell-jobs(https://github.com/azer/shell-jobs)

설치

npm install -g shell-jobs

설정파일 작성

update.jobs
~/update.sh > ~/cron.log # => 10 minutes

shell-jobs 데몬으로 구동

shell-jobs update.jobs -d

리눅스 시동시에 자동으로 구동되도록 하려면, /etc/rc.local파일에도 위 코드를 추가한다.

update.sh

원격 저장소를 업데이트하고 git diff를 통해 로컬과 다른점을 검사한다.
grep -v 뒤에는 로컬 변경을 무시할 파일명을 넣는다.
만약 원격 저장소와 다른 점이 있다면 update_platform.sh를 실행한다.
#!/bin/sh
cd /home/project
git remote update
diff=$(git diff remotes/origin/master master | grep -v <무시할 변경사항>)
if ["$diff" == ""]
then
echo "no diffs"
else
echo "have diffs"
~/update_platform.sh
fi


update_platform.sh

소스를 최신으로 업데이트하고, 서버를 재구동하는 스크립트.
sed를 이용해 debug플래그와 project.wsgi파일을 변경하기 때문에,
git reset --hard로 로컬 변경사항을 무시한다.
#!/bin/sh
cd /home/project
git checkout master
git reset --hard master
git pull
pip2 install -r requirements.txt
alembic revision --autogenerate -m "Alembic initilized boilerplate tables."
alembic upgrade head
sed -i "s/^DEBUG = .*/DEBUG = False/" ./application/config/debug_flag.py
sed -i "s/^sys.path.insert.*/sys.path.insert\(0, '\/home\/project'\)/" ./project.wsgi
chown me -R .
~/restart_server.sh


resteart_server.sh

간혹 서버를 직접 재구동하기도 하니, 서버 재구동용 스크립트는 따로 작성한다.
#!/bin/sh
service apache2 restart



by


Tags : , , , , , , , ,

  • 재미있게 읽으셨나요?
    광고를 클릭해주시면,
    블로그 운영에 큰 도움이 됩니다!

Git cheat sheet


ssh 키페어 만들기

ssh 키 페어 만들기

git 기본 명령어


git init  - 초기화
git add * - git에 파일 목록 넣기
git commit -am 'initial commit' - 커밋
git push repository_name master - 해당 브랜치에 커밋
git clone [email protected]:app/app.git - 코드를 로컬로  복제
cd app
git pull - 변경사항 풀


git 환경 설정


git config --global --list
현재 설정정보 조회할 수 있습니다. --global옵션은 전역설정에 대한 옵션이며 현재 프로젝트에만 적용할때는 주지 않습니다.
git config --global user.name "사용자명"
사용자명을 등록합니다 (필수)
git config --global user.email "이메일주소"
이메일 주소를 등록합니다. (필수)
git config --global color.ui “auto”
터미널에 표시되는 메시지에 칼라를 표시해 줍니다.



gitignore 사용하기


.gitignore
git config --global core.excludesfile ~/.gitignore_global
git rm -r --cached .
git add .
git commit -m ".gitignore 완성!"
git stash branch branchname
git fetch
git merge
git pull (git fetch + git merge branchname)


로컬 변경 무시하고 저장소에서 받아오기

모든 로컬 변경을 스태쉬 한다.
git stash save --keep-index
저장한 stash를 지운다.
git stash drop

파일 하나만 저장소에서 불러오려면 이렇게 한다.
git checkout HEAD^ path/to/file/to/revert

그리고 저장소에서 풀 한다.
git pull


특정 커밋으로 돌아가기
http://stackoverflow.com/questions/4114095/revert-to-previous-git-commit
git reset --hard 0d1d7fc32

Send a pull request on GitHub for only latest commit
http://stackoverflow.com/questions/5256021/send-a-pull-request-on-github-for-only-latest-commit
git checkout -b upstream upstream/master
git cherry-pick <SHA hash of commit>
git push origin upstream



How can I rollback a github repository to a specific commit?
http://stackoverflow.com/questions/4372435/how-can-i-rollback-a-github-repository-to-a-specific-commit
git reset --hard <old-commit-id>
git push -f



참고 자료

http://git-scm.com/
http://github.com/
아웃사이더님의 git 명령어 정리
구글 검색



by


Tags : , , , , ,

  • 재미있게 읽으셨나요?
    광고를 클릭해주시면,
    블로그 운영에 큰 도움이 됩니다!