Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / tox-run.sh
index 3bf1576..b30ea67 100644 (file)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/bash -l
 # SPDX-License-Identifier: EPL-1.0
 ##############################################################################
 # Copyright (c) 2017 The Linux Foundation and others.
 ##############################################################################
 echo "---> tox-run.sh"
 
-# Ensure we fail the job if any steps fail.
-# DO NOT set -u as virtualenv's activate script has unbound variables
-set -e -o pipefail
+# do not use -o pipefail
+set -eux
+
+#Ensure that tox from tox-install.sh takes precedence.
+PATH=$HOME/.local/bin:$PATH
 
 ARCHIVE_TOX_DIR="$WORKSPACE/archives/tox"
+ARCHIVE_DOC_DIR="$WORKSPACE/archives/docs"
 mkdir -p "$ARCHIVE_TOX_DIR"
+cd "$WORKSPACE/$TOX_DIR" || exit 1
+
+# shellcheck disable=SC1090
+source ~/lf-env.sh
 
-cd "$WORKSPACE/$TOX_DIR"
+lf-activate-venv --venv-file /tmp/.toxenv tox virtualenv urllib3~=1.26.15
 
-if [ -z "$TOX_ENVS" ]; then
-    TOX_ENVS=$(crudini --get tox.ini tox envlist)
+if [[ -d /opt/pyenv ]]; then
+    echo "---> Setting up pyenv"
+    export PYENV_ROOT="/opt/pyenv"
+    export PATH="$PYENV_ROOT/bin:$PATH"
+    PYTHONPATH="$(pwd)"
+    export PYTHONPATH
+    export TOX_TESTENV_PASSENV=PYTHONPATH
 fi
 
-run_tox() {
-    local log_dir="$1"
-    local env="$2"
+#Useful debug
+tox --version
+
+PARALLEL="${PARALLEL:-true}"
+TOX_OPTIONS_LIST=""
+
+if [[ -n ${TOX_ENVS:-} ]]; then
+    TOX_OPTIONS_LIST=$TOX_OPTIONS_LIST" -e $TOX_ENVS"
+fi;
+
+case ${PARALLEL,,} in
+    true|auto )
+        TOX_OPTIONS_LIST=$TOX_OPTIONS_LIST" --parallel auto --parallel-live";;
+    all )
+        TOX_OPTIONS_LIST=$TOX_OPTIONS_LIST" --parallel all --parallel-live";;
+    [0-9]* )
+        TOX_OPTIONS_LIST=$TOX_OPTIONS_LIST" --parallel ${PARALLEL} --parallel-live";;
+esac
 
-    # Sleep a random 10 second interval to workaround tox sdist
-    # conflicts due to building in the same dist directory.
-    sleep $[ ( $RANDOM % 10 )  + 1 ]s
 
-    echo "-----> Running tox $env"
-    if ! tox -e $env > "$log_dir/tox-$env.log"; then
-        echo "$env" >> "$log_dir/failed-envs.log"
+# $TOX_OPTIONS_LIST are intentionnaly not surrounded by quotes
+# to correcly pass options to tox
+# shellcheck disable=SC2086
+tox $TOX_OPTIONS_LIST | tee -a "$ARCHIVE_TOX_DIR/tox.log"
+tox_status="${PIPESTATUS[0]}"
+
+echo "---> Completed tox runs"
+
+# Disable SC2116 as we want to echo a space separated list of TOX_ENVS
+# shellcheck disable=SC2116
+for i in .tox/*/log; do
+    tox_env=$(echo "$i" | awk -F'/' '{print $2}')
+    # defend against glob finding no matches
+    if ! cp -r "$i" "$ARCHIVE_TOX_DIR/$tox_env"; then
+        echo "WARN: no logs found to archive"
+        break
     fi
-}
-
-TOX_ENVS=(${TOX_ENVS//,/ })
-if hash parallel 2>/dev/null; then
-    export -f run_tox
-    parallel --jobs 200% "run_tox $ARCHIVE_TOX_DIR {}" ::: ${TOX_ENVS[*]}
-else
-    for env in "${TOX_ENVS[@]}"; do
-        run_tox "$ARCHIVE_TOX_DIR" "$env"
-    done
-fi
+done
 
-if [ -f "$ARCHIVE_TOX_DIR/failed-envs.log" ]; then
-    failed_envs=($(cat "$ARCHIVE_TOX_DIR/failed-envs.log"))
-    echo "ERROR: Failed the following builds: ${failed_envs[*]}"
-    exit 1
+# If docs are generated push them to archives.
+DOC_DIR="${DOC_DIR:-docs/_build/html}"
+if [[ -d $DOC_DIR ]]; then
+    echo "---> Archiving generated docs"
+    mv "$DOC_DIR" "$ARCHIVE_DOC_DIR"
 fi
 
-echo "Completed tox runs."
+echo "---> tox-run.sh ends"
+
+test "$tox_status" -eq 0 || exit "$tox_status"