For the complete documentation index, see llms.txt. This page is also available as Markdown.

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.txt

Background steps can be used to run dependent services that are needed by steps in the same stage.

Cache 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:

Python cache key and path requirements

Python pipelines typically reference requirements.txt in Save Cache and Restore Cache steps, for example:

Additionally, spec.sourcePaths must include the python cache (typically /root/.cache) in the Save Cache step, for example:

YAML example: Save and restore cache steps

Here's an example of a pipeline with Save Cache to S3 and Restore Cache from S3 steps.

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.

Install one Python version
Install multiple Python versions
  1. Add the matrix looping strategy configuration to your stage.

  1. Reference the matrix variable in your steps.

Specify the desired Python Docker image tag in your steps. There is no need for a separate install step when using Docker.

Use a specific Python version
Use multiple Python versions
  1. Add the matrix looping strategy configuration to your stage.

  1. Reference the matrix variable in the image field of your steps.

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.

Pipeline with default Python version
Pipeline with multiple Python versions

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.

Pipeline with one specific Python version

Here is a single-stage pipeline, with steps that use Python 3.10.10.

Pipeline with multiple Python versions

Here is a single-stage pipeline, with a matrix looping strategy for Python versions 3.11.2 and 3.10.10.

Next steps

Now that you have created a pipeline that builds and tests a Python app, you could:

Last updated

Was this helpful?