Skip to content

Commit 354f85b

Browse files
author
David Robertson
authored
GitHub action to setup poetry (#1)
* GitHub action to setup poetry actions/setup-python does the hard work for us, but we make use of actions/cache to avoid having to re-install poetry and the project unless the poetry version we're using has changed, or the hash of the lockfile has changed. After using this action, subsequent job steps can use `poetry run foo [...]` to run `foo [...]` in the virtual environment. Given that I've cited snok/install-poetry I've used theirs but stuck in m.org as a secondary author
1 parent bfda40d commit 354f85b

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Sondre Lillebø Gundersen
4+
Copyright (c) 2022 The Matrix.org Foundation C.I.C.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This is an action which sets up Python and poetry for a project that uses [Poetry](https://python-poetry.org/) to manage its dependencies.
2+
It requires a `poetry.lock` file to be present in the root of the repository. The action:
3+
4+
- calls [`actions/setup-python`](https://github.com/actions/setup-python),
5+
- `pip`-installs `poetry`,
6+
- `poetry install`-s the current project,
7+
- uses [`actions/cache`](https://github.com/actions/cache) to cache the poetry installation and the virtual environment it manages.
8+
9+
From this point, the caller of the action can run commands within the poetry-managed environment with `poetry run`.
10+
11+
The Python and Poetry versions to are configurable: see `action.yaml` for details.
12+
13+
This particular action is loosely based on [`snok/install-poetry`](https://github.com/snok/install-poetry): in particular, the advice from its README on handling caching.

action.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Setup Python and Poetry
2+
description: Setup Python and Poetry. Cribbed from snok/install-poetry.
3+
inputs:
4+
python-version:
5+
description: Python version to pass to actions/setup-python@v2.
6+
required: false
7+
default: "3.x"
8+
poetry-version:
9+
description: Poetry version to install via pip.
10+
required: false
11+
default: "1.1.12"
12+
runs:
13+
using: composite
14+
steps:
15+
- name: Setup Python
16+
id: setup-python
17+
uses: actions/setup-python@v2
18+
with:
19+
python-version: ${{ inputs.python-version }}
20+
21+
- name: Check for poetry lock
22+
run: "test -f poetry.lock || (echo No lock file! && false)"
23+
shell: bash
24+
25+
# Install poetry. It takes ~10seconds for a clean pip install, most of which is
26+
# the installation rather than the downloading. So instead, we cache the user-base
27+
# directory (~/.local). For this to be safe:
28+
# - we must not write to `.local` as part of the CI
29+
# - we must not install anything with the system `pip` other than poetry.
30+
# Based on the writeup at:
31+
# https://github.com/snok/install-poetry/blob/main/README.md#caching-the-poetry-installation
32+
- name: Locate user site
33+
id: site-user-base
34+
run: echo "::set-output name=dir::$(python -m site --user-base)"
35+
shell: bash
36+
37+
- name: Restore/cache poetry installation
38+
id: poetry-install-cache
39+
uses: actions/cache@v2
40+
with:
41+
path: ${{ steps.site-user-base.outputs.dir }}
42+
key: poetry-install-cache-${{ steps.setup-python.outputs.python-version }}-${{ inputs.poetry-version }}
43+
44+
- name: Install poetry from scratch
45+
if: "${{ steps.poetry-install-cache.outputs.cache-hit != 'true' }}"
46+
run: python -m pip install --user poetry==${{ inputs.poetry-version }}
47+
shell: bash
48+
49+
# Poetry manages a virtualenv for us. We're going to cache that too.
50+
# Again, we're following snok/install-poetry's README.
51+
- name: Locate poetry venv
52+
id: poetry-venvs
53+
run: echo "::set-output name=dir::$(python -m poetry config virtualenvs.path)"
54+
shell: bash
55+
56+
- name: Restore/cache poetry venv
57+
id: poetry-venv-cache
58+
uses: actions/cache@v2
59+
with:
60+
path: ${{ steps.poetry-venvs.outputs.dir }}
61+
key: poetry-venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
62+
63+
- name: Install dependencies
64+
if: "${{ steps.poetry-venv-cache.outputs.cache-hit != 'true' }}"
65+
run: poetry install --no-interaction --no-root
66+
shell: bash
67+
68+
# (Not sure if this is needed if there's a cache hit?)
69+
- name: Install project
70+
run: poetry install --no-interaction
71+
shell: bash
72+
73+
# For debugging---let's just check what we're working with.
74+
- name: Dump virtual environment
75+
run: |
76+
poetry env info
77+
poetry show
78+
shell: bash

0 commit comments

Comments
 (0)