Skip to content

Instantly share code, notes, and snippets.

@blu3r4y
Last active August 29, 2024 09:10
Show Gist options
  • Save blu3r4y/0e316bacca5a55f37acb3258a5465b1b to your computer and use it in GitHub Desktop.
Save blu3r4y/0e316bacca5a55f37acb3258a5465b1b to your computer and use it in GitHub Desktop.
GitHub action to activate a Poetry environment for subsequent steps
name: setup-poetry
on:
push:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Setup Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
echo "$HOME/.local/bin" >> $GITHUB_PATH
- name: Install dependencies
run: poetry install
- name: Active environment
shell: bash
run: |
source $(poetry env info --path)/bin/activate
echo "$VIRTUAL_ENV/bin" >> $GITHUB_PATH
echo "PYTHONHOME=" >> $GITHUB_ENV
@IvanildoBarauna
Copy link

IvanildoBarauna commented Jun 11, 2024

Hello @blu3r4y

Sugestion for this:

jobs:
  test:
    if: github.actor != 'actions[bot]'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.head_ref }}

      - name: Set up Python 3.9
        uses: actions/setup-python@v2
        with:
          python-version: 3.9

      - name: Setup tests
        run: |
          pip install --upgrade pip
          curl -sSL https://install.python-poetry.org | python3 -
          poetry config virtualenvs.create false
          poetry install

The line poetry config virtualenvs.create false is here for dont create a virtual env in Ubuntu ;)

@blu3r4y
Copy link
Author

blu3r4y commented Jun 14, 2024

Thanks @IvanildoBarauna for the suggestion, that is a great trick.

I partially remember that the crucial pieces of my previous Gist were about sourcing to GITHUB_PATH and GITHUB_ENV because without that I had issues when trying to directly call scripts, python packages, and even the poetry binary. I have no system right now to test your script against that, but future readers might.

@IvanildoBarauna
Copy link

Hi @blu3r4y

I'm happy that I can contribute with others readers using your Gist, thanks.

I have a project that uses exactly this script structure, if you are interested in seeing how this works in the real world, here it is...

https://github.com/IvanildoBarauna/api-to-dataframe
Workflow and line is: https://github.com/IvanildoBarauna/api-to-dataframe/blob/main/.github/workflows/CI.yaml#L25-L30

The following project below also has similar needs, but uses another method with python's "venv"

https://github.com/IvanildoBarauna/ETL-awesome-api

Workflow and line: https://github.com/IvanildoBarauna/ETL-awesome-api/blob/main/.github/workflows/CI-CD.yaml#L28-L36

@DavidSchweigert
Copy link

DavidSchweigert commented Aug 7, 2024

Thank you so much for sharing those links with us. I appreciate you for helping me out and now it is my turn to repay you. And I would like to share the https://www.topessaywriting.org/samples/poetry website with you where you can read essay samples on various topics such as poetry and many more. If you are not good at writing essay assignments then you can visit the given link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment