1. 程式人生 > 實用技巧 >使用git的進行持續部署

使用git的進行持續部署

使用git的hook進行持續部署

總結自

githook持續部署

git的bare倉庫的作用

安裝git

  1. Centos

    yum install -y git

  2. Ubuntu

    apt-get -y git

  3. windows

    https://git-scm.com/ 下載安裝

建立git bare倉庫

cd /opt
git init repo.git --bare

編寫hook檔案

cd repo.git/hooks
vim post-receive
---
#!/bin/bash
GIT_DIR=/opt/repo.git
WORK_DIR=/opt/app/
echo 'server: received code push...'
cd ${WORK_DIR}
echo 'server: checkout latest code from git...'
git --git-dir=${GIT_DIR} --work-tree=${WORK_DIR} checkout master -f
echo 'server: build code...'
# build code 
---
# 編寫完成後儲存並賦予執行許可權
chmod +x post-receive
# build code demo
# python 
if [ -d venv ];then
   . venv/bin/activate
else
   python3 -m venv venv && . venv/bin/activate
fi
pip install -r requirements.txt
python manage.py run

# java 
mvn clean install
cd target
java -jar jar.jar

在本地庫上新增遠端推送地址

git remote add prod ssh://[email protected]/opt/repo.git

推送到本地git庫

git push prod master