Improve CI workflow for backend tests by enhancing virtual environment setup

- Added a function to install the python3-venv package if the virtual environment creation fails due to missing ensurepip.
- Improved error handling and messaging for the installation process, ensuring compatibility with both root and non-root users.
- Streamlined the activation of the virtual environment for better clarity in the CI pipeline.
This commit is contained in:
2026-03-24 12:37:27 -05:00
parent c8e6cb568a
commit b1a74d8e30

View File

@@ -27,6 +27,8 @@ jobs:
# No usamos actions/setup-python: en runners self-hosted (Debian) suele fallar # No usamos actions/setup-python: en runners self-hosted (Debian) suele fallar
# al descargar versiones desde el manifest. Se usa python3 del sistema + venv. # al descargar versiones desde el manifest. Se usa python3 del sistema + venv.
# En Debian/Ubuntu hace falta el paquete python3-venv (ensurepip); si falla el venv,
# se intenta instalar con apt (root en act/containers, sudo en servidor típico).
- name: Instalar dependencias y ejecutar tests del backend - name: Instalar dependencias y ejecutar tests del backend
env: env:
TEST_DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }} TEST_DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }}
@@ -38,11 +40,27 @@ jobs:
exit 1 exit 1
fi fi
python3 --version python3 --version
if ! python3 -m venv "$GITHUB_WORKSPACE/.pytest-venv" 2>/dev/null; then VENV="$GITHUB_WORKSPACE/.pytest-venv"
echo "python3 -m venv falló. En Debian/Ubuntu instala: sudo apt-get install -y python3-venv" install_python3_venv() {
exit 1 export DEBIAN_FRONTEND=noninteractive
if ! command -v apt-get >/dev/null 2>&1; then
echo "::error::No hay apt-get. Instala manualmente el paquete equivalente a python3-venv."
return 1
fi
if [ "$(id -u)" -eq 0 ]; then
apt-get update -qq
apt-get install -y python3-venv
else
sudo apt-get update -qq
sudo apt-get install -y python3-venv
fi
}
if ! python3 -m venv "$VENV" 2>/dev/null; then
echo "venv falló (ensurepip no disponible). Instalando python3-venv..."
install_python3_venv
python3 -m venv "$VENV"
fi fi
. "$GITHUB_WORKSPACE/.pytest-venv/bin/activate" . "$VENV/bin/activate"
python -m pip install --upgrade pip python -m pip install --upgrade pip
pip install -r "$GITHUB_WORKSPACE/backend/requirements.txt" pip install -r "$GITHUB_WORKSPACE/backend/requirements.txt"
cd "$GITHUB_WORKSPACE/backend" cd "$GITHUB_WORKSPACE/backend"