Skip to content

Commit c45aef3

Browse files
committed
Fix: Improve pip install reliability lf-act-venv
Enhance the lf-activate-venv function with robust error handling for pip installations. Add trusted host configurations, proxy detection, and fallback strategies to address common "Could not find a version" errors caused by SSL certificate issues, firewall restrictions, and network connectivity problems in CI/CD environments. Changes include: - Added trusted host flags for PyPI mirrors - Implemented proxy detection and configuration - Created fallback installation with verbose debugging - Added individual package installation as final fallback - Improved error messages with actionable troubleshooting guidance Change-Id: Ice9471c0bbd4bda471259f7345298de7ce61b9a3 Signed-off-by: Anil Belur <[email protected]>
1 parent c05412b commit c45aef3

File tree

2 files changed

+81
-7
lines changed

2 files changed

+81
-7
lines changed

jenkins-init-scripts/lf-env.sh

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,83 @@ lf-activate-venv () {
243243
echo "${FUNCNAME[0]}(): INFO: Save venv in file: $venv_file"
244244
fi
245245

246-
# Pin setuptools<66 until the issue with pbr + setuptools >=66 is fixed
247-
"$lf_venv/bin/python3" -m pip install --upgrade --quiet \
248-
pip 'setuptools<66' virtualenv || return 1
246+
echo "${FUNCNAME[0]}(): INFO: Installing base packages (pip, setuptools, virtualenv)"
247+
248+
# Define pip install options for network issues
249+
local pip_opts="--upgrade --quiet"
250+
251+
# Add trusted hosts to bypass SSL certificate issues
252+
# This is the most common cause of the "Could not find a version" error
253+
pip_opts="$pip_opts --trusted-host pypi.org"
254+
pip_opts="$pip_opts --trusted-host files.pythonhosted.org"
255+
pip_opts="$pip_opts --trusted-host pypi.python.org"
256+
257+
# If behind a corporate proxy, try to detect and handle it
258+
if [[ -n "${HTTP_PROXY:-}" || -n "${HTTPS_PROXY:-}" ]]; then
259+
echo "${FUNCNAME[0]}(): INFO: Proxy detected, using proxy settings"
260+
if [[ -n "${HTTP_PROXY:-}" ]]; then
261+
pip_opts="$pip_opts --proxy $HTTP_PROXY"
262+
elif [[ -n "${HTTPS_PROXY:-}" ]]; then
263+
pip_opts="$pip_opts --proxy $HTTPS_PROXY"
264+
fi
265+
fi
266+
267+
# First, try to install with enhanced options
268+
echo "${FUNCNAME[0]}(): INFO: Attempting to install with network-safe options..."
269+
if ! "$lf_venv/bin/python3" -m pip install "$pip_opts" \
270+
pip 'setuptools<66' virtualenv; then
271+
272+
echo "${FUNCNAME[0]}(): WARNING: Initial install failed, trying fallback options..."
273+
274+
# Fallback 1: Try with verbose output for debugging
275+
echo "${FUNCNAME[0]}(): INFO: Trying with verbose output for debugging..."
276+
if ! "$lf_venv/bin/python3" -m pip install --verbose --trusted-host pypi.org \
277+
--trusted-host files.pythonhosted.org \
278+
pip 'setuptools<66' virtualenv; then
279+
280+
# Fallback 2: Try installing packages one by one
281+
echo "${FUNCNAME[0]}(): WARNING: Batch install failed, trying individual packages..."
282+
283+
if ! "$lf_venv/bin/python3" -m pip install --trusted-host pypi.org \
284+
--trusted-host files.pythonhosted.org pip; then
285+
lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Cannot install pip - network connectivity issue"
286+
lf-echo-stderr "This suggests a firewall, proxy, or DNS resolution problem"
287+
lf-echo-stderr "Contact your network administrator or check proxy settings"
288+
return 1
289+
fi
290+
291+
if ! "$lf_venv/bin/python3" -m pip install --trusted-host pypi.org \
292+
--trusted-host files.pythonhosted.org 'setuptools<66'; then
293+
lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Cannot install setuptools"
294+
return 1
295+
fi
296+
297+
if ! "$lf_venv/bin/python3" -m pip install --trusted-host pypi.org \
298+
--trusted-host files.pythonhosted.org virtualenv; then
299+
lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Cannot install virtualenv"
300+
return 1
301+
fi
302+
303+
echo "${FUNCNAME[0]}(): INFO: Individual package installation succeeded"
304+
else
305+
echo "${FUNCNAME[0]}(): INFO: Fallback install succeeded"
306+
fi
307+
else
308+
echo "${FUNCNAME[0]}(): INFO: Base packages installed successfully"
309+
fi
310+
311+
# Continue with the rest of the package installation
249312
if [[ -z $pkg_list ]]; then
250-
echo "${FUNCNAME[0]}(): WARNING: No packages to install"
313+
echo "${FUNCNAME[0]}(): WARNING: No additional packages to install"
251314
else
252-
echo "${FUNCNAME[0]}(): INFO: Installing: $pkg_list"
315+
echo "${FUNCNAME[0]}(): INFO: Installing additional packages: $pkg_list"
253316
# $pkg_list is expected to be unquoted
254317
# shellcheck disable=SC2086
255-
"$lf_venv/bin/python3" -m pip install --upgrade --quiet \
256-
--upgrade-strategy eager $pkg_list || return 1
318+
if ! "$lf_venv/bin/python3" -m pip install "$pip_opts" \
319+
--upgrade-strategy eager $pkg_list; then
320+
lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Failed to install packages: $pkg_list"
321+
return 1
322+
fi
257323
fi
258324
;;
259325
*)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
fixes:
3+
- |
4+
Improve pip installation reliability in lf-activate-venv function by adding
5+
enhanced network error handling, trusted host configuration, proxy detection,
6+
and fallback installation strategies. This addresses common "Could not find
7+
a version" errors caused by SSL certificate issues, firewall restrictions,
8+
and network connectivity problems in CI/CD environments.

0 commit comments

Comments
 (0)