Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Surgo
Copy link

@Surgo Surgo commented Oct 31, 2019

This PR adds example for cache Python packages with virtual environments.

Default install path is like /opt/hostedtoolcache/Python/3.7.5/x64/lib/python3.7/site-packages/.
So use virtual environment to install packages into workspace and cache it.

First time (no cache)

Screenshot 2019-11-01 00 43 47

Second time (use cache)

Screenshot 2019-11-01 00 47 49

@smorimoto
Copy link
Contributor

The example has been moved to examples.md. See #13.

@Surgo
Copy link
Author

Surgo commented Oct 31, 2019

Moved into examples.md.

examples.md Outdated
```
```
### Python - pip
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unify format.

Suggested change
### Python - pip
## Python - pip

@smorimoto
Copy link
Contributor

Please add this to the top.

- [Python - pip](#python---pip)

examples.md Outdated
- uses: actions/cache@preview
with:
path: .venv
key: ${{ runner.os }}-pip-${{ hashFiles(format('{0}/{1}', github.workspace, 'requirements.txt')) }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
key: ${{ runner.os }}-pip-${{ hashFiles(format('{0}/{1}', github.workspace, 'requirements.txt')) }}
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}

examples.md Outdated
Comment on lines 112 to 115
- run: |
python -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove run section.

Copy link
Author

@Surgo Surgo Oct 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But .venv path for cache is created by python -m venv .venv command 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just an example of the cache action API, not how to write a Python workflow setup.

Copy link
Contributor

@smorimoto smorimoto Oct 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See an example in other languages/ecosystems.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For an example of using virtualenv, changing to the minimum example code and title to Python - pip, virtualenv should enough. (imo)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ummm... .venv is not default package install path.
Default path is/opt/hostedtoolcache/Python/3.7.5/x64/lib/python3.7/site-packages/ for actions/setup-python@v1 with python-version: 3.7
Is /opt/hostedtoolcache/Python/3.7.5/x64/lib/python3.7/site-packages/ better cache path in examples?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the comment above.

@smorimoto
Copy link
Contributor

Here is correct format.

key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}

examples.md Outdated
- [Swift, Objective-C - Carthage](#swift-objective-c---carthage)
- [Swift, Objective-C - CocoaPods](#swift-objective-c---cocoapods)
- [Ruby - Gem](#ruby---gem)
- [Python - pip, virtuelenv](#python---pip,-virtuelenv)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- [Python - pip, virtuelenv](#python---pip,-virtuelenv)
- [Python - pip, virtuelenv](#python---pip-virtuelenv)

@smorimoto
Copy link
Contributor

If you correct the title link, overall it seems good!

@smorimoto
Copy link
Contributor

Great! Looks good to me!

examples.md Outdated
- [Swift, Objective-C - Carthage](#swift-objective-c---carthage)
- [Swift, Objective-C - CocoaPods](#swift-objective-c---cocoapods)
- [Ruby - Gem](#ruby---gem)
- [Python - pip, virtuelenv](#python---pip-virtuelenv)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- [Python - pip, virtuelenv](#python---pip-virtuelenv)
- [Python - pip, virtualenv](#python---pip-virtualenv)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

@philipslan
Copy link

One thing to note about caching the virtualenv is that users will have to manually activate the environment for each task. That might be worth adding.

@Surgo
Copy link
Author

Surgo commented Nov 4, 2019

Cache download cache instead of virtualenv like Travis-CI

```yaml
- uses: actions/cache@preview
with:
path: ~/.cache/pip
Copy link
Contributor

@hugovk hugovk Nov 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This path only works for Linux, see #3 (comment).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh.. thank you for your advice :)

key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
```

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should also probably be an example for windows here. The path I believe is path: ~\AppData\Local\pip\Cache from pip docs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- uses: actions/cache@preview
  if: startsWith(runner.os, 'Windows')
  with:
    path: ~\AppData\Local\pip\Cache
    key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
    restore-keys: |
      ${{ runner.os }}-pip-

Copy link
Contributor

@joshmgross joshmgross left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

Could you add an example for windows as well? (See @philipslan's comment)

@joshmgross
Copy link
Contributor

With a step like:

    - 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)"

We can get the cache directory from pip, so that we don't need to hardcode it and we can work universally.

Once #39 is fixed, we can remove the extra Windows action as we'll be able to use / instead of \ inside hashFiles (that fix should be rolling out soon)

Thus the example would be something like

- 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
  if: startsWith(runner.os, 'Windows')
  with:
    path: ${{ steps.pip-cache.outputs.dir }}
    key: ${{ runner.os }}-pip-${{ hashFiles('**\requirements.txt') }}
    restore-keys: |
      ${{ runner.os }}-pip-

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

@hugovk
Copy link
Contributor

hugovk commented Nov 6, 2019

Note the underscore before internal, and the word internal: pip has no public API.

And it did change fairly recently:

@joshmgross
Copy link
Contributor

@hugovk Good point, we shouldn't recommend using an internal API in our docs.

@Surgo could you add a Windows example following like:

- uses: actions/cache@v1
  if: startsWith(runner.os, 'Windows')
  with:
    path: ~\AppData\Local\pip\Cache
    key: ${{ runner.os }}-pip-${{ hashFiles('**\requirements.txt') }}
    restore-keys: |
      ${{ runner.os }}-pip-

If you're busy or unable to continue this PR, please let me know and I can pick it up. Thanks again for all the work you've done!

@shadowmoose
Copy link

shadowmoose commented Nov 10, 2019

Not sure if this is useful for anyone here or at Issue #3, but I've cobbled together a cross-platform cache workflow that uses one preliminary step to find the pip library location, using built-in Python libraries & supported methods.

It seems to work on all my projects. It also avoids adding extra skipped build steps.

name: CacheTest

on:
  push

jobs:
  run-tests:
    name: ${{ matrix.os }}, ${{ matrix.python-version }}
    runs-on: ${{ matrix.os }}-latest
    strategy:
      max-parallel: 3
      matrix:
        os: [ubuntu, windows, macOS]
        python-version: [3.5, 3.6, 3.7]

    steps:
      - uses: actions/checkout@v1
      - name: Set up Python
        uses: actions/setup-python@v1
        with:
          python-version: ${{ matrix.python-version }}
      
      - name: Get Python Location
        run: |
          echo "::set-output name=path::$(python -c 'import site; print(site.getusersitepackages())')"
        id: get_location

      - name: Activate Cache
        uses: actions/cache@preview
        with:
          path: ${{ steps.get_location.outputs.path }}
          key: pytest-${{ matrix.os }}-${{ matrix.python-version }}-pip-${{ hashFiles('**/**equirement*.txt') }}
          restore-keys: |
            pytest-${{ matrix.os }}-${{ matrix.python-version }}-pip

      - name: Install Dependencies
        run: |
          python -m pip install --upgrade pip setuptools wheel
          pip install --upgrade -r requirements.txt --user
          # (Requirements are now installed to the cached user directory.)

@hugovk hugovk mentioned this pull request Nov 10, 2019
10 tasks
@joshmgross joshmgross mentioned this pull request Nov 12, 2019
@joshmgross
Copy link
Contributor

Resuming the work for PR at #86

@Surgo
Copy link
Author

Surgo commented Nov 13, 2019

Sorry my late response 🙇

@Surgo Surgo closed this Nov 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants