上一篇介紹如果把專案部署到主機,今天會使用 Jenkins 還自動這些步驟,主要步驟如下:
首頁
創建專案
設定原始碼
設定觸發建置,每分鐘檢查是否有更新
*/1 * * * *
安裝必要軟體:Debain Linux
apt-get install -y make build-essential libssl-dev zlib1g-dev libssl1.0-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev
專案建置腳本 prepare.sh
#!/bin/bash
# 套用環境變數
. ~/.bash_profile
# 檢查pyenv
which pyenv
if [ $? == "0" ]; then
echo "already have pyenv"
else
echo ""
git clone git://github.com/yyuu/pyenv.git ~/.pyenv
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
. ~/.bash_profile
pyenv install 3.5.0
pyenv global 3.5.0
pip install --upgrade pip setuptools virtualenv
fi
# 設定專案
PYENV_HOME="`pwd`/env/"
echo ${PYENV_HOME}
if [ -d ${PYENV_HOME} ]; then
. ${PYENV_HOME}/bin/activate
# 重新安裝 oklib
#pip uninstall -y oklib
#pip install git+ssh://git@bitbucket.org/okborn/oklib.git#egg=v1.6
else
pyenv global 3.5.0
virtualenv ${PYENV_HOME}
. ${PYENV_HOME}/bin/activate
# 準備專案相依插件
pip install cryptography psycopg2 SQLAlchemy psycopg2-binary
pip install git+ssh://git@bitbucket.org/okborn/oklib.git#egg=v1.6
python setup.py develop
pip install -e ".[testing]"
fi
# 初始化 DB
rm -f ithome_pellok_2018.sqlite
${PYENV_HOME}/bin/initialize_ithome_pellok_2018_db production.ini#ithome_pellok_2018
Jenkins Shell Script 執行部署腳本,並且執行測試程式
bash prepare.sh
env/bin/py.test --junitxml=pytest_result.xml --cov=ithome_pellok_2018 --cov-report=xml
覆蓋率報表
coverage.xml
測試報告
pytest_result.xml
Jenkins Server 是一個很好的 CI/CD 伺服器,還蠻多人使用也很好用,在這邊串起了建置部署,並且執行測試程式,並且會出報告(測試報告、覆蓋率報告),這邊有幾個重點和大家提醒一下: