Search code examples
continuous-integrationcontinuous-deploymentgithub-actionspython-poetry

How do I cache an installed tool between runs in GitHub Actions?


I need to run poetry version to get the pyproject.toml version on every push to master touching pyproject.toml. However, since Poetry is not installed on GitHub Actions runner virtual environments, I also need to install it before I can run any Poetry commands. I want to cache this tool installation so I don't have to install it on every run and use up Actions minutes. The only Poetry command I need to run is poetry version, so I'm not worried about the tool being outdated - it only needs to parse the pyproject.toml and get the project version number. I'm also not sure what to use as my key for the caching action - I assume it can be static

The desired order of operations would be something like:

  1. Check out repo.
  2. Check cache for Poetry. If it's not installed, install it.
  3. Run poetry version.

Solution

  • The key input of actions/cache@v2 can be a string - provide it something arbitrary. The path input is the location of the tool.

    Potential pitfalls:

    • Note that the path argument does NOT resolve environment variables like $HOME, but the tilde (~) can be used to signify the home directory.
    • Poetry has to be prepended to the PATH on every run, as default environment variables are not preserved between runs.
    • Poetry may complain that it's dropping support for Python2 soon - to ensure it's running with Python 3, make sure to setup the run with any of the Python 3 versions.
    on:
      push:
        branches:
          - master
        paths:
          - 'pyproject.toml'
    
    jobs:
      pyproject-version:
        runs-on: 'ubuntu-latest'
        steps:
          - name: Checkout code
            uses: actions/checkout@v2
          - name: Setup Python
            uses: actions/setup-python@v2
            with:
              python-version: '3.7'
          # Perma-cache Poetry since we only need it for checking pyproject version
          - name: Cache Poetry
            id: cache-poetry
            uses: actions/cache@v2
            with:
              path: ~/.poetry
              key: poetry
          # Only runs when key from caching step changes
          - name: Install latest version of Poetry
            if: steps.cache-poetry.outputs.cache-hit != 'true'
            run: |
              curl -sSL https://install.python-poetry.org | python -
          # Poetry still needs to be re-prepended to the PATH on each run, since
          # PATH does not persist between runs.
          - name: Add Poetry to $PATH
            run: |
              echo "$HOME/.poetry/bin" >> $GITHUB_PATH
          - name: Get pyproject version
            run: poetry version