GitHub Actionsでpipのキャッシュを使用する際のメモ
pipのキャッシュ
OSごとのディレクトリは下記
(試してないですがUbuntuの場合は環境変数「XDG_CACHE_HOME」を指定することで任意のパスへキャッシュできるとのこと。)
# Unix
~/.cache/pip
# macOS
~/Library/Caches/pip
# Windows
<CSIDL_LOCAL_APPDATA>\pip\Cache
使用方法
キャッシュを作成(使用するOSはubuntuを想定)
- uses: actions/cache@v1
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
キャッシュから復元
- 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-
ちなみに他の言語もそれぞれ公式が分かりやすようにサンプルを書いてくれていてとても簡単に導入できる。
requirements.txtに書かれているパッケージでもpipのキャッシュを使用したくないものについては別途「--no-cache-dir」をつけインストールする必要がある。
$ pip --no-cache-dir install [パッケージ名]
cache-hitしなかった際に行いたい動作を定義
Skipping steps based on cache-hit
キャッシュヒットの有無はcache-hitというbool値で保持される。
ヒットしなかった場合はデプロイないし後続の処理を継続といった流れを簡単に実装できる。
参考資料
■ pip install
■ 依存関係をキャッシュしてワークフローのスピードを上げる
■ GitHub Actionsでキャッシュが使えるようになって嬉しい!