Skip to content

Commit 65948b9

Browse files
committed
Less copy/pasting
1 parent 93f1d83 commit 65948b9

File tree

2 files changed

+103
-115
lines changed

2 files changed

+103
-115
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: "Restore venv and pre-commit from cache"
2+
description: "Restores the venv and pre-commit cache or fails"
3+
4+
runs:
5+
using: "composite"
6+
steps:
7+
- name: Create or reuse cache
8+
id: cache-create
9+
uses: actions/cache@v4
10+
with:
11+
path: |
12+
${{ inputs.venv-directory }}
13+
${{ inputs.precommit-home }}
14+
key: ${{ inputs.cache-key }}
15+
- name: Fail job if Python cache restore failed
16+
if: steps.cache-create.outputs.cache-hit != 'true'
17+
shell: bash
18+
run: |
19+
echo "Failed to restore cache for {{ inputs.python-version}} virtual environment from cache"
20+
exit 1
21+
22+
inputs:
23+
python-version:
24+
required: true
25+
cache-key:
26+
required: true
27+
venv-directory:
28+
required: true
29+
precommit-home:
30+
required: true

.github/workflows/verify.yml

Lines changed: 73 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
name: Latest commit
55

66
env:
7-
CACHE_VERSION: 1
7+
CACHE_VERSION: 2
88
DEFAULT_PYTHON: "3.13"
99
PRE_COMMIT_HOME: ~/.cache/pre-commit
10+
VENV: venv
1011

1112
on:
1213
schedule:
@@ -28,31 +29,25 @@ jobs:
2829
uses: actions/setup-python@v5
2930
with:
3031
python-version: ${{ env.DEFAULT_PYTHON }}
31-
- name: Ensure/restore Python ${{ env.DEFAULT_PYTHON }} virtual environment
32-
id: cache-venv
33-
uses: actions/cache@v4
32+
- name: Create or reuse cache
33+
id: cache-reuse
34+
uses: ./.github/actions/restore-venv
3435
with:
35-
path: venv
36-
key: ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt') }}
37-
- name: Create Python virtual environment (from cache)
38-
if: steps.cache-venv.outputs.cache-hit != 'true'
36+
python-version: ${{ steps.python.outputs.python-version }}
37+
cache-key: ${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt', '.pre-commit-config.yaml') }}
38+
venv-directory: ${{ env.VENV }}
39+
precommit-home: ${{ env.PRE_COMMIT_HOME }}
40+
- name: Create Python virtual environment
3941
run: |
4042
pip install virtualenv --upgrade
4143
python -m venv venv
4244
. venv/bin/activate
4345
pip install uv
4446
uv pip install -U pip setuptools wheel
45-
uv pip install -r requirements_test.txt -r requirements_commit.txt
46-
- name: Ensure/restore python and pre-commit environment (from cache)
47-
id: cache-precommit
48-
uses: actions/cache@v4
49-
with:
50-
path: |
51-
venv
52-
${{ env.PRE_COMMIT_HOME }}
53-
key: ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt', '.pre-commit-config.yaml') }}
47+
# 20220124 Mimic setup_test.sh
48+
uv pip install --upgrade -r requirements_commit.txt -r requirements_test.txt -c https://raw.githubusercontent.com/home-assistant/core/dev/homeassistant/package_constraints.txt -r https://raw.githubusercontent.com/home-assistant/core/dev/requirements_test.txt -r https://raw.githubusercontent.com/home-assistant/core/dev/requirements_test_pre_commit.txt
49+
uv pip install --upgrade pytest-asyncio
5450
- name: Install pre-commit dependencies
55-
if: steps.cache-precommit.outputs.cache-hit != 'true'
5651
run: |
5752
. venv/bin/activate
5853
pre-commit install-hooks
@@ -71,19 +66,14 @@ jobs:
7166
uses: actions/setup-python@v5
7267
with:
7368
python-version: ${{ env.DEFAULT_PYTHON }}
74-
- name: Ensure/restore pre-commit and Python ${{ env.DEFAULT_PYTHON }} virtual environment
75-
id: cache-venv
76-
uses: actions/cache@v4
69+
- name: Create or reuse cache
70+
id: cache-reuse
71+
uses: ./.github/actions/restore-venv
7772
with:
78-
path: |
79-
venv
80-
${{ env.PRE_COMMIT_HOME }}
81-
key: ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt', '.pre-commit-config.yaml') }}
82-
- name: Fail job if Python cache restore failed
83-
if: steps.cache-venv.outputs.cache-hit != 'true'
84-
run: |
85-
echo "Failed to restore pre-commit and Python ${{ env.DEFAULT_PYTHON }} virtual environment from cache"
86-
exit 1
73+
python-version: ${{ steps.python.outputs.python-version }}
74+
cache-key: ${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt', '.pre-commit-config.yaml') }}
75+
venv-directory: ${{ env.VENV }}
76+
precommit-home: ${{ env.PRE_COMMIT_HOME }}
8777
- name: Ruff (check)
8878
run: |
8979
. venv/bin/activate
@@ -116,19 +106,14 @@ jobs:
116106
uses: actions/setup-python@v5
117107
with:
118108
python-version: ${{ env.DEFAULT_PYTHON }}
119-
- name: Restore pre-commit and Python ${{ env.DEFAULT_PYTHON }} virtual environment
120-
id: cache-venv
121-
uses: actions/cache@v4
109+
- name: Create or reuse cache
110+
id: cache-reuse
111+
uses: ./.github/actions/restore-venv
122112
with:
123-
path: |
124-
venv
125-
${{ env.PRE_COMMIT_HOME }}
126-
key: ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt', '.pre-commit-config.yaml') }}
127-
- name: Fail job if Python cache restore failed
128-
if: steps.cache-venv.outputs.cache-hit != 'true'
129-
run: |
130-
echo "Failed to restore pre-commit and Python ${{ env.DEFAULT_PYTHON }} virtual environment from cache"
131-
exit 1
113+
python-version: ${{ steps.python.outputs.python-version }}
114+
cache-key: ${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt', '.pre-commit-config.yaml') }}
115+
venv-directory: ${{ env.VENV }}
116+
precommit-home: ${{ env.PRE_COMMIT_HOME }}
132117
- name: Verify commit
133118
run: |
134119
. venv/bin/activate
@@ -160,23 +145,19 @@ jobs:
160145
uses: actions/setup-python@v5
161146
with:
162147
python-version: ${{ matrix.python-version }}
163-
- name: Restore Python ${{ matrix.python-version }} virtual environment
164-
id: cache-venv
165-
uses: actions/cache@v4
148+
- name: Create or reuse cache
149+
id: cache-reuse
150+
uses: ./.github/actions/restore-venv
166151
with:
167-
path: venv
168-
key: ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt') }}
169-
- name: Create full Python ${{ matrix.python-version }} virtual environment
170-
if: steps.cache-venv.outputs.cache-hit != 'true'
152+
python-version: ${{ steps.python.outputs.python-version }}
153+
cache-key: ${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt', '.pre-commit-config.yaml') }}
154+
venv-directory: ${{ env.VENV }}
155+
precommit-home: ${{ env.PRE_COMMIT_HOME }}
156+
- name: Extend Python ${{ matrix.python-version }} virtual environment
171157
run: |
172158
python -m venv venv
173159
. venv/bin/activate
174160
pip install uv
175-
uv pip install -U pip setuptools wheel
176-
#pip install -r requirements_test.txt
177-
# 20220124 Mimic setup_test.sh
178-
uv pip install --upgrade -r requirements_test.txt -c https://raw.githubusercontent.com/home-assistant/core/dev/homeassistant/package_constraints.txt -r https://raw.githubusercontent.com/home-assistant/core/dev/requirements_test.txt -r https://raw.githubusercontent.com/home-assistant/core/dev/requirements_test_pre_commit.txt
179-
uv pip install --upgrade pytest-asyncio
180161
181162
pytest:
182163
runs-on: ubuntu-latest
@@ -196,17 +177,14 @@ jobs:
196177
uses: actions/setup-python@v5
197178
with:
198179
python-version: ${{ matrix.python-version }}
199-
- name: Restore full Python ${{ matrix.python-version }} virtual environment
200-
id: cache-venv
201-
uses: actions/cache@v4
180+
- name: Create or reuse cache
181+
id: cache-reuse
182+
uses: ./.github/actions/restore-venv
202183
with:
203-
path: venv
204-
key: ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt') }}
205-
- name: Fail job if Python cache restore failed
206-
if: steps.cache-venv.outputs.cache-hit != 'true'
207-
run: |
208-
echo "Failed to restore Python virtual environment from cache"
209-
exit 1
184+
python-version: ${{ steps.python.outputs.python-version }}
185+
cache-key: ${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt', '.pre-commit-config.yaml') }}
186+
venv-directory: ${{ env.VENV }}
187+
precommit-home: ${{ env.PRE_COMMIT_HOME }}
210188
- name: Run all tests
211189
run: |
212190
. venv/bin/activate
@@ -220,6 +198,7 @@ jobs:
220198
include-hidden-files: true
221199

222200
mypy:
201+
if: false # disables the job --> "Code is not up to par for mypy, skipping - see #171"
223202
runs-on: ubuntu-latest
224203
name: Run mypy
225204
needs:
@@ -235,27 +214,19 @@ jobs:
235214
uses: actions/setup-python@v5
236215
with:
237216
python-version: ${{ env.DEFAULT_PYTHON }}
238-
- name: Restore Python ${{ env.DEFAULT_PYTHON }} virtual environment
239-
id: cache-venv
240-
uses: actions/cache@v4
217+
- name: Create or reuse cache
218+
id: cache-reuse
219+
uses: ./.github/actions/restore-venv
241220
with:
242-
path: venv
243-
key: ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt') }}
244-
- name: Fail job if Python cache restore failed
245-
if: steps.cache-venv.outputs.cache-hit != 'true'
246-
run: |
247-
echo "Failed to restore Python ${{ env.DEFAULT_PYTHON }} virtual environment from cache"
248-
exit 1
221+
python-version: ${{ steps.python.outputs.python-version }}
222+
cache-key: ${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt', '.pre-commit-config.yaml') }}
223+
venv-directory: ${{ env.VENV }}
224+
precommit-home: ${{ env.PRE_COMMIT_HOME }}
249225
- name: Run mypy
250226
run: |
251227
. venv/bin/activate
252228
uv pip list | grep -i mypy
253-
#mypy plugwise_usb/
254-
echo "***"
255-
echo "***"
256-
echo "Code is not up to par for mypy, skipping - see #171"
257-
echo "***"
258-
echo "***"
229+
mypy plugwise_usb/
259230
260231
# Check shellscripts
261232
shellcheck:
@@ -291,17 +262,14 @@ jobs:
291262
uses: actions/setup-python@v5
292263
with:
293264
python-version: ${{ env.DEFAULT_PYTHON }}
294-
- name: Restore Python ${{ env.DEFAULT_PYTHON }} virtual environment
295-
id: cache-venv
296-
uses: actions/cache@v4
265+
- name: Create or reuse cache
266+
id: cache-reuse
267+
uses: ./.github/actions/restore-venv
297268
with:
298-
path: venv
299-
key: ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt') }}
300-
- name: Fail job if Python cache restore failed
301-
if: steps.cache-venv.outputs.cache-hit != 'true'
302-
run: |
303-
echo "Failed to restore Python virtual environment from cache"
304-
exit 1
269+
python-version: ${{ steps.python.outputs.python-version }}
270+
cache-key: ${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt', '.pre-commit-config.yaml') }}
271+
venv-directory: ${{ env.VENV }}
272+
precommit-home: ${{ env.PRE_COMMIT_HOME }}
305273
- name: Download all coverage artifacts
306274
uses: actions/download-artifact@v4
307275
- name: Combine coverage results
@@ -332,23 +300,16 @@ jobs:
332300
uses: actions/setup-python@v5
333301
with:
334302
python-version: ${{ env.DEFAULT_PYTHON }}
335-
- name: Restore Python ${{ env.DEFAULT_PYTHON }} virtual environment
336-
id: cache-venv
337-
uses: actions/cache@v4
303+
- name: Create or reuse cache
304+
id: cache-reuse
305+
uses: ./.github/actions/restore-venv
338306
with:
339-
path: venv
340-
key: ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt') }}
341-
- name: Fail job if Python cache restore failed
342-
if: steps.cache-venv.outputs.cache-hit != 'true'
343-
run: |
344-
echo "Failed to restore Python virtual environment from cache"
345-
exit 1
307+
python-version: ${{ steps.python.outputs.python-version }}
308+
cache-key: ${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt', '.pre-commit-config.yaml') }}
309+
venv-directory: ${{ env.VENV }}
310+
precommit-home: ${{ env.PRE_COMMIT_HOME }}
346311
- name: Install pypa/build
347-
run: >-
348-
python3 -m
349-
pip install
350-
build
351-
--user
312+
run: uv pip install build --user
352313
- name: Build a binary wheel and a source tarball
353314
run: python3 -m build
354315
- name: Publish distribution 📦 to Test PyPI
@@ -373,17 +334,14 @@ jobs:
373334
uses: actions/setup-python@v5
374335
with:
375336
python-version: ${{ env.DEFAULT_PYTHON }}
376-
- name: Restore Python ${{ env.DEFAULT_PYTHON }} virtual environment
377-
id: cache-venv
378-
uses: actions/cache@v4
337+
- name: Create or reuse cache
338+
id: cache-reuse
339+
uses: ./.github/actions/restore-venv
379340
with:
380-
path: venv
381-
key: ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt') }}
382-
- name: Fail job if Python cache restore failed
383-
if: steps.cache-venv.outputs.cache-hit != 'true'
384-
run: |
385-
echo "Failed to restore Python virtual environment from cache"
386-
exit 1
341+
python-version: ${{ steps.python.outputs.python-version }}
342+
cache-key: ${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt', '.pre-commit-config.yaml') }}
343+
venv-directory: ${{ env.VENV }}
344+
precommit-home: ${{ env.PRE_COMMIT_HOME }}
387345
- name: Run complexity report (click to view details)
388346
run: |
389347
. venv/bin/activate

0 commit comments

Comments
 (0)