Skip to content

cache doesn't work with github workspace directory #833

Description

@erwan-joly

Hi, I'm trying to cache the whole workspace to fetch it between jobs.
Unfortunately it seems like the local directory can not be cached

Using . doesn't work (it cache 22B)

- name: Cache build folder
        uses: actions/cache@v3
        with:
          key: cs-repo-${{ github.run_number }}-${{ github.run_attempt }}
          path: .

Using ${{ github.workspace }} doesn't work (it also cache 22B)

- name: Cache build folder
        uses: actions/cache@v3
        with:
          key: cs-repo-${{ github.run_number }}-${{ github.run_attempt }}
          path: ${{ github.workspace }}

the only solution I found which work is to list all the files/directory

- name: Cache build folder
        uses: actions/cache@v3
        with:
          key: cs-repo-${{ github.run_number }}-${{ github.run_attempt }}
          path: |
              file1
              file2
              dir1
              dir2

Activity

  1. aparna-ravindra commented on Jun 24, 2022

    @aparna-ravindra
    Contributor

    Thank you for the report. I was able to reproduce this. From the first look, it appears that there is a bug while converting the cache paths to relative paths for compression in the toolkit. I will debug the cause and get back.

    Contribution are welcome as this will mostly require changes in actions/toolkit and actions/cache.

  2. erwan-joly commented on Aug 4, 2022

    @erwan-joly
    Author

    It doesn't seems to be fixed for some reason :/
    I've tested with actions/cache@v3.0.1 both the two version with . and env variable and it still cache 22B @pdotl

  3. lvpx commented on Aug 4, 2022

    @lvpx
    Contributor

    Hi @erwan-joly , the above version is for actions/cache npm module and not for the actions/cache action. We are yet to merge toolkit changes into the actions/cache. Once done, you should be able to test and use the same. I'll post an update here once done.

  4. lvpx commented on Aug 5, 2022

    @lvpx
    Contributor

    @erwan-joly ,please test now with the latest version of actions/cache using actions/cache@v3.

  5. erwan-joly commented on Aug 5, 2022

    @erwan-joly
    Author

    @pdotl I confirm it's now fixed 💯

  6. khelkun commented on Feb 23, 2023

    @khelkun

    One should also consider this issue if he has troubles with github.worspace context and cache action as I did:

    github.workspace === /home/runner/work/testContainers/testContainers
    GITHUB_WORKSPACE === /__w/testContainers/testContainers
    runner.workspace === /home/runner/work/testContainers
    RUNNER_WORKSPACE === /__w/testContainers
    

    My build job relies on ${{ github.workspace }} to output its result and I want to cache this result and restore it in my publish job. Here is how I made this works:

    name: Build
    
    on:
      push:
        tags:
          - '*'
      workflow_dispatch:
    
    jobs:
      build:
        runs-on: ubuntu-latest
        container:
          image: mstorsjo/llvm-mingw:latest
        steps:
          - name: build stuff
            run: |
              echo "GITHUB_WORKSPACE=$GITHUB_WORKSPACE"
              echo "github.workspace=${{ github.workspace }}"
              # echo curent working dir which is $GITHUB_WORKSPACE and not ${{ github.workspace }}.
              echo "current working dir:"
              pwd
    
              # cd to build dir.
              # for the example, creates a my_build_dir and cd to it.
              cd my_build_dir
    
              # run build command that generates the buil result in ${{ github.workspace }}/output.
              # for the example, creates the output dir and touch a result.out file in it.
              mkdir -p ${{ github.workspace }}/output
              touch ${{ github.workspace }}/output/result.out
    
              # back to $GITHUB_WORKSPACE
              cd ..
              cp -r ${{ github.workspace }}/output $GITHUB_WORKSPACE
              ls -l $GITHUB_WORKSPACE
    
          - name: Cache build output
            id: cache-save-build-ouput
            uses: actions/cache/save@v3
            with:
              path: |
                ./output
              key: ${{ runner.os }}-build-output-run_${{ github.run_id }}
              enableCrossOsArchive: true
    
    
      publish:
        runs-on: ubuntu-latest
        image: mstorsjo/llvm-mingw:latest
        needs: build
        steps:
          - name: Restore build ouput cache
            uses: actions/cache/restore@v3
            id: cache-restore-build-ouput
            with:
              path: |
                ./output
              key: ${{ runner.os }}-build-output-run_${{ github.run_id }}
              enableCrossOsArchive: true
              fail-on-cache-miss: true
    
          - name: archive
            run: |
              echo "GITHUB_WORKSPACE=$GITHUB_WORKSPACE"
              echo "github.workspace=${{ github.workspace }}"
              # echo curent working dir which is $GITHUB_WORKSPACE and not ${{ github.workspace }}.
              echo "current working dir:"
              pwd
    
              echo "restored cache output dir:"
              ls -l ./ouput          

    Note that restoring a cache in a container image different from the one that saved the cache may fail because the comrpession tool (tar) may be different. For example using image: alpine:latest container in my publish job fails this way:

    /bin/tar: unrecognized option: P
    

    On the other hand, using no container on the my publish job fails this way:

    ##[debug]No matching cache found for cache key 'Linux-build-output-run_4252492458', version '864e6dbc1f60e04c96ce9a7e3cc1dc27a256109377aae3de699034043992ea42 and scope refs/tags/v4.2.1-full. There exist one or more cache(s) with similar key but they have different version or scope. See more info on cache matching here: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key 
    ##[debug]Other caches with similar key:
    ##[debug]Cache Key: Linux-build-output-run_4252492[45](https://github.com/khelkun/ffmpeg-wos-arm64-build/actions/runs/4252492458/jobs/7396194685#step:2:46)8, Cache Version: ea8764569ab4d245f2db7a85367253cd303504ee4289[46](https://github.com/khelkun/ffmpeg-wos-arm64-build/actions/runs/4252492458/jobs/7396194685#step:2:47)df7d21a697b0c7812f, Cache Scope: refs/tags/v4.2.1-full, Cache Created: 2023-02-23T12:16:30.7133333Z
    ##[debug]Failed to delete archive: Error: ENOENT: no such file or directory, unlink ''
    Error: Failed to restore cache entry. Exiting as fail-on-cache-miss is set. Input key: Linux-build-output-run_4252492458
    

    And using the enableCrossOsArchive: true in the actions/cache/restore@v3 does not fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions