Cache poetry install + unify Python/Poetry setup for lint and test jobs. (#9625)

With this PR:
- All lint and test jobs use the exact same Python + Poetry installation
approach, instead of lints doing it one way and tests doing it another
way.
- The Poetry installation itself is cached, which saves ~15s per run.
- We no longer pass shell commands as workflow arguments to a workflow
that just runs them in a shell. This makes our actions more resilient to
shell code injection.

If y'all like this approach, I can modify the scheduled tests workflow
and the release workflow to use this too.
This commit is contained in:
Predrag Gruevski 2023-08-22 15:59:22 -04:00 committed by GitHub
parent 81163e3c0c
commit 83986ea98a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 69 deletions

View File

@ -15,19 +15,13 @@ inputs:
description: Poetry version description: Poetry version
required: true required: true
install-command:
description: Command run for installing dependencies
required: false
default: poetry install
cache-key: cache-key:
description: Cache key to use for manual handling of caching description: Cache key to use for manual handling of caching
required: true required: true
working-directory: working-directory:
description: Directory to run install-command in description: Directory whose poetry.lock file should be cached
required: false required: true
default: ""
runs: runs:
using: composite using: composite
@ -38,47 +32,35 @@ runs:
python-version: ${{ inputs.python-version }} python-version: ${{ inputs.python-version }}
- uses: actions/cache@v3 - uses: actions/cache@v3
id: cache-pip id: cache-bin-poetry
name: Cache Pip ${{ inputs.python-version }} name: Cache Poetry binary - Python ${{ inputs.python-version }}
env: env:
SEGMENT_DOWNLOAD_TIMEOUT_MIN: "4" SEGMENT_DOWNLOAD_TIMEOUT_MIN: "1"
with: with:
path: | path: |
~/.cache/pip /opt/pipx/venvs/poetry
key: pip-${{ runner.os }}-${{ runner.arch }}-py-${{ inputs.python-version }} /opt/pipx_bin/poetry
# This step caches the poetry installation, so make sure it's keyed on the poetry version as well.
key: bin-poetry-${{ runner.os }}-${{ runner.arch }}-py-${{ inputs.python-version }}-${{ inputs.poetry-version }}
- name: Install poetry - name: Install poetry
if: steps.cache-bin-poetry.outputs.cache-hit != 'true'
shell: bash shell: bash
env: env:
POETRY_VERSION: ${{ inputs.poetry-version }} POETRY_VERSION: ${{ inputs.poetry-version }}
PYTHON_VERSION: ${{ inputs.python-version }} PYTHON_VERSION: ${{ inputs.python-version }}
run: pipx install "poetry==$POETRY_VERSION" --python "python$PYTHON_VERSION" --verbose run: pipx install "poetry==$POETRY_VERSION" --python "python$PYTHON_VERSION" --verbose
- name: Check Poetry File - name: Restore pip and poetry cached dependencies
shell: bash uses: actions/cache@v3
working-directory: ${{ inputs.working-directory }}
run: |
poetry check
- name: Check lock file
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
poetry lock --check
- uses: actions/cache@v3
id: cache-poetry
env: env:
SEGMENT_DOWNLOAD_TIMEOUT_MIN: "4" SEGMENT_DOWNLOAD_TIMEOUT_MIN: "4"
WORKDIR: ${{ inputs.working-directory == '' && '.' || inputs.working-directory }} WORKDIR: ${{ inputs.working-directory == '' && '.' || inputs.working-directory }}
with: with:
path: | path: |
~/.cache/pip
~/.cache/pypoetry/virtualenvs ~/.cache/pypoetry/virtualenvs
~/.cache/pypoetry/cache ~/.cache/pypoetry/cache
~/.cache/pypoetry/artifacts ~/.cache/pypoetry/artifacts
${{ env.WORKDIR }}/.venv ${{ env.WORKDIR }}/.venv
key: poetry-${{ runner.os }}-${{ runner.arch }}-py-${{ inputs.python-version }}-poetry-${{ inputs.poetry-version }}-${{ inputs.cache-key }}-${{ hashFiles(format('{0}/poetry.lock', env.WORKDIR)) }} key: py-deps-${{ runner.os }}-${{ runner.arch }}-py-${{ inputs.python-version }}-poetry-${{ inputs.poetry-version }}-${{ inputs.cache-key }}-${{ hashFiles(format('{0}/**/poetry.lock', env.WORKDIR)) }}
- run: ${{ inputs.install-command }}
working-directory: ${{ inputs.working-directory }}
shell: bash

View File

@ -80,31 +80,32 @@ jobs:
find "$WORKDIR" -name '*.py' -type f -not -newermt "$OLDEST_COMMIT_TIME" -exec touch -c -m -t '200001010000' '{}' '+' find "$WORKDIR" -name '*.py' -type f -not -newermt "$OLDEST_COMMIT_TIME" -exec touch -c -m -t '200001010000' '{}' '+'
echo "oldest-commit=$OLDEST_COMMIT" >> "$GITHUB_OUTPUT" echo "oldest-commit=$OLDEST_COMMIT" >> "$GITHUB_OUTPUT"
- uses: actions/cache@v3
id: cache-pip - name: Set up Python ${{ matrix.python-version }} + Poetry ${{ env.POETRY_VERSION }}
name: Cache langchain editable pip install - ${{ matrix.python-version }} uses: "./.github/actions/poetry_setup"
env:
SEGMENT_DOWNLOAD_TIMEOUT_MIN: "4"
with:
path: |
~/.cache/pip
key: pip-editable-langchain-deps-${{ runner.os }}-${{ runner.arch }}-py-${{ matrix.python-version }}
- name: Install poetry
run: |
pipx install "poetry==$POETRY_VERSION"
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
env:
SEGMENT_DOWNLOAD_TIMEOUT_MIN: "4"
with: with:
python-version: ${{ matrix.python-version }} python-version: ${{ matrix.python-version }}
cache: poetry poetry-version: ${{ env.POETRY_VERSION }}
cache-dependency-path: | working-directory: ${{ inputs.working-directory }}
${{ env.WORKDIR }}/**/poetry.lock cache-key: lint
- name: Check Poetry File
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
poetry check
- name: Check lock file
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
poetry lock --check
- name: Install dependencies - name: Install dependencies
working-directory: ${{ inputs.working-directory }} working-directory: ${{ inputs.working-directory }}
run: | run: |
poetry install poetry install
- name: Install langchain editable - name: Install langchain editable
working-directory: ${{ inputs.working-directory }} working-directory: ${{ inputs.working-directory }}
if: ${{ inputs.working-directory != 'libs/langchain' }} if: ${{ inputs.working-directory != 'libs/langchain' }}

View File

@ -27,14 +27,19 @@ jobs:
name: Pydantic v1/v2 compatibility - Python ${{ matrix.python-version }} name: Pydantic v1/v2 compatibility - Python ${{ matrix.python-version }}
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
- name: Set up Python ${{ matrix.python-version }} + Poetry ${{ env.POETRY_VERSION }}
uses: "./.github/actions/poetry_setup" uses: "./.github/actions/poetry_setup"
with: with:
python-version: ${{ matrix.python-version }} python-version: ${{ matrix.python-version }}
working-directory: ${{ inputs.working-directory }}
poetry-version: ${{ env.POETRY_VERSION }} poetry-version: ${{ env.POETRY_VERSION }}
working-directory: ${{ inputs.working-directory }}
cache-key: pydantic-cross-compat cache-key: pydantic-cross-compat
install-command: poetry install
- name: Install dependencies
shell: bash
run: poetry install
- name: Install the opposite major version of pydantic - name: Install the opposite major version of pydantic
# If normal tests use pydantic v1, here we'll use v2, and vice versa. # If normal tests use pydantic v1, here we'll use v2, and vice versa.
shell: bash shell: bash

View File

@ -27,14 +27,19 @@ jobs:
name: Python ${{ matrix.python-version }} name: Python ${{ matrix.python-version }}
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
- name: Set up Python ${{ matrix.python-version }} + Poetry ${{ env.POETRY_VERSION }}
uses: "./.github/actions/poetry_setup" uses: "./.github/actions/poetry_setup"
with: with:
python-version: ${{ matrix.python-version }} python-version: ${{ matrix.python-version }}
working-directory: ${{ inputs.working-directory }}
poetry-version: ${{ env.POETRY_VERSION }} poetry-version: ${{ env.POETRY_VERSION }}
working-directory: ${{ inputs.working-directory }}
cache-key: core cache-key: core
install-command: poetry install
- name: Install dependencies
shell: bash
run: poetry install
- name: Run core tests - name: Run core tests
shell: bash shell: bash
run: make test run: make test

View File

@ -64,15 +64,20 @@ jobs:
name: Python ${{ matrix.python-version }} extended tests name: Python ${{ matrix.python-version }} extended tests
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
- name: Set up Python ${{ matrix.python-version }} + Poetry ${{ env.POETRY_VERSION }}
uses: "./.github/actions/poetry_setup" uses: "./.github/actions/poetry_setup"
with: with:
python-version: ${{ matrix.python-version }} python-version: ${{ matrix.python-version }}
working-directory: ${{ env.WORKDIR }}
poetry-version: ${{ env.POETRY_VERSION }} poetry-version: ${{ env.POETRY_VERSION }}
working-directory: libs/langchain
cache-key: extended cache-key: extended
install-command: |
- name: Install dependencies
shell: bash
run: |
echo "Running extended tests, installing dependencies with poetry..." echo "Running extended tests, installing dependencies with poetry..."
poetry install -E extended_testing poetry install -E extended_testing
- name: Run extended tests - name: Run extended tests
run: make extended_tests run: make extended_tests

View File

@ -61,18 +61,23 @@ jobs:
name: test with unpublished langchain - Python ${{ matrix.python-version }} name: test with unpublished langchain - Python ${{ matrix.python-version }}
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
- name: Set up Python ${{ matrix.python-version }} + Poetry ${{ env.POETRY_VERSION }}
uses: "./.github/actions/poetry_setup" uses: "./.github/actions/poetry_setup"
with: with:
python-version: ${{ matrix.python-version }} python-version: ${{ matrix.python-version }}
working-directory: ${{ env.WORKDIR }}
poetry-version: ${{ env.POETRY_VERSION }} poetry-version: ${{ env.POETRY_VERSION }}
working-directory: ${{ env.WORKDIR }}
cache-key: unpublished-langchain cache-key: unpublished-langchain
install-command: |
- name: Install dependencies
shell: bash
run: |
echo "Running tests with unpublished langchain, installing dependencies with poetry..." echo "Running tests with unpublished langchain, installing dependencies with poetry..."
poetry install poetry install
echo "Editably installing langchain outside of poetry, to avoid messing up lockfile..." echo "Editably installing langchain outside of poetry, to avoid messing up lockfile..."
poetry run pip install -e ../langchain poetry run pip install -e ../langchain
- name: Run tests - name: Run tests
run: make test run: make test