Cache: Python example

Created on 31 Oct 2019  路  2Comments  路  Source: actions/cache

Could you add an example for a python workspace

documentation

Most helpful comment

pip cache

For pip:

The default location for the cache directory depends on the Operating System:

Unix
~/.cache/pip and it respects the XDG_CACHE_HOME directory.

macOS
~/Library/Caches/pip.

Windows
<CSIDL_LOCAL_APPDATA>\pip\Cache

https://pip.pypa.io/en/stable/reference/pip_install/#caching


Only Ubuntu

Try something like this for Ubuntu:

name: Test

on: [push, pull_request]

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.6", "3.7"]
        os: [ubuntu-latest, ubuntu-16.04, macOS-latest]

    steps:
      - uses: actions/checkout@v1

      - uses: actions/cache@preview
        with:
          path: ~/.cache/pip
          key: test-${{ matrix.os }}-${{ matrix.python-version }}-pip

That won't work for the other operating systems, but doesn't fail the build.


Ubuntu + macOS

An if can be added to duplicated blocks with different paths (plus a name: for clarity):

      - name: Ubuntu cache
        uses: actions/cache@preview
        if: startsWith(matrix.os, 'ubuntu')
        with:
          path: ~/.cache/pip
          key: test-${{ matrix.os }}-${{ matrix.python-version }}-pip

      - name: macOS cache
        uses: actions/cache@preview
        if: startsWith(matrix.os, 'macOS')
        with:
          path: ~/Library/Caches/pip
          key: test-${{ matrix.os }}-${{ matrix.python-version }}-pip

Example: https://github.com/hugovk/tinytext/runs/283473777


Unfortunately you cannot list multiple directories, eg. for both Ubuntu and macOS:

      - uses: actions/cache@preview
        with:
          path:
            - ~/.cache/pip
            - ~/Library/Caches/pip
          key: test-${{ matrix.os }}-${{ matrix.python-version }}-pip
- Your workflow file was invalid: The pipeline is not valid. .github/workflows/test.yml (Line: 20, Col: 13): A sequence was not expected

Windows

I've not tried Windows yet, anyone know the path: to use for <CSIDL_LOCAL_APPDATA>\pip\Cache?

All 2 comments

pip cache

For pip:

The default location for the cache directory depends on the Operating System:

Unix
~/.cache/pip and it respects the XDG_CACHE_HOME directory.

macOS
~/Library/Caches/pip.

Windows
<CSIDL_LOCAL_APPDATA>\pip\Cache

https://pip.pypa.io/en/stable/reference/pip_install/#caching


Only Ubuntu

Try something like this for Ubuntu:

name: Test

on: [push, pull_request]

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.6", "3.7"]
        os: [ubuntu-latest, ubuntu-16.04, macOS-latest]

    steps:
      - uses: actions/checkout@v1

      - uses: actions/cache@preview
        with:
          path: ~/.cache/pip
          key: test-${{ matrix.os }}-${{ matrix.python-version }}-pip

That won't work for the other operating systems, but doesn't fail the build.


Ubuntu + macOS

An if can be added to duplicated blocks with different paths (plus a name: for clarity):

      - name: Ubuntu cache
        uses: actions/cache@preview
        if: startsWith(matrix.os, 'ubuntu')
        with:
          path: ~/.cache/pip
          key: test-${{ matrix.os }}-${{ matrix.python-version }}-pip

      - name: macOS cache
        uses: actions/cache@preview
        if: startsWith(matrix.os, 'macOS')
        with:
          path: ~/Library/Caches/pip
          key: test-${{ matrix.os }}-${{ matrix.python-version }}-pip

Example: https://github.com/hugovk/tinytext/runs/283473777


Unfortunately you cannot list multiple directories, eg. for both Ubuntu and macOS:

      - uses: actions/cache@preview
        with:
          path:
            - ~/.cache/pip
            - ~/Library/Caches/pip
          key: test-${{ matrix.os }}-${{ matrix.python-version }}-pip
- Your workflow file was invalid: The pipeline is not valid. .github/workflows/test.yml (Line: 20, Col: 13): A sequence was not expected

Windows

I've not tried Windows yet, anyone know the path: to use for <CSIDL_LOCAL_APPDATA>\pip\Cache?

pip + pre-commit

Here's an example for caching two directories. Both on a single OS/Python:

name: Lint

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.7]

    steps:
      - uses: actions/checkout@v1

      - name: pip cache
        uses: actions/cache@preview
        with:
          path: ~/.cache/pip
          key: lint-${{ matrix.python-version }}-pip

      - name: pre-commit cache
        uses: actions/cache@preview
        with:
          path: ~/.cache/pre-commit
          key: lint-${{ matrix.python-version }}-pre-commit

You have to be careful the keys are unique between different workflows to avoid collisions!
(See also https://github.com/actions/cache/issues/17.)

Was this page helpful?
0 / 5 - 0 ratings