When setting up CI for python projects, virtualenv could help to separate the test/deployment environment. This post will provide a template for Jenkins to use virtualenv for Jenkins tasks with Jenkins example.
Common Test Script Template
Here is the script I used on Jenkins
There are two requirements files in my repo: requirements.txt and test/test_requirements.txt. The later one defines the prerequisites for test using as told by name and location. Special attention is recommended to take for nose, coverage, pylint and codecov (if using travis).
The test_requirements.txt has below contents.
Configure Jenkins with above test
To utilize above bash script, we can add one shell execution step on Jenkins task management portal.
bash -x test/nosetests_ci-stat.sh
Sometimes the build-step is divided to several steps including virtualenv, nose test, and, lint scoring. Explicit "shell exeuction" steps with Jenkins make the logic clear to maintain and reuse.
Some other times, the test is based on PyPi package and virtualenv could offer a separate environment to install real packages for test using.
It is a critical security concern to protect environmental variables if there is configuration items applied through them. For example, I have local config.yaml file to specify my Travis and Circle CI API token, but this config.yaml is in gitignore file. To read such secrets, I configure Jenkins secret as in menu "Build Environment -> Binding". For environmental variables on Travis, it has protection not to show plain text contents in logs so which is secured automatically.
Post Build Steps
Three popular but fundamental plugins will be introduced: - JUnit Test Result Publishment. - Violation Report: The lint scan results. - Cobertura Coverage Report: the coverage data (for given Python package).