actions / cache

Cache dependencies and build outputs in GitHub Actions
MIT License
4.45k stars 1.18k forks source link

Cache returning a hit with 0mb (workflow_dispatch) #1140

Closed anthonyc4 closed 11 months ago

anthonyc4 commented 1 year ago

We have noticed a problem that if we used workflow_dispatch that, not always, the cache would return a hit with 0mb of content. Since we are using this to cache node_modules, it means the rest of the workflow will not work. We are using GIthub Enterprise which apparently does not have the same API to be able to burst a cache element.

Our jobs are running on pull_request and can be dispatched by the user. Running via pull_request does seem to work

Here is a log:

Run actions/cache@v3
  with:
    path: node_modules
    key: deps-node-modules-[9](https://ghe.coxautoinc.com/DigitalRetailing/dr-lambda-template-cdk/runs/3066999?check_suite_focus=true#step:12:9)eab895e99704e2917f579a2481c449dd1fcb55a8bba04654ecd3ade3aede607
    enableCrossOsArchive: false
    fail-on-cache-miss: false
    lookup-only: false
Cache Size: ~0 MB (72000 B)
/usr/bin/tar -xf /runner/_work/_temp/4c7570bb-c5f8-4204-81f0-b296f0abe849/cache.tzst -P -C /runner/_work/dr-lambda-template-cdk/dr-lambda-template-cdk --use-compress-program unzstd
Cache restored successfully
Cache restored from key: deps-node-modules-9eab895e99704e29[17](https://ghe.coxautoinc.com/DigitalRetailing/dr-lambda-template-cdk/runs/3066999?check_suite_focus=true#step:12:17)f579a2481c449dd1fcb55a8bba04654ecd3ade3aede607

While doing some debugging, when the cache was 0mb, we saw the output looked like this:

image

In this case, the package-lock is correct, but for some reason, the node_modules are not retrieved from cache. Again, this only was happening on workflow_dispatch

We were able to create a work around that runs right after theactions/cache that looks like this (but it is a hack). This will run the npm ci if the cache size is less than 1mb.

    - name: Cache dependencies
      id: cache
      uses: actions/cache@v3
      with:
        path: ${{ inputs.NODE_MODULES_PATH }}
        key: ${{ inputs.CACHE_KEY_PREFIX }}-${{ hashFiles(inputs.PACKAGE_LOCK_LOCATION) }}

    - name: 'Install Dependencies'
      shell: bash
      env:
        CMD: ${{ inputs.NPM_CI_COMMAND }}
        WORKING_DIRECTORY: ${{ inputs.WORKING_DIRECTORY }}
        NODE_MODULES_PATH: ${{ inputs.NODE_MODULES_PATH }}
        CACHE_HIT: ${{ steps.cache.outputs.cache-hit }}
      run: |
        if [[ "$CACHE_HIT" == "true" ]]; then
          OUTPUT=$(du -sh "$NODE_MODULES_PATH")
          echo "$OUTPUT"
          NODE_MODULES_SIZE=$(echo "$OUTPUT" | cut -f1)
          echo "Size of $NODE_MODULES_PATH=$NODE_MODULES_SIZE"

          if [[ "$NODE_MODULES_SIZE" != *"M" ]]; then
            echo "Cache size is too small, installing dependencies"
            cd $WORKING_DIRECTORY
            eval $CMD
          else
            echo "Cache size has been met."
          fi
        else
          echo "$NODE_MODULES_PATH does not exist"
          cd $WORKING_DIRECTORY
          eval $CMD
        fi
github-actions[bot] commented 11 months ago

This issue is stale because it has been open for 200 days with no activity. Leave a comment to avoid closing this issue in 5 days.

github-actions[bot] commented 11 months ago

This issue was closed because it has been inactive for 5 days since being marked as stale.