Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use pip cache in github actions?

I am having some issues with using cached pip in Github Actions. My workflow setup is

name: tests

on: [push, pull_request]

jobs:
  linux:

    runs-on: ubuntu-18.04
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.6, 3.7, 3.8]

    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
      if: steps.cache.outputs.cache-hit != 'true'
      run: |
        pip install pytest pytest-cov bandit sphinx recommonmark
        python -m pip install --upgrade pip
        pip install .
    - name: Test with pytest
      run: |
        pytest --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/
        coverage report -m
    - name: Test with bandit
      run: |
        bandit --recursive chepy/ --ignore-nosec --skip B101,B413,B303,B310,B112,B304,B320,B410,B404
    - name: Test docs
      run: |
        make -C docs/ clean html
    - name: Upload to codecov
      uses: codecov/[email protected]
      with:
        token: ${{secrets.CODECOV_TOKEN}}
        file: ./coverage.xml

The issue I am having is that in subsequent actions that are triggered, it will not using the cache, but instead installing from pip all over again.

How can I make it use the cache?

like image 477
securisec Avatar asked Dec 01 '19 15:12

securisec


People also ask

What is pip cache used for?

pip provides an on-by-default caching, designed to reduce the amount of time spent on duplicate downloads and builds.

What is action cache?

This action allows caching dependencies and build outputs to improve workflow execution time.


2 Answers

You forgot about adding id in step which uses actions/cache. This is required for condition in Install step to work.

- uses: actions/cache@v1
  id:   cache
  with:
   ...
like image 154
Samira Avatar answered Sep 21 '22 04:09

Samira


You can also cache a virtualenv, like this:

    - uses: actions/cache@v2
      id: cache-venv  # name for referring later
      with:
        path: ./.venv/  # what we cache: the virtualenv
        # The cache key depends on requirements.txt
        key: ${{ runner.os }}-venv-${{ hashFiles('**/requirements*.txt') }}
        restore-keys: |
          ${{ runner.os }}-venv-
    # Build a virtualenv, but only if it doesn't already exist
    - run: python -m venv ./.venv && . ./.venv/bin/activate && 
           pip install -r requirements.txt
      if: steps.cache-venv.outputs.cache-hit != 'true'
    # Run tests
    # Note that you have to activate the virtualenv in every step
    # because GitHub actions doesn't preserve the environment
    - run: . ./.venv/bin/activate && nosetests tests/
like image 21
kolypto Avatar answered Sep 24 '22 04:09

kolypto