How to use cache along w/ setup-python workflows?

Here’s how my workflow looks like

name: Python package

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.7]

    steps:
    - uses: actions/checkout@v1
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v1
      with:
        python-version: ${{ matrix.python-version }}

    - uses: actions/cache@v1
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
        restore-keys: |
          ${{ runner.os }}-pip-

    - name: Install Dependencies
      if: steps.cache.outputs.cache-hit != 'true'
      run: |
        pip install -r requirements.txt

However, it seems that the requirements are not being cached as they are downloaded and installed every time. Here’s the output of “Post actions/cache@v1”:

Cache hit occurred on the primary key Linux-pip-ec38f2290e660cb7a14876cd8ec60de142217331849cdf625483659c4d36d621, not saving cache.

I’d appreciate it if someone could explain what’s wrong and how to fix it.

Many thanks,

Amin

You only have 1 element in your version matrix so I am not sure what max-parallel is doing for you here. 

Are you saying that on subsiquent runs you are not seeing a cache download and restore?

Hi,

Glad to see you in Github Community Forum!

Your ‘key’ only changes when you update ‘requirement.txt’ file or runner.os.  If you have cached for the first time, and not change requirement.txt/runner.os,  the ‘cache-hit’ value will always be true which mean ‘cache hit’. The cache will not be saved then. Regarding the cache progress, please check official doc here.

I fixed some code, please kindly check below. You can use a variable for the key value, please check this ticket.

runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.7']

    steps:
      - uses: actions/checkout@v1
      - name: Setup python ${{ matrix.python-version }}
        uses: actions/setup-python@v1
        with:
          python-version: ${{ matrix.python-version }}

      - uses: actions/cache@v1
        id: cache
        with:
          path: .cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-test
      - name: Install Dependencies
        if: steps.cache.outputs.cache-hit != 'true'
        run: |
          echo test

4 Likes