tekton-hub/tasks/pytest/task.yaml
2025-04-10 14:02:12 +00:00

121 lines
3.4 KiB
YAML

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: pytest
spec:
params:
- name: subdirectory
type: string
description: Subdirectory within the workspace where the tests are located
default: ""
- name: python-version
type: string
description: Python version to use (e.g., 3.9, 3.11)
default: "3.9"
- name: pypi-username
type: string
description: PyPI username (fallback)
default: ""
- name: pypi-password
type: string
description: PyPI password or token (fallback)
default: ""
- name: pypi-group-url
type: string
description: PyPI repository URL (install endpoint)
default: https://upload.pypi.org/legacy/
workspaces:
- name: source
description: Workspace containing the cloned Git repository from git-clone-checkout
- name: pypi-auth
optional: true
description: |
A workspace containing authentication credentials for a private PyPI repository.
Should include:
- username
- password
steps:
- name: install-dependencies
image: python:$(params.python-version)-slim
workingDir: /workspace/source
script: |
#!/usr/bin/env bash
set -e
if [ -n "$(params.subdirectory)" ]; then
cd "$(params.subdirectory)"
fi
pip install --upgrade pip
pip install pytest
if [ -f pyproject.toml ]; then
echo "[INFO] Using Poetry to install dependencies"
pip install poetry
poetry config virtualenvs.in-project true
poetry lock --no-cache --regenerate
PYPI_USER="$(params.pypi-username)"
PYPI_PASS="$(params.pypi-password)"
PYPI_URL="$(params.pypi-group-url)"
if [ -f /workspace/pypi-auth/username ]; then
PYPI_USER=$(cat /workspace/pypi-auth/username)
fi
if [ -f /workspace/pypi-auth/password ]; then
PYPI_PASS=$(cat /workspace/pypi-auth/password)
fi
if [ -n "$PYPI_URL" ]; then
REPO_NAME=$(echo "$PYPI_URL" | sed -E 's#https?://([^/]+)/repository/([^/]+)/?.*#\1_\2#' | tr -cd '[:alnum:]_')
echo "[INFO] Configuring poetry source '$REPO_NAME': $PYPI_URL"
poetry config repositories."$REPO_NAME" "$PYPI_URL"
poetry config http-basic."$REPO_NAME" "$PYPI_USER" "$PYPI_PASS"
fi
poetry install --no-root
elif [ -f requirements.txt ]; then
echo "[INFO] Using pip to install dependencies"
pip install -r requirements.txt
fi
- name: run-tests
image: python:$(params.python-version)-slim
workingDir: /workspace/source
script: |
#!/usr/bin/env bash
set -e
if [ -n "$(params.subdirectory)" ]; then
cd "$(params.subdirectory)"
fi
if [ -f pyproject.toml ]; then
poetry run pytest --verbose --junitxml=/workspace/source/pytest-results.xml
else
pytest --verbose --junitxml=/workspace/source/pytest-results.xml
fi
onError: continue
- name: check-results
image: ubuntu
workingDir: /workspace/source
script: |
#!/usr/bin/env bash
set -e
if [ -f pytest-results.xml ]; then
echo "Test results generated: pytest-results.xml"
cat pytest-results.xml
else
echo "No test results found."
exit 1
fi