Skip to content

Commit 7129ac1

Browse files
committed
Cache pipx in CI and add custom setup action
1 parent 5d08a4c commit 7129ac1

File tree

2 files changed

+97
-52
lines changed

2 files changed

+97
-52
lines changed

.github/actions/setup/action.yaml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
name: Setup Environment
3+
description: Install requested pipx dependencies, configure the system python, and install poetry and the package dependencies
4+
5+
inputs:
6+
poetry-install-options:
7+
default: ""
8+
poetry-version:
9+
default: 1.8.2
10+
python-version:
11+
required: true
12+
cache-pre-commit:
13+
default: false
14+
15+
runs:
16+
using: composite
17+
steps:
18+
- uses: "actions/setup-python@v5"
19+
id: setup-python
20+
with:
21+
python-version: "${{ inputs.python-version }}"
22+
23+
- name: Setup pipx environment Variables
24+
id: pipx-env-setup
25+
# pipx default home and bin dir are not writable by the cache action
26+
# so override them here and add the bin dir to PATH for later steps.
27+
# This also ensures the pipx cache only contains poetry
28+
run: |
29+
SEP="${{ !startsWith(runner.os, 'windows') && '/' || '\\' }}"
30+
PIPX_CACHE="${{ github.workspace }}${SEP}pipx_cache"
31+
echo "pipx-cache-path=${PIPX_CACHE}" >> $GITHUB_OUTPUT
32+
echo "pipx-version=$(pipx --version)" >> $GITHUB_OUTPUT
33+
echo "PIPX_HOME=${PIPX_CACHE}${SEP}home" >> $GITHUB_ENV
34+
echo "PIPX_BIN_DIR=${PIPX_CACHE}${SEP}bin" >> $GITHUB_ENV
35+
echo "PIPX_MAN_DIR=${PIPX_CACHE}${SEP}man" >> $GITHUB_ENV
36+
echo "${PIPX_CACHE}${SEP}bin" >> $GITHUB_PATH
37+
shell: bash
38+
39+
- name: Pipx cache
40+
id: pipx-cache
41+
uses: actions/cache@v4
42+
with:
43+
path: ${{ steps.pipx-env-setup.outputs.pipx-cache-path }}
44+
key: ${{ runner.os }}-python-${{ steps.setup-python.outputs.python-version }}-pipx-${{ steps.pipx-env-setup.outputs.pipx-version }}-poetry-${{ inputs.poetry-version }}
45+
46+
- name: Install poetry
47+
if: steps.pipx-cache.outputs.cache-hit != 'true'
48+
id: install-poetry
49+
shell: bash
50+
run: |-
51+
pipx install poetry==${{ inputs.poetry-version }} --python "${{ steps.setup-python.outputs.python-path }}"
52+
53+
- name: Read poetry cache location
54+
id: poetry-cache-location
55+
shell: bash
56+
run: |-
57+
echo "poetry-venv-location=$(poetry config virtualenvs.path)" >> $GITHUB_OUTPUT
58+
59+
- uses: actions/cache@v4
60+
name: Poetry cache
61+
with:
62+
path: |
63+
${{ steps.poetry-cache-location.outputs.poetry-venv-location }}
64+
key: ${{ runner.os }}-python-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('poetry.lock') }}-options-${{ inputs.poetry-install-options }}
65+
66+
- name: "Poetry install"
67+
shell: bash
68+
run: |
69+
poetry install ${{ inputs.poetry-install-options }}
70+
71+
- name: Read pre-commit version
72+
if: inputs.cache-pre-commit == 'true'
73+
id: pre-commit-version
74+
shell: bash
75+
run: >-
76+
echo "pre-commit-version=$(poetry run pre-commit -V | awk '{print $2}')" >> $GITHUB_OUTPUT
77+
78+
- uses: actions/cache@v4
79+
if: inputs.cache-pre-commit == 'true'
80+
name: Pre-commit cache
81+
with:
82+
path: ~/.cache/pre-commit/
83+
key: ${{ runner.os }}-pre-commit-${{ steps.pre-commit-version.outputs.pre-commit-version }}-python-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('.pre-commit-config.yaml') }}

.github/workflows/ci.yml

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ on:
77
branches: ["master"]
88
workflow_dispatch: # to allow manual re-runs
99

10+
env:
11+
POETRY_VERSION: 1.8.2
1012

1113
jobs:
1214
linting:
@@ -19,35 +21,13 @@ jobs:
1921

2022
steps:
2123
- uses: "actions/checkout@v4"
22-
- uses: "actions/setup-python@v5"
23-
id: setup-python
24+
- name: Setup environment
25+
uses: ./.github/actions/setup
2426
with:
25-
python-version: "${{ matrix.python-version }}"
26-
- name: Install poetry
27-
run: pipx install poetry --python "${{ steps.setup-python.outputs.python-path }}"
28-
- name: Read poetry cache location
29-
id: poetry-cache-location
30-
shell: bash
31-
run: |
32-
echo "POETRY_VENV_LOCATION=$(poetry config virtualenvs.path)" >> $GITHUB_OUTPUT
33-
- uses: actions/cache@v3
34-
name: Poetry cache
35-
with:
36-
path: |
37-
${{ steps.poetry-cache-location.outputs.POETRY_VENV_LOCATION }}
38-
key: ${{ runner.os }}-python-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('poetry.lock') }}-extras-false
39-
- name: "Install dependencies"
40-
run: |
41-
poetry install
42-
- name: Read pre-commit version
43-
id: pre-commit-version
44-
run: >-
45-
echo "PRE_COMMIT_VERSION=$(poetry run pre-commit -V | awk '{print $2}')" >> $GITHUB_OUTPUT
46-
- uses: actions/cache@v3
47-
name: Pre-commit cache
48-
with:
49-
path: ~/.cache/pre-commit/
50-
key: ${{ runner.os }}-pre-commit-${{ steps.pre-commit-version.outputs.PRE_COMMIT_VERSION }}-python-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('.pre-commit-config.yaml') }}
27+
os: ${{ matrix.os }}
28+
python-version: ${{ matrix.python-version }}
29+
cache-pre-commit: true
30+
poetry-version: ${{ env.POETRY_VERSION }}
5131
- name: "Check supported device md files are up to date"
5232
run: |
5333
poetry run pre-commit run generate-supported --all-files
@@ -108,31 +88,13 @@ jobs:
10888

10989
steps:
11090
- uses: "actions/checkout@v4"
111-
- uses: "actions/setup-python@v5"
112-
id: setup-python
91+
- name: Setup environment
92+
uses: ./.github/actions/setup
11393
with:
114-
python-version: "${{ matrix.python-version }}"
115-
- name: Install poetry
116-
run: pipx install poetry --python "${{ steps.setup-python.outputs.python-path }}"
117-
- name: Read poetry cache location
118-
id: poetry-cache-location
119-
shell: bash
120-
run: |
121-
echo "POETRY_VENV_LOCATION=$(poetry config virtualenvs.path)" >> $GITHUB_OUTPUT
122-
- uses: actions/cache@v3
123-
name: Poetry cache
124-
with:
125-
path: |
126-
${{ steps.poetry-cache-location.outputs.POETRY_VENV_LOCATION }}
127-
key: ${{ runner.os }}-python-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('poetry.lock') }}-extras-${{ matrix.extras }}
128-
- name: "Install dependencies (no extras)"
129-
if: matrix.extras == false
130-
run: |
131-
poetry install
132-
- name: "Install dependencies (with extras)"
133-
if: matrix.extras == true
134-
run: |
135-
poetry install --all-extras
94+
os: ${{ matrix.os }}
95+
python-version: ${{ matrix.python-version }}
96+
poetry-version: ${{ env.POETRY_VERSION }}
97+
poetry-install-options: ${{ matrix.extras == true && '--all-extras' || '' }}
13698
- name: "Run tests (no coverage)"
13799
if: ${{ startsWith(matrix.python-version, 'pypy') }}
138100
run: |

0 commit comments

Comments
 (0)