Trouble caching python dependencies

After trying various solutions, including this one, I am still unable to cache the pip dependencies for actions. I have a dependency package that takes 8 minutes to install, so it would be a game-changer if I could skip this step every time I create a pull request.

My pythonpackage.yml file:

name: Python package

on:
  push:
    branches: [master]
  pull_request:
    branches: [dev]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.7, 3.8]
    steps:
      - uses: actions/checkout@v2
      - name: Setup python ${{ matrix.python-version }}
        uses: actions/setup-python@v1
        with:
          python-version: ${{ matrix.python-version }}
      - name: Use Python Dependency Cache
        uses: actions/cache@v1
        id: cache
        with:
          path: .cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
            ${{ runner.os }}-
      - name: Install Dependencies
        if: steps.cache.outputs.cache-hit != 'true'
        run: |
          python -m pip install --upgrade pip
          pip install flake8 pytest
          if [-f requirements.txt]; then pip install -r requirements.txt; fi
      - name: Lint with flake8
        run: |
          # stop the build if there are Python syntax errors or undefined names
          flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
          # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
          flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
      - name: Test with pytest
        run: |
          pytest

The output of action:

Hi @kzaborski ,

Thank you for reaching this out! Please use ‘actions/cache@ master’ instead.

This a ‘always cache miss’ issue which i can repro on my side with version v1.

It has been reported in issue list here: https://github.com/actions/cache/issues/296

It’s recommended to comment on the existing ticket and track if there’s any fix from github dev team.

Thanks.

1 Like

Thanks for the reply @weide-zhou. The dependency install step is being skipped after following your advice and using ‘actions/cache@ master’, but I am getting an error with flake8. I don’t think that flake8 should check this file at all. Any ideas on how to tackle it?

Capture.JPG

Hi @kzaborski ,

Thanks for your quick reply! The ‘install Dependencies’ step is skipped since catch hit in the cache step, it’s a correct behavior. 

Please edit requirements.txt to make a cache miss manually, it will install dependencies, and at last it will restore a new cache which include the flake… then you can restore from this cache for your next workflow runs.

Regards.

Hi @weide-zhou,

I have tried following your guidelines, unfortunately without success. It seems like the cache is hit, but the dependencies seem not to be there! Please see the code and screenshot below:

name: Python application

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.7
      uses: actions/setup-python@v2
      with:
        python-version: 3.7
    - name: Cache Python Dependencies
      uses: actions/cache@master
      id: cache
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
        restore-keys: |
          ${{ runner.os }}-pip-
          ${{ runner.os }}-
    - name: Install dependencies
      if: steps.cache.outputs.cache-hit != 'true'
      run: |
        python -m pip install --upgrade pip
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Lint with flake8
      run: |
        pip install flake8
        # stop the build if there are Python syntax errors or undefined names
        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
        # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
    - name: Test with pytest
      run: |
        pip install pytest
        pytest

HI @kzaborski,

Sorry for late response! From the attached screenshot, looks the error is caused by the test module file and the module name, which seems not be related to the dependency.

Could you please check the file before the error step? If the script works without cache, could you please compare the files (with/without cache) before the step.

Thanks.