GitHub Actions Python cache sample

14541002696_1cc1d3c206.jpg

actions/cache

Notes on using pip cache with GitHub Actions

pip cache

The directory for each OS is as follows (I haven't tried it, but in the case of Ubuntu, it can be cached to any path by specifying the environment variable "XDG_CACHE_HOME".)

# Unix
~/.cache/pip

# macOS
~/Library/Caches/pip

# Windows
<CSIDL_LOCAL_APPDATA>\pip\Cache

how to use

Create cache (assuming ubuntu is used)

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

Restore from cache

- name: Get pip cache
   id: pip-cache
   run: |
     python -c "from pip._internal.locations import USER_CACHE_DIR; print('::set-output name=dir::' + USER_CACHE_DIR)"

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

By the way, other languages also have samples written so that the formulas are easy to understand, so it is very easy to introduce.

cache/examples.md

If you don't want to use pip cache even if the package is written in requirements.txt, you need to install it with "--no-cache-dir" separately.

$ pip --no-cache-dir install [package name]

Define what you want to do if you don't cache-hit

Skipping steps based on cache-hit

The presence or absence of a cache hit is held by a bool value called cache-hit. If there is no hit, you can easily implement the flow of deploying or continuing the subsequent processing.

Reference material

pip install ■ [Cache Dependencies to Speed Up Workflow](https://help.github.com/ja/actions/automating-your-workflow-with-github-actions/caching-dependencies-to-speed-up -workflows) ■ I'm happy to be able to use the cache on GitHub Actions!

Recommended Posts

GitHub Actions Python cache sample
Python closure sample
Ajax + Python + PostgreSQL sample
Python --Simple multi-thread sample
Sample data created with python
Use HTTP cache in Python
Tested pipenv with GitHub Actions
Python higher-order function (decorator) sample
Sample usage of Python pickle
[Python] Sample code for Python grammar
AtCoder: Python: Daddy the sample test.
[Python] Test sample using unittest2, mock
My favorite boto3 (Python) API sample
GitHub x Python development preparation memo
Git & Github & python & VScode Personal memorandum
Python parallel / parallel processing sample code summary
NAOqi Python sample (make NAO walk)