Skip to content

Commit

Permalink
Change how CICD restores and saves the build cache, and cache pyright.
Browse files Browse the repository at this point in the history
* Always restore the build cache for non-`workflow_dispatch` events.
* Added input `use_build_cache` to control whether a `workflow_dispatch` event will restore the build cache.
* Always save the build cache.
* Reduced steps required when installing all packages through `install_all.sh`.
* Cause `pyright` to install its associated NPM module during package install, making `actions/cache/save` cache the NPM module.
* Remove the unused `matrix` strategy and use just an envvar for the Python version.

Related #33
  • Loading branch information
aholmes committed Jan 24, 2024
1 parent 29ca077 commit 6354faf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 46 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/CICD-scripts/workflow_dispatch.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"description": "Use this file when testing the workflow_dispatch action locally with nektos/act.",
"description": "Use this file when testing the workflow_dispatch action locally with nektos/act.",

"action": "workflow_dispatch",
"inputs": {
"rewrite_dependencies": "false"
}
"action": "workflow_dispatch",
"inputs": {
"rewrite_dependencies": "false",
"use_build_cache": "true"
}
}
71 changes: 41 additions & 30 deletions .github/workflows/CICD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,63 @@ on:
- false
default: "true"
required: true
use_build_cache:
description:
If "true", the workflow will use the build cache if the associated
cache key has a hit. If "false," any existing build cache will be ignored.
The workflow will save a new build cache with the same cache key regardless.
type: choice
options:
- true
- false
default: "true"
required: false

jobs:
Checkout:
name: Checkout and Setup
runs-on: ubuntu-latest

outputs:
repository-build-cache-key: ${{ steps.repository-build-cache-key.outputs.cache_key }}
build-cache-key: ${{ steps.build-cache-key.outputs.cache_key }}

strategy:
matrix:
python-version: ["3.10"]
env:
PYTHON_VERSION: "3.10"

steps:
- uses: actions/checkout@v4

- id: repository-build-cache-key
- id: build-cache-key
# Use the same cache between Workflow runs _except_ when the event is a workflow_dispatch.
# This should result in cache keys like "Linux-true-(true|false)-abc123" for all runs _except_ workflow_dispatch,
# and cache keys like "linux-false-(true|123)-abc123" for workflow_dispatch.
run: |
cache_key='${{ runner.os }}-${{ github.event_name != 'workflow_dispatch' || github.run_attempt }}-${{ hashFiles('pyproject.toml', 'src/*/pyproject.toml') }}'
cache_key='${{ runner.os }}-${{ hashFiles('pyproject.toml', 'src/*/pyproject.toml') }}'
echo "cache_key=$cache_key" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
name: Check repository build cache
id: repository-build-cache
- name: Restore build cache
if: ${{ success() && (github.event_name != 'workflow_dispatch' || inputs.use_build_cache == 'true') }}
uses: actions/cache/restore@v3
id: restore-build-cache
with:
path: ${{ github.workspace }}
# Use the same cache between Workflow runs _except_ when the event is a workflow_dispatch.
# This should result in cache keys like "Linux-true-abc123" for all runs _except_ workflow_dispatch,
# and cache keys like "linux-123-abc123" for workflow_dispatch.
key: ${{ steps.repository-build-cache-key.outputs.cache_key }}
key: ${{ steps.build-cache-key.outputs.cache_key }}

- name: Set up Python ${{ matrix.python-version }}
if: ${{ success() && steps.repository-build-cache.outputs.cache-hit != 'true' }}
- name: Set up Python ${{ env.PYTHON_VERSION }}
if: ${{ success() && steps.restore-build-cache.outputs.cache-hit != 'true' }}
uses: actions/setup-python@v4
id: install_python
with:
python-version: ${{ matrix.python-version }}
python-version: ${{ env.PYTHON_VERSION }}
cache: "pip"

- name: Create Venv
if: ${{ success() && steps.repository-build-cache.outputs.cache-hit != 'true' }}
if: ${{ success() && steps.restore-build-cache.outputs.cache-hit != 'true' }}
run: |
${{ steps.install_python.outputs.python-path }} -m venv .github-venv
- name: Install dependencies
if: ${{ success() && steps.repository-build-cache.outputs.cache-hit != 'true' }}
if: ${{ success() && steps.restore-build-cache.outputs.cache-hit != 'true' }}
run: |
echo Setting up dependencies
python -m pip install -U pip
Expand All @@ -75,29 +86,35 @@ jobs:
source .github-venv/bin/activate
./install_all.sh -e
- name: Save build cache
uses: actions/cache/save@v3
id: save-build-cache
with:
path: ${{ github.workspace }}
key: ${{ steps.build-cache-key.outputs.cache_key }}

Pyright:
name: Static type checking
runs-on: ubuntu-latest
needs:
- Checkout
if: ${{( always() && !cancelled() ) }}
strategy:
matrix:
python-version: ["3.10"]

steps:
- uses: actions/cache/restore@v3
name: Restore repository build
id: restore-repository-build
with:
key: ${{ needs.Checkout.outputs.repository-build-cache-key }}
key: ${{ needs.Checkout.outputs.build-cache-key }}
path: ${{ github.workspace }}
fail-on-cache-miss: true

- name: Test with pyright
run: |
echo Running pyright
source .github-venv/bin/activate
# Do not specify `PYRIGHT_PYTHON_FORCE_VERSION=latest` here because
# install_all.sh does this, and the package is then cached.
pyright
Pytest:
Expand All @@ -106,16 +123,13 @@ jobs:
needs:
- Checkout
if: ${{( always() && !cancelled() ) }}
strategy:
matrix:
python-version: ["3.10"]

steps:
- uses: actions/cache/restore@v3
name: Restore repository build
id: restore-repository-build
with:
key: ${{ needs.Checkout.outputs.repository-build-cache-key }}
key: ${{ needs.Checkout.outputs.build-cache-key }}
path: ${{ github.workspace }}
fail-on-cache-miss: true

Expand Down Expand Up @@ -146,16 +160,13 @@ jobs:
needs:
- Checkout
if: ${{( always() && !cancelled() ) }}
strategy:
matrix:
python-version: ["3.10"]

steps:
- uses: actions/cache/restore@v3
name: Restore repository build
id: restore-repository-build
with:
key: ${{ needs.Checkout.outputs.repository-build-cache-key }}
key: ${{ needs.Checkout.outputs.build-cache-key }}
path: ${{ github.workspace }}
fail-on-cache-miss: true

Expand Down
15 changes: 4 additions & 11 deletions install_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@

# use $1 to pass in -e (or any other options)

python -m pip install $1 .
python -m pip install $1 .[dev-dependencies]

python -m pip install $1 \
.[dev-dependencies] \
src/AWS \
src/database \
src/development \
src/GitHub \
src/platform \
src/programming \
src/testing \
src/web
# Causes pyright to install the associated NPM module.
# Use latest because pyproject.toml does not specify a version.
PYRIGHT_PYTHON_FORCE_VERSION=latest pyright --version

0 comments on commit 6354faf

Please sign in to comment.