Template for setting up a simple dev-ops pipeline for your Python code including code-analysis, testing and deployment.
This project is intended to be used as a template in order to set up a simple dev-ops pipeline for your Python code.
Features:
These use cases are accessible through a Makefile
. A summary of the most important make targets can be obtained by running
make help
The following sections serve as a quickstart guide. More detailed documentation:
The project structure follows ideas discussed on stackoverflow. Most importantly for the following top-level components:
README.md
file (this file).requirements.txt
file for setting up development environment (refers to setup.py
).setup.py
file for defining the app’s pip deployment package (including development dependencies).MANIFEST.in
file for advanced pip package build directives.LICENSE
for defining users’ rights and obligations.src
directory (redundant) but a top-level Python import package (here sampleproject
directory).tests
directory for unit tests.scripts
directory for storing scripts that are directly executable.Makefile
for setting up development environment, building, testing, code quality reporting, deployment (run make help
for an overview).Dockerfile
that defines how to build and deploy the app in a container.Documentation:
Automatically install dependencies and symlink your sources to your Python environment.
Note that also development dependencies will be installed automatically.
Development dependencies, like linter and test tools, can be managed in addition to runtime dependencies.
Prerequisites:
dev-ops
# virtual environment
python3 -m venv venv
source venv/bin/activate
# install dependencies and symlink sources to PYTHONPATH
make install-dev
# run application
sampleproject --help
Run code analyses in your local Python development environment.
Prerequisites:
dev-ops
# run linters
make lint
# run unit tests
make test
Start a SonarQube server. Run code analyses and report analysis results to SonarQube.
Prerequisites:
dev-ops
# start SonarQube Server
docker-compose -p sonarqube -f sonarqube/docker-compose.yml up -d
# wait until SonarQube has started at http://localhost:9000
Makefile
by assigning the Makefile
variable SONARTOKEN
to the authentication token you just generated. You can configure to use a different SonarQube server with the variable SONARURL
.Makefile
.
# run code analyses and report to SonarQube
make sonar
# in order to specify configuration variables run
# make sonar SONARURL=<url> SONARTOKEN=<token>
More details on how to set up a SonarQube server in a dockerized environment can be found here.
Build a Python wheel package for your application that can easily be installed (sources and runtime dependencies) in another Python environment.
Prerequisites:
dev-ops
# build the wheel
make dist
Test the installation of the package:
dev-ops
) and activate it.dev-ops/dist
with pip install
.More details on Python packaging can be found here.
Build a Docker image in two stages. The first stage runs unit tests, code analyses and builds a Python wheel package. The second stage installs the wheel from the first stage and is ready for deployment.
Notes:
Prerequisites:
dev-ops
# build the Docker image
make docker-build
# run container
docker run --rm sampleproject
More details on Docker deployment can be found here.
You can easily adapt the template for your own project.
<name>
for your project (here sampleproject
).<name>
. Directory must contain __init__.py
. This will be your top-level import package (e.g., import <name>
).<name>/__init__.py
(default is __version__ = '0.1.0'
).tests
directory. Directory must contain __init__.py
.scripts
directory. Not required necessarily because you can define entry points based on Python functions in setup.py
.setup.py
to your needs.name
to <name>
. Important: The name must match the name of the top-level import directory.find_packages
will search the include
directories, i.e., the top-level import directory and sub-directories according to wildcards.scripts
and/or entry_points
. Important: One executable must be called <name>
(see below).install_requires
.package_data
as needed.extras_require
as needed or define additional build targets.Makefile
to your needs.Dockerfile
to your needs. This should be uncommon since the definitions/configurations are rather generic.ENTRYPOINT
/ CMD
definition. Set the definition according to your own defaults (scripts/executables).user
in working directory /home/user/app
.Dockerfile
uses the bash script entrypoint.sh
as ENTRYPOINT
.
For this purpose, it is expected that an executable <name>
exists on the PATH
in your Docker container.
entrypoint.sh
executes the application <name>
with all command-line arguments provided to docker run
.<name>
of the application is obtained through an environment variable. The environment variable is defined in the Docker container, see Dockerfile
.Makefile
with setuptools
and passed as a build-arg
to Dockerfile
.