Skip to content

Instantly share code, notes, and snippets.

@alswl
Created July 23, 2021 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alswl/23dbd313e0168b435fbec795c79f754d to your computer and use it in GitHub Desktop.
Save alswl/23dbd313e0168b435fbec795c79f754d to your computer and use it in GitHub Desktop.
Jenkinsfile for Python project with Chinese mirror, lint and versioned virtual env
pipeline {
agent {
docker {
image 'python:latest'
args '-v /var/lib/jenkins/caches/pipcache:/var/lib/jenkins/caches/pipcache -v /var/lib/jenkins/caches/durable-task:/var/lib/jenkins/caches/durable-task'
}
}
environment {
CI_PROJECT_DIR = '/var/lib/jenkins'
PIP_CACHE_DIR = "${CI_PROJECT_DIR}/caches/pipcache"
}
stages {
stage('validation') {
steps {
sh '''
pwd
echo $HOME
which python
which pip
mkdir -p $HOME/.pip
echo "[global]\\nindex-url = https://mirrors.aliyun.com/pypi/simple/\\ndownload-cache = $PIP_CACHE_DIR\\n" > $HOME/.pip/pip.conf
echo $HOME/.pip/pip.conf
cat $HOME/.pip/pip.conf
echo "[easy_install]\\nindex-url = https://mirrors.aliyun.com/pypi/simple/\\n" > $HOME/.pydistutils.cfg
echo $HOME/.pydistutils.cfg
cat $HOME/.pydistutils.cfg
export HASH=$(cat ./requirements.txt | md5sum | cut -d ' ' -f1)
echo ".venv/venv_${HASH}"
export VENV_EXIST=0
test -d .venv/venv_${HASH} && export VENV_EXIST=1
test ${VENV_EXIST} -eq 0 && pip install virtualenv
test ${VENV_EXIST} -eq 0 && mkdir -p .venv
test ${VENV_EXIST} -eq 0 && virtualenv -p /usr/local/bin/python .venv/venv_${HASH}
test ${VENV_EXIST} -eq 0 && chmod +x .venv/venv_${HASH}/bin/activate
. .venv/venv_${HASH}/bin/activate
test ${VENV_EXIST} -eq 0 && pip install -r ./requirements.txt
mkdir -p logs
pylint --load-plugins pylint_django -E --rcfile .pylintrc app
flake8 app
# no global
test `grep -R '^ *global ' -R app | wc -l` -gt 0 && exit -1;
# no ms-dos newline
test `grep --include '*.py' -E $'\\r' -R app -l | wc -l` -gt 0 && exit -1;
# no ms-dos newline
test `grep --include '*.sql' -E $'\\r' -R app -l | wc -l` -gt 0 && exit -1;
# no ms-dos newline
test `grep --include '*.sh' -E $'\\r' -R app -l | wc -l` -gt 0 && exit -1;
# all manager has type hint
test $(grep -E 'manager *=.*Manager\\(\\)' -R app/manager/*.py | wc -l) -gt 0 && exit -1;
# all service has type hint
test $(grep -E 'service *=.*Service\\(\\)' -R app/service/*.py | wc -l) -gt 0 && exit -1;
mypy --config-file .mypy.ini app
# stric mypy
#mypy --follow-imports skip --config-file .mypy_ng.ini manage.py $(git diff origin/master --name-only | grep '^app/.*.py$')
python ./manage.py test --settings=app.settings_unit_test --pattern="test_*.py"
black --check --config .pyproject.toml app manage.py bin/*.py
'''
}
}
}
post {
always {
echo 'build finished, clean up workspace'
deleteDir()
}
success {
echo 'success'
dingTalk accessToken: 'token',
jenkinsUrl: "${env.BUILD_URL}",
message: ' Success',
notifyPeople: ''
}
failure {
echo 'failure'
dingTalk accessToken: 'token',
jenkinsUrl: "${env.BUILD_URL}",
message: ' Failure',
notifyPeople: ''
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment