Merge pull request #760 from plugwise/alignci #5608
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions | |
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions | |
name: Latest commit | |
env: | |
CACHE_VERSION: 14 | |
DEFAULT_PYTHON: "3.13" | |
PRE_COMMIT_HOME: ~/.cache/pre-commit | |
VENV: venv | |
on: | |
schedule: | |
- cron: "2 4 * * 0" # weekly | |
workflow_dispatch: | |
push: | |
# pull_request: | |
jobs: | |
# Determine cache key once | |
cache: | |
runs-on: ubuntu-latest | |
name: Cache identify | |
outputs: | |
cache-key: ${{ steps.set-key.outputs.cache-key }} | |
steps: | |
- name: Check out committed code | |
uses: actions/checkout@v4 | |
- name: Set up Python ${{ env.DEFAULT_PYTHON }} | |
id: python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: ${{ env.DEFAULT_PYTHON }} | |
- name: Fetch HA pyproject | |
id: core-version | |
run: wget -O ha_pyproject.toml "https://raw.githubusercontent.com/home-assistant/core/refs/heads/dev/pyproject.toml" | |
- name: Compute cache key | |
id: set-key | |
run: echo "cache-key=${{ runner.os }}-venv-cache-${{ env.CACHE_VERSION }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt', '.pre-commit-config.yaml') }}" >> "$GITHUB_OUTPUT" | |
# Prepare default python version environment | |
prepare: | |
runs-on: ubuntu-latest | |
needs: cache | |
name: Prepare | |
steps: | |
- name: Prepare code checkout and python/pre-commit setup | |
id: cache-reuse | |
uses: plugwise/gh-actions/prepare-python-and-code@v1 | |
with: | |
cache-key: ${{ needs.cache.outputs.cache-key }} | |
fail-on-miss: false # First time create cache (if not already exists) | |
python-version: ${{ env.DEFAULT_PYTHON }} | |
venv-dir: ${{ env.VENV }} | |
precommit-home: ${{ env.PRE_COMMIT_HOME }} | |
ruff: | |
runs-on: ubuntu-latest | |
name: Ruff check and force | |
needs: | |
- cache | |
- prepare | |
steps: | |
- name: Check out committed code | |
uses: actions/checkout@v4 | |
with: | |
persist-credentials: false | |
- name: Restore cached environment | |
id: cache-reuse | |
uses: plugwise/gh-actions/restore-venv@v1 | |
with: | |
cache-key: ${{ needs.cache.outputs.cache-key }} | |
python-version: ${{ env.DEFAULT_PYTHON }} | |
venv-dir: ${{ env.VENV }} | |
precommit-home: ${{ env.PRE_COMMIT_HOME }} | |
- name: Ruff (with fix) | |
run: | | |
. venv/bin/activate | |
ruff check plugwise/ tests/ | |
- name: If needed, commit ruff changes to the pull request | |
if: failure() | |
run: | | |
. venv/bin/activate | |
ruff format plugwise/ tests/ | |
git config --global user.name 'autoruff' | |
git config --global user.email 'plugwise@users.noreply.github.com' | |
git remote set-url origin https://x-access-token:${{ secrets.PAT_CT }}@github.com/$GITHUB_REPOSITORY | |
git checkout $GITHUB_HEAD_REF | |
git commit -am "fixup: ${GITHUB_REF##*/} Python code fixed using ruff" | |
git push origin ${GITHUB_REF##*/} | |
commitcheck: | |
runs-on: ubuntu-latest | |
name: Check commit | |
needs: | |
- cache | |
- prepare | |
- ruff | |
- shellcheck | |
- dependencies_check | |
steps: | |
- name: Check out committed code | |
uses: actions/checkout@v4 | |
- name: Restore cached environment | |
id: cache-reuse | |
uses: plugwise/gh-actions/restore-venv@v1 | |
with: | |
cache-key: ${{ needs.cache.outputs.cache-key }} | |
python-version: ${{ env.DEFAULT_PYTHON }} | |
venv-dir: ${{ env.VENV }} | |
precommit-home: ${{ env.PRE_COMMIT_HOME }} | |
- name: Verify commit | |
run: | | |
. venv/bin/activate | |
pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage manual pylint | |
- name: Biome lint | |
run: | | |
. venv/bin/activate | |
mkdir -p ./tmp && curl -sL "https://github.com/biomejs/biome/releases/latest/download/biome-linux-x64" -o ./tmp/biome && chmod +x ./tmp/biome | |
pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage manual biome | |
- name: Lint markdown files | |
run: | | |
. venv/bin/activate | |
pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage manual markdownlint | |
pytest: | |
runs-on: ubuntu-latest | |
name: Run pytest using Python ${{ matrix.python-version }} | |
needs: | |
- cache | |
- prepare | |
- commitcheck | |
strategy: | |
matrix: | |
python-version: ["3.13"] | |
steps: | |
- name: Check out committed code | |
uses: actions/checkout@v4 | |
- name: Restore cached environment | |
id: cache-reuse | |
uses: plugwise/gh-actions/restore-venv@v1 | |
with: | |
cache-key: ${{ needs.cache.outputs.cache-key }} | |
python-version: ${{ env.DEFAULT_PYTHON }} | |
venv-dir: ${{ env.VENV }} | |
precommit-home: ${{ env.PRE_COMMIT_HOME }} | |
- name: Run all tests | |
run: | | |
. venv/bin/activate | |
pytest --log-level info tests/*.py --cov='.' | |
- name: Upload coverage artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: coverage-${{ matrix.python-version }} | |
path: .coverage | |
if-no-files-found: error | |
include-hidden-files: true | |
mypy: | |
runs-on: ubuntu-latest | |
name: Run mypy | |
needs: | |
- cache | |
- prepare | |
- pytest | |
steps: | |
- name: Check out committed code | |
uses: actions/checkout@v4 | |
with: | |
persist-credentials: false | |
- name: Restore cached environment | |
id: cache-reuse | |
uses: plugwise/gh-actions/restore-venv@v1 | |
with: | |
cache-key: ${{ needs.cache.outputs.cache-key }} | |
python-version: ${{ env.DEFAULT_PYTHON }} | |
venv-dir: ${{ env.VENV }} | |
precommit-home: ${{ env.PRE_COMMIT_HOME }} | |
- name: Run mypy | |
run: | | |
. venv/bin/activate | |
pip list | grep -i mypy | |
mypy plugwise/ | |
# Check shellscripts | |
shellcheck: | |
name: Shellcheck | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out committed code | |
uses: actions/checkout@v4 | |
- name: Run ShellCheck | |
uses: ludeeus/action-shellcheck@master | |
# Check for missing python dependencies | |
dependencies_check: | |
runs-on: ubuntu-latest | |
name: Dependency | |
steps: | |
- name: Check out committed code | |
uses: actions/checkout@v4 | |
- name: Run dependency checker | |
run: scripts/dependencies_check.sh debug | |
coverage: | |
name: Process test coverage | |
runs-on: ubuntu-latest | |
needs: | |
- cache | |
- prepare | |
- pytest | |
- mypy | |
steps: | |
- name: Check out committed code | |
uses: actions/checkout@v4 | |
- name: Restore cached environment | |
id: cache-reuse | |
uses: plugwise/gh-actions/restore-venv@v1 | |
with: | |
cache-key: ${{ needs.cache.outputs.cache-key }} | |
python-version: ${{ env.DEFAULT_PYTHON }} | |
venv-dir: ${{ env.VENV }} | |
precommit-home: ${{ env.PRE_COMMIT_HOME }} | |
- name: Download all coverage artifacts | |
uses: actions/download-artifact@v4 | |
- name: Combine coverage results | |
run: | | |
. venv/bin/activate | |
coverage combine coverage*/.coverage* | |
coverage report --fail-under=94 | |
coverage xml | |
- name: Upload coverage to Codecov | |
uses: codecov/codecov-action@v5 | |
env: | |
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} | |
test-publishing: | |
name: Build and publish Python 🐍 distributions 📦 to TestPyPI | |
runs-on: ubuntu-latest | |
needs: | |
- cache | |
- prepare | |
- coverage | |
- mypy | |
steps: | |
- name: Check out committed code | |
uses: actions/checkout@v4 | |
- name: Restore cached environment | |
id: cache-reuse | |
uses: plugwise/gh-actions/restore-venv@v1 | |
with: | |
cache-key: ${{ needs.cache.outputs.cache-key }} | |
python-version: ${{ env.DEFAULT_PYTHON }} | |
venv-dir: ${{ env.VENV }} | |
precommit-home: ${{ env.PRE_COMMIT_HOME }} | |
- name: Install pypa/build | |
run: | | |
. venv/bin/activate | |
uv pip install build | |
- name: Build a binary wheel and a source tarball | |
run: | | |
. venv/bin/activate | |
python3 -m build | |
- name: Publish distribution 📦 to Test PyPI | |
uses: pypa/gh-action-pypi-publish@release/v1 | |
continue-on-error: true | |
with: | |
password: ${{ secrets.testpypi_token }} | |
repository-url: https://test.pypi.org/legacy/ | |
skip-existing: true | |
complexity: | |
name: Process test complexity | |
runs-on: ubuntu-latest | |
needs: | |
- cache | |
- prepare | |
- coverage | |
steps: | |
- name: Check out committed code | |
uses: actions/checkout@v4 | |
- name: Restore cached environment | |
id: cache-reuse | |
uses: plugwise/gh-actions/restore-venv@v1 | |
with: | |
cache-key: ${{ needs.cache.outputs.cache-key }} | |
python-version: ${{ env.DEFAULT_PYTHON }} | |
venv-dir: ${{ env.VENV }} | |
precommit-home: ${{ env.PRE_COMMIT_HOME }} | |
- name: Run complexity report (click to view details) | |
run: | | |
. venv/bin/activate | |
echo "Showing complexity higher or equal to 'C'" | |
radon cc plugwise/ tests/ -s -nc --no-assert |