From a86705ba8929e40ca94723194be1711ca940267c Mon Sep 17 00:00:00 2001 From: per1234 Date: Thu, 16 Mar 2023 15:59:08 -0700 Subject: [PATCH] Use `actions/setup-python` action to install Python for action At the time the arduino/compile-sketches action was converted to a composite action, the only step type supported for this action type was "run", where shell commands are executed. For this reason, it was necessary to use shell commands to install Python. Since that time, support was added for using other actions in composite action steps. This allows Python to be set up for use by the action in a more simple and efficient manner via the `actions/setup-python` action. The Python-based infrastructure workflows are also migrated to using `actions/setup-python` action for installing Python. In order to ensure the same version of Python is used for CI as the action, the version of Python used by the project is defined in a `.python-version` file, and all `actions/setup-python` steps pointed to that file. In order to ensure the stability of the action, the `actions/setup-python` action is pinned to a patch version. Although the common practice is to only pin the major version of actions in CI workflows, in this case there is no benefit to doing so for the `actions/setup-python` action since patch level bumps are going to be received from Dependabot regardless. --- .github/workflows/lint-python.yml | 9 ++++++++- .github/workflows/test-python.yml | 15 +++++++++++---- .python-version | 1 + action-setup.sh | 20 +++----------------- action.yml | 7 ++++++- 5 files changed, 29 insertions(+), 23 deletions(-) create mode 100644 .python-version diff --git a/.github/workflows/lint-python.yml b/.github/workflows/lint-python.yml index 5608b28..60fb062 100644 --- a/.github/workflows/lint-python.yml +++ b/.github/workflows/lint-python.yml @@ -5,11 +5,13 @@ on: paths: - '.github/workflows/lint-python.yml' - 'compilesketches/**.py' + - '.python-version' push: paths: - '.github/workflows/lint-python.yml' - 'compilesketches/**.py' + - '.python-version' # Scheduled trigger checks for workflow failures resulting from updates to the linting tools schedule: @@ -32,6 +34,11 @@ jobs: - name: Checkout uses: actions/checkout@v3 + - name: Install Python + uses: actions/setup-python@v4.5.0 + with: + python-version-file: .python-version + - name: Run the set up script id: setup run: | @@ -40,7 +47,7 @@ jobs: - name: Install flake8 run: | source "${{ steps.setup.outputs.python-venv-activate-script-path }}" - "${{ steps.setup.outputs.python-command }}" \ + python \ -m \ pip install \ flake8 \ diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index a2fc608..970436d 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -4,12 +4,14 @@ on: pull_request: paths: - '.github/workflows/test-python.yml' + - '.python-version' - 'action-setup.sh' - 'compilesketches/**' push: paths: - '.github/workflows/test-python.yml' + - '.python-version' - 'action-setup.sh' - 'compilesketches/**' @@ -39,6 +41,11 @@ jobs: - name: Checkout uses: actions/checkout@v3 + - name: Install Python + uses: actions/setup-python@v4.5.0 + with: + python-version-file: .python-version + - name: Run the set up script id: setup run: | @@ -47,7 +54,7 @@ jobs: - name: Install test dependencies run: | source "${{ steps.setup.outputs.python-venv-activate-script-path }}" - "${{ steps.setup.outputs.python-command }}" \ + python \ -m \ pip install \ --requirement "${{ env.PYTHON_PROJECT_TESTS_PATH }}/requirements.txt" @@ -56,7 +63,7 @@ jobs: run: | source "${{ steps.setup.outputs.python-venv-activate-script-path }}" export PYTHONPATH="${{ env.PYTHON_PROJECT_PATH }}" - "${{ steps.setup.outputs.python-command }}" \ + python \ -m \ coverage run \ --rcfile="${{ env.PYTHON_PROJECT_TESTS_PATH }}/.coveragerc" \ @@ -65,7 +72,7 @@ jobs: pytest "${{ env.PYTHON_PROJECT_TESTS_PATH }}" # Generate coverage data file for consumption by `codecov/codecov-action`. # Otherwise that action generates the file using the system Python environment, which doesn't work. - "${{ steps.setup.outputs.python-command }}" \ + python \ -m \ coverage xml \ -o "${{ github.workspace }}/${{ env.COVERAGE_DATA_FILENAME }}" @@ -73,7 +80,7 @@ jobs: - name: Display code coverage report run: | source "${{ steps.setup.outputs.python-venv-activate-script-path }}" - "${{ steps.setup.outputs.python-command }}" \ + python \ -m \ coverage report diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..cc1923a --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.8 diff --git a/action-setup.sh b/action-setup.sh index 61c09a1..c44eb94 100755 --- a/action-setup.sh +++ b/action-setup.sh @@ -2,8 +2,6 @@ # Set up the Python environment for the action's script to run in -readonly PYTHON_PACKAGE_VERSION='3.8' - # https://stackoverflow.com/a/29835459 SCRIPT_PATH="$( CDPATH='' \ @@ -15,32 +13,20 @@ SCRIPT_PATH="$( )" readonly SCRIPT_PATH -readonly PYTHON_COMMAND="python${PYTHON_PACKAGE_VERSION}" readonly PYTHON_VENV_PATH="${SCRIPT_PATH}/compilesketches/.venv" readonly PYTHON_VENV_ACTIVATE_SCRIPT_PATH="${PYTHON_VENV_PATH}/bin/activate" -# Install Python -sudo apt-get install --yes software-properties-common > /dev/null -sudo add-apt-repository --yes ppa:deadsnakes/ppa > /dev/null -sudo apt-get update --yes > /dev/null -sudo apt-get install --yes python${PYTHON_PACKAGE_VERSION} > /dev/null -echo "Using Python version: $("$PYTHON_COMMAND" --version)" - -sudo apt-get install --yes python3-setuptools > /dev/null -sudo apt-get install --yes python${PYTHON_PACKAGE_VERSION}-venv > /dev/null - # Create Python virtual environment -"$PYTHON_COMMAND" -m venv --system-site-packages "$PYTHON_VENV_PATH" +python -m venv --system-site-packages "$PYTHON_VENV_PATH" # Activate Python virtual environment # shellcheck source=/dev/null . "$PYTHON_VENV_ACTIVATE_SCRIPT_PATH" # Install Python dependencies -"$PYTHON_COMMAND" -m pip install --upgrade pip > /dev/null -"$PYTHON_COMMAND" -m pip install --quiet --requirement "${SCRIPT_PATH}/compilesketches/requirements.txt" +python -m pip install --upgrade pip > /dev/null +python -m pip install --quiet --requirement "${SCRIPT_PATH}/compilesketches/requirements.txt" # Set outputs for use in GitHub Actions workflow steps # See: https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-output-parameter -echo "::set-output name=python-command::$PYTHON_COMMAND" echo "::set-output name=python-venv-activate-script-path::$PYTHON_VENV_ACTIVATE_SCRIPT_PATH" diff --git a/action.yml b/action.yml index 1f593b4..edd2eda 100644 --- a/action.yml +++ b/action.yml @@ -49,6 +49,11 @@ inputs: runs: using: composite steps: + - name: Install Python + uses: actions/setup-python@v4.5.0 + with: + python-version-file: ${{ github.action_path }}/.python-version + - name: Run the set up script id: setup shell: bash @@ -74,4 +79,4 @@ runs: INPUT_SKETCHES-REPORT-PATH: ${{ inputs.sketches-report-path }} run: | source "${{ steps.setup.outputs.python-venv-activate-script-path }}" - "${{ steps.setup.outputs.python-command }}" "${{ github.action_path }}/compilesketches/compilesketches.py" + python "${{ github.action_path }}/compilesketches/compilesketches.py"