Search code examples
pythoncontinuous-integrationgithub-actionspython-poetry

Github Actions don't reuse cache


I have a pretty simple step for CI on Github Actions, which is supposed to cache Python dependencies, so it would save a lot of computing time.

  some-step:
    name: 'Test step'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: pipx install poetry
      - name: Set up Python 3.8
        uses: actions/setup-python@v4
        with:
          python-version: "3.8"
          architecture: "x64"
          cache: "poetry"
      - name: Install dependencies
        run: poetry install
      - run: poetry run ...

Every time when I create a new PR, new cache is generated, even if dependencies didn't change. As I found out it happens because of cache branch restrictions.

My question is how to create a common cache? Or how to remove branch restrictions?

I rarely have to rerun my actions, so this caching implementation doesn't give any benefits.


Solution

  • I reused action (it's really light and does a simple check quickly) and created a new workflow, which runs on lock file changes in master or could be run manually.

    on:
      push:
        branches:
          - master
        paths:
          - 'poetry.lock'
      workflow_dispatch:
        inputs:
          logLevel:
            description: 'Log level'
            required: false
            default: 'warning'
            type: choice
            options:
              - info
              - warning
              - debug 
    jobs:  
        some-step:
            name: 'Test step'
            runs-on: ubuntu-latest
            steps:
              - uses: actions/checkout@v3
              - run: pipx install poetry
              - name: Set up Python 3.8
                uses: actions/setup-python@v4
                with:
                  python-version: "3.8"
                  architecture: "x64"
                  cache: "poetry"
              - name: Install dependencies
                run: poetry install
              - run: poetry run ...