Skip to content

Commit

Permalink
Check if venv package is installed already (#558)
Browse files Browse the repository at this point in the history
* Uses installed `virtualenv` and `venv` packages
* Installs `virtualenv` if none of the above works
* Installs without `PIP_OPTIONS` if `pip install $PIP_OPTIONS` does not work
* Creates a virtual environment
* Does not activate the virtual environment but uses the Python binary directly
  • Loading branch information
EnricoMi authored Feb 3, 2024
1 parent 8551547 commit 5a94863
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 40 deletions.
17 changes: 16 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ jobs:
- os: macos-latest
os-label: macOS
python: "3.8"
- os: macos-latest
os-label: macOS
python: "venv"
- os: macos-latest
os-label: macOS
python: "installed"
Expand All @@ -242,6 +245,9 @@ jobs:
- os: ubuntu-latest
os-label: Linux
python: "3.8"
- os: ubuntu-latest
os-label: Linux
python: "venv"
- os: ubuntu-latest
os-label: Linux
python: "installed"
Expand All @@ -252,6 +258,9 @@ jobs:
- os: windows-latest
os-label: Windows
python: "installed"
- os: windows-latest
os-label: Windows
python: "venv"
- os: windows-2019
os-label: Windows 2019
python: "installed"
Expand All @@ -261,11 +270,17 @@ jobs:
uses: actions/checkout@v4

- name: Setup Python
if: matrix.python != 'installed'
if: matrix.python != 'installed' && matrix.python != 'venv'
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}

- name: Install virtualenv
if: matrix.python == 'venv'
run: |
python3 -m pip install virtualenv
shell: bash

- name: Download Artifacts
uses: actions/download-artifact@v3
with:
Expand Down
74 changes: 35 additions & 39 deletions composite/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,14 @@ runs:
exit 1
fi
interpreter="$(which python)"
if [[ ! -e "${interpreter}3" ]]
then
mkdir -p "$RUNNER_TEMP/bin/"
ln -s "$interpreter" "$RUNNER_TEMP/bin/python3"
echo "$RUNNER_TEMP/bin" >> $GITHUB_PATH
fi
PYTHON_BIN=$(python -c 'import sys; print(sys.executable)')
else
PYTHON_BIN=$(python3 -c 'import sys; print(sys.executable)')
fi
echo "version=$(python3 -V)" >> $GITHUB_OUTPUT
echo "Python that creates venv: $PYTHON_BIN"
echo "PYTHON_BIN=$PYTHON_BIN" >> $GITHUB_ENV
echo "version=$($PYTHON_BIN -V)" >> $GITHUB_OUTPUT
echo '##[endgroup]'
shell: bash

Expand Down Expand Up @@ -210,55 +209,52 @@ runs:
PIP_OPTIONS: ${{ steps.os.outputs.pip-options }}
run: |
echo '##[group]Create virtualenv'
# install virtualenv, if it is not yet installed
python3 -m pip install $PIP_OPTIONS virtualenv
python3 -m virtualenv enricomi-publish-action-venv
# test activating virtualenv
echo "Python that creates venv: $PYTHON_BIN"
echo "Creating virtual environment"
if ! $PYTHON_BIN -m virtualenv enricomi-publish-action-venv && ! $PYTHON_BIN -m venv enricomi-publish-action-venv
then
echo "Looks like there is neither virtualenv nor venv package installed"
if ! $PYTHON_BIN -m pip install $PIP_OPTIONS virtualenv && [ -n "$PIP_OPTIONS" ]
then
echo "Installing virtualenv package with PIP options '$PIP_OPTIONS' failed, now trying without"
$PYTHON_BIN -m pip install virtualenv
fi
if ! $PYTHON_BIN -m virtualenv enricomi-publish-action-venv
then
echo "Still cannot create venv after installing virtualenv package"
exit 1
fi
fi
echo "Finding Python interpreter in venv"
case "$RUNNER_OS" in
Linux*|macOS*)
source enricomi-publish-action-venv/bin/activate;;
PYTHON_VENV="enricomi-publish-action-venv/bin/python";;
Windows*)
source enricomi-publish-action-venv\\Scripts\\activate;;
PYTHON_VENV="enricomi-publish-action-venv\\Scripts\\python";;
esac
which python3
PYTHON_VENV=$($PYTHON_VENV -c 'import sys; print(sys.executable)')
echo "Python in venv: $PYTHON_VENV"
echo "PYTHON_VENV=$PYTHON_VENV" >> $GITHUB_ENV
echo '##[endgroup]'
shell: bash

- name: Install Python dependencies
env:
PIP_OPTIONS: ${{ steps.os.outputs.pip-options }}
run: |
echo '##[group]Install Python dependencies'
if [ "${{ steps.venv.outcome }}" == "success" ]
then
# activate virtualenv
case "$RUNNER_OS" in
Linux*|macOS*)
source enricomi-publish-action-venv/bin/activate;;
Windows*)
source enricomi-publish-action-venv\\Scripts\\activate;;
esac
fi
which python3
# make sure wheel is installed, which improves installing our dependencies
python3 -m pip install $PIP_OPTIONS wheel
python3 -m pip install $PIP_OPTIONS -r $GITHUB_ACTION_PATH/../python/requirements.txt
$PYTHON_VENV -m pip install wheel
$PYTHON_VENV -m pip install -r $GITHUB_ACTION_PATH/../python/requirements.txt
echo '##[endgroup]'
shell: bash

- name: Publish Test Results
id: test-results
run: |
echo '##[group]Publish Test Results'
# activate virtualenv
case "$RUNNER_OS" in
Linux*|macOS*)
source enricomi-publish-action-venv/bin/activate;;
Windows*)
source enricomi-publish-action-venv\\Scripts\\activate;;
esac
python3 $GITHUB_ACTION_PATH/../python/publish_test_results.py
$PYTHON_VENV $GITHUB_ACTION_PATH/../python/publish_test_results.py
echo '##[endgroup]'
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
Expand Down

0 comments on commit 5a94863

Please sign in to comment.