Skip to main content
← Back to workflows

How to Use GitHub Actions for Installing and Configuring Poetry

snok/install-poetry -
GitHub Action
v1.3.4
574
Contributors
Contributor - sondrelgContributor - mario-bermontiContributor - connortann
Categories
CICUBE ANALYTICS INSIGHTS
Engineering Velocity: 25% Team Time Lost to CI Issues
View Platform →
3.5h
Time Saved/Dev/Week
40%
Faster Releases
Click for next insight
Usage
- name: Install Poetry
uses: snok/install-poetry@v1

install-poetry logo

install-poetry

Github action for installing and configuring Poetry


snok/install-poetry GitHub action streamlines our Python project workflows. It automates the installation and configuration of Poetry. This helps ensure consistent and efficient dependency management across our projects.

Integrating Poetry via GitHub Actions can significantly reduce manual setup efforts, ensuring consistency and saving time during our CI/CD processes.

By automating the installation and configuration of Poetry, we can enhance our CI/CD workflows, ensuring reliable and repeatable results for our Python projects.

How to Install Poetry Using GitHub Actions?

  1. Basic Installation

    • For a default Poetry setup, add the following step to your workflow:
    - name: Install Poetry
    uses: snok/install-poetry@v1

This installs Poetry with the default settings, adding executables to the runner system path and setting relevant config settings.

How to Configure Poetry with Specific Settings?

  1. Advanced Configuration

    • You can specify various inputs to customize the Poetry installation and configuration.
    - name: Install and configure Poetry
    uses: snok/install-poetry@v1
    with:
    version: 1.5.1
    virtualenvs-create: true
    virtualenvs-in-project: false
    virtualenvs-path: ~/my-custom-path
    installer-parallel: true

This allows setting specific versions, virtual environment locations, and other configurations to suit project needs.

How to Specify Installation Arguments?

  1. Using Installation Arguments

    • Directly pass installation arguments to the Poetry installer.
    - name: Install and configure Poetry
    uses: snok/install-poetry@v1
    with:
    installation-arguments: --git https://github.com/python-poetry/poetry.git@69bd6820e320f84900103fdf867e24b355d6aa5d

This flexibility allows you to use specific versions or branches of Poetry as required.

How to Make Further Config Changes?

  1. Post-Installation Configuration

    • After invoking the action, make additional configuration changes if needed.
    - uses: snok/install-poetry@v1
    - run: poetry config experimental.new-installer false

This ensures any necessary configurations that aren't covered by the initial setup can still be applied.

How to Structure a Basic Test Workflow?

  1. Example Workflow for Running Tests

    • A basic workflow for running your test suite.
    name: test

    on: pull_request

    jobs:
    test:
    runs-on: ubuntu-latest
    steps:
    - name: Check out repository
    uses: actions/checkout@v4
    - name: Set up python
    id: setup-python
    uses: actions/setup-python@v5
    with:
    python-version: '3.12'
    - name: Install Poetry
    uses: snok/install-poetry@v1
    with:
    virtualenvs-create: true
    virtualenvs-in-project: true
    installer-parallel: true
    - name: Load cached venv
    id: cached-poetry-dependencies
    uses: actions/cache@v3
    with:
    path: .venv
    key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
    - name: Install dependencies
    if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
    run: poetry install --no-interaction --no-root
    - name: Install project
    run: poetry install --no-interaction
    - name: Run tests
    run: |
    source .venv/bin/activate
    pytest tests/
    coverage report

This setup ensures dependencies are installed, cached, and the test suite is run efficiently.

How to Implement Matrix Testing?

  1. Matrix Testing Workflow

    • An extensive example for running tests across multiple OS and Python versions.
    name: test

    on: pull_request

    jobs:
    linting:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-python@v5
    - uses: actions/cache@v3
    with:
    path: ~/.cache/pip
    key: ${{ runner.os }}-pip
    restore-keys: ${{ runner.os }}-pip
    - run: python -m pip install black flake8 isort
    - run: |
    flake8 .
    black . --check
    isort .
    test:
    needs: linting
    strategy:
    fail-fast: true
    matrix:
    os: [ "ubuntu-latest", "macos-latest" ]
    python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12" ]
    django-version: [ "4", "5" ]
    runs-on: ${{ matrix.os }}
    steps:
    - name: Check out repository
    uses: actions/checkout@v4
    - name: Set up python ${{ matrix.python-version }}
    id: setup-python
    uses: actions/setup-python@v5
    with:
    python-version: ${{ matrix.python-version }}
    - name: Install Poetry
    uses: snok/install-poetry@v1
    with:
    virtualenvs-create: true
    virtualenvs-in-project: true
    - name: Load cached venv
    id: cached-poetry-dependencies
    uses: actions/cache@v3
    with:
    path: .venv
    key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
    - name: Install dependencies
    if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
    run: poetry install --no-interaction --no-root
    - name: Install library
    run: poetry install --no-interaction
    - name: Install django ${{ matrix.django-version }}
    run: |
    source .venv/bin/activate
    pip install "Django==${{ matrix.django-version }}"
    - name: Run tests
    run: |
    source .venv/bin/activate
    pytest tests/
    coverage report

This matrix setup helps ensure compatibility across multiple environments and versions.