From 0166957d31b9e930bbab6a46a099cb3a59ecb258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20M=2E=20K=C3=B6hler?= Date: Thu, 15 Feb 2024 15:03:33 +0100 Subject: [PATCH] Update workflows & CI --- .github/actions/setup-venv/action.yml | 58 ++++++++++ .github/workflows/main.yml | 161 ++++++++++++++++++++++++++ .github/workflows/tests.yml | 52 --------- tox.ini | 6 +- 4 files changed, 222 insertions(+), 55 deletions(-) create mode 100644 .github/actions/setup-venv/action.yml create mode 100644 .github/workflows/main.yml delete mode 100644 .github/workflows/tests.yml diff --git a/.github/actions/setup-venv/action.yml b/.github/actions/setup-venv/action.yml new file mode 100644 index 0000000..2253ebe --- /dev/null +++ b/.github/actions/setup-venv/action.yml @@ -0,0 +1,58 @@ +name: Python virtualenv +description: Set up a Python virtual environment with caching +inputs: + python-version: + description: The Python version to use + required: true + cache-prefix: + description: Update this to invalidate the cache + required: true + default: v0 +runs: + using: composite + steps: + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: ${{ inputs.python-version }} + + - shell: bash + run: | + # Install prerequisites. + pip install --upgrade pip setuptools wheel virtualenv + + - shell: bash + run: | + # Get the exact Python version to use in the cache key. + echo "PYTHON_VERSION=$(python --version)" >> $GITHUB_ENV + + - uses: actions/cache@v2 + id: virtualenv-cache + with: + path: .venv + key: + ${{ inputs.cache-prefix }}-${{ runner.os }}-${{ env.PYTHON_VERSION + }}-${{ hashFiles('pyproject.toml') }} + + - if: steps.virtualenv-cache.outputs.cache-hit != 'true' + shell: bash + run: | + # Set up virtual environment without cache hit. + test -d .venv || virtualenv -p $(which python) --copies --reset-app-data .venv + . .venv/bin/activate + pip install -e .[dev] + + - if: steps.virtualenv-cache.outputs.cache-hit == 'true' + shell: bash + run: | + # Set up virtual environment from cache hit. + . .venv/bin/activate + pip install --no-deps -e .[dev] + + - shell: bash + run: | + # Show environment info. + . .venv/bin/activate + echo "✓ Installed $(python --version) virtual environment to $(which python)" + echo "Packages:" + pip freeze diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..481d6ce --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,161 @@ +name: Main + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +on: + pull_request: + branches: + - main + push: + branches: + - main + tags: + - "v*.*.*" + +env: + # Change this to invalidate existing cache. + CACHE_PREFIX: v0 + PYTHONPATH: ./ + +jobs: + checks: + name: Python ${{ matrix.python }} - ${{ matrix.task.name }} + timeout-minutes: 15 + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest] + python: ["3.12", "3.11", "3.10"] + task: + - name: Test + os: [windows-latest, ubuntu-latest, macos-latest] + run: | + pytest -v --color=yes tests/ + - name: Build + os: [windows-latest, ubuntu-latest, macos-latest] + run: python -m build + + include: + # - os: [windows-latest, ubuntu-latest, macos-latest] + # task: + # name: Build + # run: | + # python -m build + + - python: "3.12" + task: + name: pre-commit + run: | + pip install pre-commit + pre-commit run --all-files + + - python: "3.12" + task: + name: Lint + run: ruff check . + + - python: "3.12" + task: + name: Type check + run: mypy . + + - python: "3.12" + task: + name: Style + run: black --check . + + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + + - name: Setup Python environment + uses: ./.github/actions/setup-venv + with: + python-version: ${{ matrix.python }} + cache-prefix: ${{ env.CACHE_PREFIX }} + + - name: Restore mypy cache + if: matrix.task.name == 'Type check' + uses: actions/cache@v4 + with: + path: .mypy_cache + key: + mypy-${{ env.CACHE_PREFIX }}-${{ runner.os }}-${{ matrix.python + }}-${{ hashFiles('*requirements.txt') }}-${{ github.ref }}-${{ + github.sha }} + restore-keys: | + mypy-${{ env.CACHE_PREFIX }}-${{ runner.os }}-${{ matrix.python }}-${{ hashFiles('*requirements.txt') }}-${{ github.ref }} + mypy-${{ env.CACHE_PREFIX }}-${{ runner.os }}-${{ matrix.python }}-${{ hashFiles('*requirements.txt') }} + + - name: ${{ matrix.task.name }} + run: | + . .venv/bin/activate + ${{ matrix.task.run }} + + - name: Upload package distribution files + if: matrix.task.name == 'Build' + uses: actions/upload-artifact@v4 + with: + name: package + path: dist + + - name: Clean up + if: always() + run: | + . .venv/bin/activate + pip uninstall -y pte-stats + + release: + name: Release + if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes + runs-on: ubuntu-latest + needs: [checks] + environment: + name: pypi + url: https://pypi.org/p/ # Replace with your PyPI project name + permissions: + id-token: write # IMPORTANT: mandatory for trusted publishing + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Install requirements + run: | + pip install --upgrade pip + pip install -e .[dev] + + - name: Prepare environment + run: | + echo "RELEASE_VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV + echo "TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + + - name: Download package distribution files + uses: actions/download-artifact@v4 + with: + name: package + path: dist + + - name: Generate release notes + run: | + python scripts/release_notes.py > ${{ github.workspace }}-RELEASE_NOTES.md + + - name: Publish distribution 📦 to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 + + - name: Publish GitHub release + uses: softprops/action-gh-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + body_path: ${{ github.workspace }}-RELEASE_NOTES.md + prerelease: ${{ contains(env.TAG, 'rc') }} + files: | + dist/* diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml deleted file mode 100644 index d4ba6fc..0000000 --- a/.github/workflows/tests.yml +++ /dev/null @@ -1,52 +0,0 @@ -name: Tests -on: - push: - branches: - - "*" - pull_request: - branches: - - "*" -jobs: - tests: - name: ${{ matrix.name }} - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - include: - # - { name: 3.12 Linux, python: "3.12", os: ubuntu-latest, tox: py312 } - # - { name: 3.11 Linux, python: "3.11", os: ubuntu-latest, tox: py311 } - # - { name: 3.10 Linux, python: "3.10", os: ubuntu-latest, tox: py310 } - # - { - # name: 3.12 Windows, - # python: "3.12", - # os: windows-latest, - # tox: py312, - # } - # - { name: 3.12 Mac, python: "3.12", os: macos-latest, tox: py312 } - - { - name: Lint with pre-commit, - python: "3.12", - os: ubuntu-latest, - tox: lint, - } - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python }} - - name: update pip - run: | - python -m pip install -U pip - - name: get pip cache dir - id: pip-cache - run: echo "::set-output name=dir::$(pip cache dir)" - - name: cache pip - uses: actions/cache@v4 - with: - path: ${{ steps.pip-cache.outputs.dir }} - key: - pip|${{ runner.os }}|${{ matrix.python }}|${{ - hashFiles('setup.py')}} - - run: pip install tox - - run: tox -e ${{ matrix.tox }} diff --git a/tox.ini b/tox.ini index 0fe4a14..210b426 100644 --- a/tox.ini +++ b/tox.ini @@ -1,14 +1,14 @@ [tox] envlist = py3{12,11,10} - lint + pre-commit skip_missing_interpreters = true isolated_build = true [testenv] commands = pytest -v --tb=short --basetemp={envtmpdir} {posargs:tests} -[testenv:{lint}] +[testenv:{pre-commit}] commands = install_pre-commit: pip install pre-commit - run_pre-commit: pre-commit run --show-diff-on-failure --all-files + run_pre-commit: pre-commit run --all-files