From 78b628159a70e2c81b88e2e50914f1b00fa60a42 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Tue, 10 Oct 2023 15:56:20 +0200 Subject: [PATCH] Use virtualenv in composite action (#501) --- README.md | 49 -------------------------------------------- composite/action.yml | 47 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index b1f8635d..e2050285 100644 --- a/README.md +++ b/README.md @@ -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. - -
-Isolating composite action from your workflow - -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" -``` -
- diff --git a/composite/action.yml b/composite/action.yml index 8c50cd75..c33b276f 100644 --- a/composite/action.yml +++ b/composite/action.yml @@ -186,6 +186,7 @@ runs: ;; Windows*) echo "pip-cache=~\\AppData\\Local\\pip\\Cache" >> $GITHUB_OUTPUT + echo "pip-options=--user" >> $GITHUB_OUTPUT ;; esac shell: bash @@ -198,12 +199,47 @@ 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 @@ -211,6 +247,13 @@ runs: 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: