Skip to content

Commit a86705b

Browse files
committed
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.
1 parent de5fc19 commit a86705b

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

.github/workflows/lint-python.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ on:
55
paths:
66
- '.github/workflows/lint-python.yml'
77
- 'compilesketches/**.py'
8+
- '.python-version'
89

910
push:
1011
paths:
1112
- '.github/workflows/lint-python.yml'
1213
- 'compilesketches/**.py'
14+
- '.python-version'
1315

1416
# Scheduled trigger checks for workflow failures resulting from updates to the linting tools
1517
schedule:
@@ -32,6 +34,11 @@ jobs:
3234
- name: Checkout
3335
uses: actions/checkout@v3
3436

37+
- name: Install Python
38+
uses: actions/setup-python@v4.5.0
39+
with:
40+
python-version-file: .python-version
41+
3542
- name: Run the set up script
3643
id: setup
3744
run: |
@@ -40,7 +47,7 @@ jobs:
4047
- name: Install flake8
4148
run: |
4249
source "${{ steps.setup.outputs.python-venv-activate-script-path }}"
43-
"${{ steps.setup.outputs.python-command }}" \
50+
python \
4451
-m \
4552
pip install \
4653
flake8 \

.github/workflows/test-python.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ on:
44
pull_request:
55
paths:
66
- '.github/workflows/test-python.yml'
7+
- '.python-version'
78
- 'action-setup.sh'
89
- 'compilesketches/**'
910

1011
push:
1112
paths:
1213
- '.github/workflows/test-python.yml'
14+
- '.python-version'
1315
- 'action-setup.sh'
1416
- 'compilesketches/**'
1517

@@ -39,6 +41,11 @@ jobs:
3941
- name: Checkout
4042
uses: actions/checkout@v3
4143

44+
- name: Install Python
45+
uses: actions/setup-python@v4.5.0
46+
with:
47+
python-version-file: .python-version
48+
4249
- name: Run the set up script
4350
id: setup
4451
run: |
@@ -47,7 +54,7 @@ jobs:
4754
- name: Install test dependencies
4855
run: |
4956
source "${{ steps.setup.outputs.python-venv-activate-script-path }}"
50-
"${{ steps.setup.outputs.python-command }}" \
57+
python \
5158
-m \
5259
pip install \
5360
--requirement "${{ env.PYTHON_PROJECT_TESTS_PATH }}/requirements.txt"
@@ -56,7 +63,7 @@ jobs:
5663
run: |
5764
source "${{ steps.setup.outputs.python-venv-activate-script-path }}"
5865
export PYTHONPATH="${{ env.PYTHON_PROJECT_PATH }}"
59-
"${{ steps.setup.outputs.python-command }}" \
66+
python \
6067
-m \
6168
coverage run \
6269
--rcfile="${{ env.PYTHON_PROJECT_TESTS_PATH }}/.coveragerc" \
@@ -65,15 +72,15 @@ jobs:
6572
pytest "${{ env.PYTHON_PROJECT_TESTS_PATH }}"
6673
# Generate coverage data file for consumption by `codecov/codecov-action`.
6774
# Otherwise that action generates the file using the system Python environment, which doesn't work.
68-
"${{ steps.setup.outputs.python-command }}" \
75+
python \
6976
-m \
7077
coverage xml \
7178
-o "${{ github.workspace }}/${{ env.COVERAGE_DATA_FILENAME }}"
7279
7380
- name: Display code coverage report
7481
run: |
7582
source "${{ steps.setup.outputs.python-venv-activate-script-path }}"
76-
"${{ steps.setup.outputs.python-command }}" \
83+
python \
7784
-m \
7885
coverage report
7986

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.8

action-setup.sh

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
# Set up the Python environment for the action's script to run in
44

5-
readonly PYTHON_PACKAGE_VERSION='3.8'
6-
75
# https://stackoverflow.com/a/29835459
86
SCRIPT_PATH="$(
97
CDPATH='' \
@@ -15,32 +13,20 @@ SCRIPT_PATH="$(
1513
)"
1614
readonly SCRIPT_PATH
1715

18-
readonly PYTHON_COMMAND="python${PYTHON_PACKAGE_VERSION}"
1916
readonly PYTHON_VENV_PATH="${SCRIPT_PATH}/compilesketches/.venv"
2017
readonly PYTHON_VENV_ACTIVATE_SCRIPT_PATH="${PYTHON_VENV_PATH}/bin/activate"
2118

22-
# Install Python
23-
sudo apt-get install --yes software-properties-common > /dev/null
24-
sudo add-apt-repository --yes ppa:deadsnakes/ppa > /dev/null
25-
sudo apt-get update --yes > /dev/null
26-
sudo apt-get install --yes python${PYTHON_PACKAGE_VERSION} > /dev/null
27-
echo "Using Python version: $("$PYTHON_COMMAND" --version)"
28-
29-
sudo apt-get install --yes python3-setuptools > /dev/null
30-
sudo apt-get install --yes python${PYTHON_PACKAGE_VERSION}-venv > /dev/null
31-
3219
# Create Python virtual environment
33-
"$PYTHON_COMMAND" -m venv --system-site-packages "$PYTHON_VENV_PATH"
20+
python -m venv --system-site-packages "$PYTHON_VENV_PATH"
3421

3522
# Activate Python virtual environment
3623
# shellcheck source=/dev/null
3724
. "$PYTHON_VENV_ACTIVATE_SCRIPT_PATH"
3825

3926
# Install Python dependencies
40-
"$PYTHON_COMMAND" -m pip install --upgrade pip > /dev/null
41-
"$PYTHON_COMMAND" -m pip install --quiet --requirement "${SCRIPT_PATH}/compilesketches/requirements.txt"
27+
python -m pip install --upgrade pip > /dev/null
28+
python -m pip install --quiet --requirement "${SCRIPT_PATH}/compilesketches/requirements.txt"
4229

4330
# Set outputs for use in GitHub Actions workflow steps
4431
# See: https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-output-parameter
45-
echo "::set-output name=python-command::$PYTHON_COMMAND"
4632
echo "::set-output name=python-venv-activate-script-path::$PYTHON_VENV_ACTIVATE_SCRIPT_PATH"

action.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ inputs:
4949
runs:
5050
using: composite
5151
steps:
52+
- name: Install Python
53+
uses: actions/setup-python@v4.5.0
54+
with:
55+
python-version-file: ${{ github.action_path }}/.python-version
56+
5257
- name: Run the set up script
5358
id: setup
5459
shell: bash
@@ -74,4 +79,4 @@ runs:
7479
INPUT_SKETCHES-REPORT-PATH: ${{ inputs.sketches-report-path }}
7580
run: |
7681
source "${{ steps.setup.outputs.python-venv-activate-script-path }}"
77-
"${{ steps.setup.outputs.python-command }}" "${{ github.action_path }}/compilesketches/compilesketches.py"
82+
python "${{ github.action_path }}/compilesketches/compilesketches.py"

0 commit comments

Comments
 (0)