Skip to content

Add poetry install #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions install-python-poetry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Install Python Poetry

This action will install and setup [python-poetry](https://python-poetry.org/).

Unlike [snok/install-poetry](https://github.com/snok/install-poetry), this action does NOT
rely on the [poetry installer](https://install.python-poetry.org/).

The poetry installer is recommended for desktop and personal usage, in CI environments, it is best
to setup poetry from Pypi for maximum control. There have been issues where CI builds failed as
`install.python-poetry.org` was down.

```yaml
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
- uses: XanaduAI/cloud-actions/install-python-poetry@main
- run: poetry --version
```

## Inputs

### Install a specific version of Poetry
By default, the latest version of poetry available on Pypi is installed.
But you can pass a specific version to install, this is recommended in CI as it would
make the builds more deterministic.
```yaml
- uses: XanaduAI/cloud-actions/install-python-poetry@main
with:
poetry_version: 1.6.2
```

### Install poetry to a specific directory
The default installation directory is within `/tmp/`. But you can
change it using `poetry_home`:
```yaml
- uses: XanaduAI/cloud-actions/install-python-poetry@main
with:
poetry_version: 1.6.2
poetry_home: /some/other/directory
```
A python venv is created in the directory, and poetry is installed within that venv.
This ensures that poetry and the calling workflow python packages do not intermix as
that may lead to poetry attempting to manage it's own dependencies (and can break).

### Adding poetry to PATH
The default behavior is to add poetry to PATH by creating a symlink.
This action does NOT update PATH as it can change the python interpreter that
is being used by the calling workflow. The symlink is created to `/usr/local/bin/poetry`.
In case you do not want to create the symlink, use the input `add_poetry_to_path` to toggle the behavior.
If this is set to false, you have to invoke poetry by calling the binary directly.
```yaml
- uses: XanaduAI/cloud-actions/install-python-poetry@main
id: setup_poetry
with:
poetry_version: 1.6.2
poetry_home: /some/other/directory
add_poetry_to_path: false

- run: ${{ steps.setup_poetry.outputs.poetry_bin }} --version
```


## Outputs:

- `poetry_home` -> The directory where poetry is installed
- `poetry_bin` -> Path to the poetry binary ($POETRY_HOME/bin/poetry)


95 changes: 95 additions & 0 deletions install-python-poetry/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Install Python Poetry
description: This action installs and sets up Python Poetry

inputs:
poetry_version:
type: string
description: The version of poetry that will be installed. Omitting this input will install the latest version of poetry.
required: false
default: ''
poetry_home:
type: string
description: Set a custom directory for POETRY_HOME
required: false
default: ''
add_poetry_to_path:
type: boolean
description: Update PATH so `poetry` can be invoked directly. Defaults to true
required: false
default: true


outputs:
poetry_home:
description: 'Path to directory where poetry is installed'
value: ${{ steps.poetry.outputs.home }}
poetry_bin:
description: 'Path to poetry binary'
value: ${{ steps.poetry.outputs.bin }}


runs:
using: composite
steps:
- name: Check Python available
shell: bash
run: |
if ! command -v python &> /dev/null
then
echo "::error title=Missing Python::Could not find a python installation. Make sure to use actions/setup-python prior to calling this action."
exit 1
fi

# This step outputs `==<inputs.poetry_version>` only if a version is specified by the calling workflow.
# This is done so that if the calling workflow omits version, then the `Install Poetry` step renders to `pip install poetry`
# And if a version is specified, then that step would instead render to `pip install poetry==<inputs.poetry_version>`
- name: Check Poetry Version Input
shell: bash
id: poetry_version
if: inputs.poetry_version != ''
env:
INPUT_POETRY_VERSION: ${{ inputs.poetry_version }}
run: echo "version===$INPUT_POETRY_VERSION" >> $GITHUB_OUTPUT

- name: Setup POETRY_HOME
shell: bash
id: poetry_home
env:
INPUT_POETRY_HOME: ${{ inputs.poetry_home }}
run: |
if [ "$INPUT_POETRY_HOME" == "" ]; then
poetry_home=$(mktemp -d)
else
poetry_home=$INPUT_POETRY_HOME
fi
echo "poetry_home=$poetry_home" >> $GITHUB_OUTPUT


- name: Install Poetry
id: poetry
shell: bash
env:
POETRY_HOME: ${{ steps.poetry_home.outputs.poetry_home }}
run: |
python3 -m venv $POETRY_HOME
$POETRY_HOME/bin/python -m pip install poetry${{ steps.poetry_version.outputs.version }}

echo "home=$POETRY_HOME" >> $GITHUB_OUTPUT
echo "bin=$POETRY_HOME/bin/poetry" >> $GITHUB_OUTPUT

- name: Validate Poetry Installation
shell: bash
run: ${{ steps.poetry.outputs.home }}/bin/poetry --version

# This step symlinks the poetry binary to /usr/local/bin/poetry
# The reason for doing this instead of pre-pending the directory to GITHUB_PATH is so that
# the python binary being used by the calling workflow is not altered.
# If this step did `echo '/path/to/poetry/bin' >> GITHUB_PATH` .. that /bin would also contain a python
# binary, and if the calling workflow used python commands after installing poetry, they may use the wrong
# python binary without knowing it.
# This avoids that situation by not updating PATH.
- name: Add to PATH
if: inputs.add_poetry_to_path == 'true'
shell: bash
run: |
ln -sf ${{ steps.poetry.outputs.home }}/bin/poetry /usr/local/bin/poetry