From f79c8c0fc2d0379183ef544bdbdfc56599f8d649 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Thu, 15 Feb 2024 13:05:46 -0800 Subject: [PATCH] Use caching in Github Action workflows. --- .github/actions/setup-python-env/action.yml | 29 ++++++++++++++++++++- .github/workflows/ci-pre-commit.yaml | 1 + .github/workflows/ci-pytest.yaml | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/.github/actions/setup-python-env/action.yml b/.github/actions/setup-python-env/action.yml index 9564d433..5555b410 100644 --- a/.github/actions/setup-python-env/action.yml +++ b/.github/actions/setup-python-env/action.yml @@ -4,15 +4,42 @@ inputs: python-version: description: Python version number to install required: true + additional-cache-key: + description: | + An additional cache key that can be specified to differentiate workflows that install a different set of packages. + required: false + default: "default" runs: using: "composite" steps: + - name: Set Linux Release Environment Variable + shell: bash + run: echo "LINUX_RELEASE=$(lsb_release -rs)" >> $GITHUB_ENV - name: Set up Python ${{ inputs.python-version }} uses: actions/setup-python@v4 with: python-version: ${{ inputs.python-version }} - + - uses: actions/cache@v3 + with: + # Save pip cache. + # Save Hatch environments. + # Save the package cache for Hatch. + # Save pre-commit environments. + path: | + ${{ env.pythonLocation }} + ~/.cache/pip + ~/.local/share/hatch + ~/.cache/hatch + ~/.cache/pre-commit + # >- means combine all lines to a single line + # The cache key can be any string. The format used here is just for readability. + key: >- + python_location: "${{ env.pythonLocation }}" + AND pyproject_hash: "${{ hashFiles('pyproject.toml') }}" + AND precommit_config_hash: "${{ hashFiles('.pre-commit-config.yaml') }}" + AND linux_release: "${{ env.LINUX_RELEASE }}" + AND additional_cache_key: "${{ inputs.additional-cache-key }}" - name: Install Hatch shell: bash run: pip3 install hatch diff --git a/.github/workflows/ci-pre-commit.yaml b/.github/workflows/ci-pre-commit.yaml index 21313eb8..cb5f655b 100644 --- a/.github/workflows/ci-pre-commit.yaml +++ b/.github/workflows/ci-pre-commit.yaml @@ -22,6 +22,7 @@ jobs: uses: ./.github/actions/setup-python-env with: python-version: ${{ matrix.python-version }} + additional-cache-key: "pydantic~=1.10" # This step is necessary so long as we're allowing Pydantic 1 and Pydantic 2 via shimming - name: Force Pydantic 1 run: hatch run dev-env:pip install "pydantic~=1.10" diff --git a/.github/workflows/ci-pytest.yaml b/.github/workflows/ci-pytest.yaml index 658df1b3..26384194 100644 --- a/.github/workflows/ci-pytest.yaml +++ b/.github/workflows/ci-pytest.yaml @@ -24,6 +24,7 @@ jobs: uses: ./.github/actions/setup-python-env with: python-version: ${{ matrix.python-version }} + additional-cache-key: "pydantic~=${{ matrix.pydantic-version }}" - name: Set pydantic Version ~= ${{ matrix.pydantic-version }} run: hatch run dev-env:pip install "pydantic~=${{ matrix.pydantic-version }}" - name: Run Python Tests