Dependency Cache Not Being Used

During the Action the dependency cache is checked and then the dependencies are downloaded again regardless. Every time!

Here’re the relevant blocks in the Action:

    - name: Locate Pip Cache
      id: pip-cache
      run: echo "::set-output name=dir::$(pip cache dir)"

    - name: Cache dependencies
      uses: actions/cache@v2
      with:
        path: ${{ steps.pip-cache.outputs.dir }}
        key: ${{ runner.os }}-pip-${{ hashFiles('**/poetry.lock') }}
        restore-keys: ${{ runner.os }}-pip-

    - name: Install dependencies
      run: |
        python -m pip install --upgrade poetry
        poetry config virtualenvs.create false
        poetry install --no-root

Full Action is here.
Action log is here

Any help would be appreciated!

Wouldn’t your second step need to check if the dependencies are needed?

Edit: actually the log you linked to shows that the dependencies are being installed from cache.

I must be misreading it then. It says it is collecting from cache but then has to install anyway - is the cache just storing the download rather than the installed version?

@Snayff,

If the dependencies will be installed in the “Install dependencies” step have been restored in the “Cache dependencies” step, you can use the if conditional like as below to skip the “Install dependencies” step.
For example:

- name: Locate Pip Cache
      id: pip-cache
      run: echo "::set-output name=dir::$(pip cache dir)"

    - name: Cache dependencies
      uses: actions/cache@v2
      id: cache
      with:
        path: ${{ steps.pip-cache.outputs.dir }}
        key: ${{ runner.os }}-pip-${{ hashFiles('**/poetry.lock') }}
        restore-keys: ${{ runner.os }}-pip-

    - name: Install dependencies
      if: steps.cache.outputs.cache-hit != 'true'
      run: |
        python -m pip install --upgrade poetry
        poetry config virtualenvs.create false
        poetry install --no-root
1 Like

In the log, success in cache:
Cache restored from key: Windows-pip-xxx
But still installed dependencies:
Run python -m pip install --upgrade poetry

This is what I have, which unless I am missing something is what you suggested, @brightran.

    - name: Cache dependencies
      uses: actions/cache@v2
      with:
        path: ${{ steps.pip-cache.outputs.dir }}
        key: ${{ runner.os }}-pip-${{ hashFiles('**/poetry.lock') }}
        restore-keys: ${{ runner.os }}-pip-

    - name: Install dependencies
      if: steps.cache.outputs.cache-hit != 'true'
      run: |
        python -m pip install --upgrade poetry
        poetry config virtualenvs.create false
        poetry install --no-root

Am I missing something?

Try adding a step ID with value cache to your cache step perhaps

Doing that successfully skips the install of dependencies but then all further commands fail. The term 'poetry' is not recognized as the name of a cmdlet, function, script file, or operable program.

Is this the right step forward and I now need to amend the subsequent commands or do I need to unpick the new if statement?

    - name: Locate Pip Cache
      id: pip-cache
      run: echo "::set-output name=dir::$(pip cache dir)"

    - name: Cache dependencies
      id: cache
      uses: actions/cache@v2
      with:
        path: ${{ steps.pip-cache.outputs.dir }}
        key: ${{ runner.os }}-pip-${{ hashFiles('**/poetry.lock') }}
        restore-keys: ${{ runner.os }}-pip-

    - name: Install dependencies
      if: steps.cache.outputs.cache-hit != 'true'
      run: |
        python -m pip install --upgrade poetry
        poetry config virtualenvs.create false
        poetry install --no-root

The pip cache dir itself is not where dependencies are installed (it’s not on the path)

^ From that doc, it looks like if you go to install the packages via pip (regardless of cache hit or miss), it will use the results from cache if they haven’t expired. I’m not sure if pip will tell you that it found it in cache by default but if not maybe try appending --verbose and see if you can see a difference between restoring the cache and not.

Or you could try to cache the actual installed packages. I think this is someone who figured out something similar. This is for Circle CI, so the syntax is going to be different but my point is just you need to make sure the installed packages are there one way or another. Hope this helps

1 Like

@Snayff,

Looks like, after restoring ‘poetry’ from the cache, it will not be added to the system PATH variable automatically.
You can try using the workflow command “Adding a system path” to do add the tool to ‘poetry’ to the system PATH variable after restoring it.

- name: Locate Pip Cache
      id: pip-cache
      run: echo "::set-output name=dir::$(pip cache dir)"

    - name: Cache dependencies
      uses: actions/cache@v2
      id: cache
      with:
        path: ${{ steps.pip-cache.outputs.dir }}
        key: ${{ runner.os }}-pip-${{ hashFiles('**/poetry.lock') }}
        restore-keys: ${{ runner.os }}-pip-

    - name: Add poetry to PATH
      if: steps.cache.outputs.cache-hit == 'true'
      run: echo "/path/to/poetry" >> $GITHUB_PATH

    - name: Install dependencies
      if: steps.cache.outputs.cache-hit != 'true'
      run: |
        python -m pip install --upgrade poetry
        poetry config virtualenvs.create false
        poetry install --no-root
1 Like

Thank you for the suggestion. I’ve added the “Add poetry to PATH” step, as you suggested, but I get the same result of poetry not being a recognised command.

From the poetry docs, poetry installs to “the poetry tool to Poetry’s bin directory. On Unix it is located at $HOME/.poetry/bin and on Windows at %USERPROFILE%\.poetry\bin .” So I tried that. I also tried using the steps.cache.outputs.dir from the “cache dependencies” step, which outputs as echo c:\users\runneradmin\appdata\local\pip\cache >> $GITHUB_PATH so that doesnt look right, either.

Am I doing it wrong or is it just not what we need?

Edit:
If I add a step that just installs poetry, python -m pip install --upgrade poetry, then the rest of the commands run, they just dont find their modules.