Do you guys test it? "Tox" that executes unit tests in Python in multiple environments. It's convenient, isn't it? If you create a CI environment with Jenkins, you can check the operation with multiple versions in one shot.
However, it takes a lot of time and effort to install pyenv and prepare multiple python environments. Therefore, it is an easy way to build an environment that uses tox in Jenkins.
The procedure is as follows.
This time, I will mainly explain the part that dynamically generates the CI environment using Jenkisfile. Therefore, I will omit the construction of Docker environment and Jenkins. However, please be sure to build Jenkins under Docker environment. __
If you are in a Windows environment, you should build it by referring to the following page. https://qiita.com/sh1928kd/items/b248a8d48d16eaab9617
One point, when sharing a Docker socket in Ubuntu environment etc., please be careful about the socket authority of the socket on the host side. If you do not have execute permission from Jenkins side, an error will occur.
The version of Jenkins at the time of writing the article is "Jenkins 2.255".
Put in whatever you like. This time I want to display the result of Unit test and coverage report, so install "JUnit" and "Cobertura".
You should be able to build the environment with just the Jenkins file, When I tried to use pyenv when building multiple Python environments, it didn't work. Therefore, we will use DockerFile to build multiple Python environments.
FROM python:3.8
USER root
ENV HOME /root
ENV PYENV_ROOT $HOME/.pyenv
ENV PATH $PYENV_ROOT/bin:$PATH
# ENV HTTP_PROXY '{Proxy server address}'
# ENV HTTPS_PROXY '{Proxy server address'
# ENV FTP_PROXY '{Proxy server address}'
# RUN echo 'Acquire::http::Proxy "{Proxy server address}";' >> /etc/apt/apt.conf
# RUN echo 'Acquire::https::Proxy "{Proxy server address}";' >> /etc/apt/apt.conf
RUN apt-get update && apt-get upgrade -y \
&& apt-get install -y \
git \
make \
build-essential \
libssl-dev \
zlib1g-dev \
libbz2-dev \
libreadline-dev \
libsqlite3-dev \
wget \
curl \
llvm \
libncurses5-dev \
libncursesw5-dev \
xz-utils \
tk-dev \
libffi-dev \
liblzma-dev \
&& git clone https://github.com/pyenv/pyenv.git $PYENV_ROOT
RUN echo 'eval "$(pyenv init -)"' >> ~/.bashrc && \
eval "$(pyenv init -)"
RUN pyenv install 3.5.9
RUN pyenv install 3.6.2
RUN pyenv install 3.7.2
RUN pyenv install 3.8.2
RUN echo '3.5.9' >> .python-version
RUN echo '3.6.2' >> .python-version
RUN echo '3.7.2' >> .python-version
RUN echo '3.8.2' >> .python-version
Jenkinsfile is not difficult once you build the environment with Dockerfile
pipeline {
agent {
dockerfile {
filename 'Dockerfile'
args '-u root:sudo'
}
}
stages {
stage('setup') {
steps {
sh 'pip install flake8'
sh 'pip install tox'
sh 'pip install tox-pyenv'
}
}
stage('test') {
steps {
sh 'tox'
//Get coverage
cobertura coberturaReportFile: 'coverage.xml'
//Get test results
junit 'junit_reports/*.xml'
}
}
}
}
It might have been good to describe the contents of setup in DockerFile.
Write tox.ini. This time we will get a test report and a coverage report, so we have set it to output each report
tox.ini
[tox]
envlist = py35, py36, py37, py38, flake8
[travis]
python =
3.8: py38
3.7: py37
3.6: py36
3.5: py35
[testenv:flake8]
basepython = python
deps = flake8
commands = flake8 {Module name to analyze} tests
[flake8]
exclude = tests/*
[testenv]
setenv =
PYTHONPATH = {toxinidir}
deps =
coverage
unittest-xml-reporting
commands =
rm -rf junit_reports
python -m xmlrunner discover -o junit_reports
coverage erase
coverage run --append setup.py test
coverage report --omit='.tox/*'
coverage xml --omit='.tox/*'
Basically, if it is a PYPI format project, it will have the following structure.
-project
|-module_dir/
|-main.py
|-tests/
|-test_main.py
|-setup.py
|-Dockerfile
|-Jenkinsfile
|-tox.ini
Push the completed script to Github etc.
Choose to create a new job,
On the settings screen
If you do "build execution" from the Jenkins job screen, you can build the environment from the Dockerfile and Jenkins will get the result of executing tox.
I introduced the environment to easily execute tox with Jenkins. I really wanted to build it with only Jenkinsfile, but I didn't understand the behavior around PATH, so I used a Dockerfile.
If you have a PyPI-style project structure, you can almost use it in this way, which makes it easier to build a test environment. In addition, since the raw environment is completed, there is an advantage that you should be aware of module registration omissions and dependent libraries.
Recommended Posts