Python
Use a CI pipeline to build and test a Python application.
You can build and test a Python application using a Linux platform on Harness Cloud or a self-managed Kubernetes cluster build infrastructure.
This guide assumes you've created a Harness CI pipeline.
Install dependencies
Use Run steps to install dependencies in the build environment.
- step:
type: Run
identifier: dependencies
name: Dependencies
spec:
shell: Sh
command: |-
python -m pip install --upgrade pip
pip install -r requirements.txt
envVariables:
PIP_CACHE_DIR: "/root/.cache"- step:
type: Run
identifier: dependencies
name: Dependencies
spec:
connectorRef: YOUR_IMAGE_REGISTRY_CONNECTOR
image: python:latest
command: |-
python -m pip install --upgrade pip
pip install -r requirements.txtCache dependencies
Add caching to your stage.
Cache your Python module dependencies with Cache Intelligence.
Add caching to your stage.spec:
You can use built-in steps to:
Run tests
You can use Run and Test steps to run tests in Harness CI.
These examples run tests in a Run step.
Visualize test results
If you want to view test results in Harness, make sure your test commands produce reports in JUnit XML format.
If you run tests in a Run step, your Run step must include the reports specification. The reports specification is not required for Test steps (Test Intelligence).
Run tests with Test Intelligence
Test Intelligence is available for Python.
Test splitting
Harness CI supports test splitting (parallelism) for both Run and Test steps.
Specify version
Python is pre-installed on Harness Cloud runners. For details about all available tools and versions, go to Platforms and image specifications.
If your application requires a specific Python version, add a Run step to install it.
Specify the desired Python Docker image tag in your steps. There is no need for a separate install step when using Docker.
Full pipeline examples
The following full pipeline examples are based on the partial examples above.
This pipeline uses Harness Cloud build infrastructure and Cache Intelligence.
If you copy this example, replace the placeholder values with appropriate values for your code repo connector and repository name. Depending on your project and organization, you may also need to replace projectIdentifier and orgIdentifier.
If you copy this example, replace the placeholder values with appropriate values for your code repo connector, kubernetes cluster connector, kubernetes namespace, and repository name. Depending on your project and organization, you may also need to replace projectIdentifier and orgIdentifier.
Next steps
Now that you have created a pipeline that builds and tests a Python app, you could:
Create triggers to automatically run your pipeline.
Add steps to build and upload artifacts.
Add a step to build and push an image to a Docker registry.
Last updated
Was this helpful?