5
4

More than 3 years have passed since last update.

GitHub Actions Pythonのcacheサンプル

Posted at

14541002696_1cc1d3c206.jpg

actions/cache

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-

ちなみに他の言語もそれぞれ公式が分かりやすようにサンプルを書いてくれていてとても簡単に導入できる。

cache/examples.md

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でキャッシュが使えるようになって嬉しい!

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4