Skip to content

Commit

Permalink
Merge pull request arduino#105 from per1234/uses-setup
Browse files Browse the repository at this point in the history
Use `actions/setup-python` action to install Python for action
  • Loading branch information
per1234 authored Mar 17, 2023
2 parents d210132 + a86705b commit e5c4238
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/lint-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -32,6 +34,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Install Python
uses: actions/[email protected]
with:
python-version-file: .python-version

- name: Run the set up script
id: setup
run: |
Expand All @@ -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 \
Expand Down
15 changes: 11 additions & 4 deletions .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/**'

Expand Down Expand Up @@ -39,6 +41,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Install Python
uses: actions/[email protected]
with:
python-version-file: .python-version

- name: Run the set up script
id: setup
run: |
Expand All @@ -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"
Expand All @@ -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" \
Expand All @@ -65,15 +72,15 @@ 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 }}"
- name: Display code coverage report
run: |
source "${{ steps.setup.outputs.python-venv-activate-script-path }}"
"${{ steps.setup.outputs.python-command }}" \
python \
-m \
coverage report
Expand Down
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.8
20 changes: 3 additions & 17 deletions action-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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='' \
Expand All @@ -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"
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ inputs:
runs:
using: composite
steps:
- name: Install Python
uses: actions/[email protected]
with:
python-version-file: ${{ github.action_path }}/.python-version

- name: Run the set up script
id: setup
shell: bash
Expand All @@ -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"

0 comments on commit e5c4238

Please sign in to comment.