Skip to content

Instantly share code, notes, and snippets.

@gh640
Created April 7, 2023 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gh640/317865611e39b93442579ed3cd4491ce to your computer and use it in GitHub Desktop.
Save gh640/317865611e39b93442579ed3cd4491ce to your computer and use it in GitHub Desktop.
Sample: Composite action to install Python and Poetry on GitHub Actions workflows
name: 'Install Python and Poetry'
description: 'Add Poetry, dependency manager for Python'
inputs:
python-version:
description: 'Python version'
required: true
poetry-version:
description: 'Poetry version'
required: true
runs:
using: 'composite'
steps:
- name: Set up Python ${{ inputs.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ inputs.python-version }}
id: setup_python
- name: Install Poetry ${{ inputs.poetry-version }}
run: |
curl -sSL ${{ env.POETRY_URL }} | \
python - --version ${{ inputs.poetry-version }}
echo "$HOME/.local/bin" >> $GITHUB_PATH
shell: bash
env:
POETRY_URL: https://install.python-poetry.org
- name: Cache Poetry cache
uses: actions/cache@v3
with:
path: ~/.cache/pypoetry
key: poetry-cache-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}-${{ inputs.poetry-version }}
- name: Cache Packages
uses: actions/cache@v3
with:
path: ~/.local
key: poetry-local-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}-${{ hashFiles('.github/workflows/*.yml') }}
@gh640
Copy link
Author

gh640 commented Apr 7, 2023

Usage

  1. Put the file in <project_root>/.github/actions/setup-python-poetry/action.yml
  2. Use the action in a workflow:
jobs:
  my_job_name:
    name: ...
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3      
      - uses: ./.github/actions/setup-python-poetry
        with:
          python-version: "3.11"
          poetry-version: "1.4.2"

This might not work in the future due to GitHub Actions changes.

@Elijas
Copy link

Elijas commented Sep 25, 2023

Alternatively, there is a Github Action that does this
https://github.com/packetcoders/action-setup-cache-python-poetry

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