Skip to content

Commit

Permalink
Use virtualenv in composite action (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
EnricoMi authored Oct 10, 2023
1 parent 48fc7ad commit 78b6281
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 51 deletions.
49 changes: 0 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -786,52 +786,3 @@ Self-hosted runners may require setting up a Python environment first:
Self-hosted runners for Windows require Bash shell to be installed. Easiest way to have one is by installing
Git for Windows, which comes with Git BASH. Make sure that the location of `bash.exe` is part of the `PATH`
environment variable seen by the self-hosted runner.

<details>
<summary>Isolating composite action from your workflow</summary>

Note that the composite action modifies this Python environment by installing dependency packages.
If this conflicts with actions that later run Python in the same workflow (which is a rare case),
it is recommended to run this action as the last step in your workflow, or to run it in an isolated workflow.
Running it in an isolated workflow is similar to the workflows shown in [Use with matrix strategy](#use-with-matrix-strategy).

To run the composite action in an isolated workflow, your CI workflow should upload all test result files:

```yaml
build-and-test:
name: "Build and Test"
runs-on: macos-latest
steps:
- …
- name: Upload Test Results
if: always()
uses: actions/upload-artifact@v3
with:
name: Test Results
path: "test-results/**/*.xml"
```

Your dedicated publish-test-results workflow then downloads these files and runs the action there:

```yaml
publish-test-results:
name: "Publish Tests Results"
needs: build-and-test
runs-on: windows-latest
# the build-and-test job might be skipped, we don't need to run this job then
if: success() || failure()
steps:
- name: Download Artifacts
uses: actions/download-artifact@v3
with:
path: artifacts
- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action/composite@v2
with:
files: "artifacts/**/*.xml"
```
</details>

47 changes: 45 additions & 2 deletions composite/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ runs:
;;
Windows*)
echo "pip-cache=~\\AppData\\Local\\pip\\Cache" >> $GITHUB_OUTPUT
echo "pip-options=--user" >> $GITHUB_OUTPUT
;;
esac
shell: bash
Expand All @@ -198,19 +199,61 @@ runs:
path: ${{ steps.os.outputs.pip-cache }}
key: enricomi-publish-action-${{ runner.os }}-${{ runner.arch }}-pip-${{ steps.python.outputs.version }}-df386fe4e04a72c96e140f0566a5c849

- name: Create virtualenv
id: venv
continue-on-error: true
env:
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
case "$RUNNER_OS" in
Linux*|macOS*)
source enricomi-publish-action-venv/bin/activate;;
Windows*)
source enricomi-publish-action-venv\\Scripts\\activate;;
esac
which python3
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 wheel
python3 -m pip install -r $GITHUB_ACTION_PATH/../python/requirements.txt
python3 -m pip install $PIP_OPTIONS wheel
python3 -m pip install $PIP_OPTIONS -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
echo '##[endgroup]'
env:
Expand Down

0 comments on commit 78b6281

Please sign in to comment.