Skip to content

Partially matching a key and not setting cache-hit to true is just ..... stupid #1808

Backlog

Description

@RazorBest

Why would you ever do that? It's not intuitive. The reference recommends using

- if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }}

To check that the cache was hit

When an exact match is found for a cache for the specified key, the cache-hit output is set to true.

Oh, thank you for nothing.

Activity

  1. RazorBest commented on Nov 27, 2026

    @RazorBest
    Author

    For whoever's looking for a solution, here's what I found.

    You need to replace this:

          - uses: actions/cache@v6
            id: my-step-cache
            with:
              path: <path-to-cache>
              key: <cache-key>
    
          - name: Build
            # Issue: if partial hit happens, `path` is still written, but cache-hit is set to false. So, the build still executes 
            if: steps.my-step-cache.outputs.cache-hit != 'true'
            run: <build command>
    

    With this:

          - uses: actions/cache/restore@v6
            id: my-step-cache
            with:
              path: <path-to-cache>
              key: <cache-key>
              restore-keys: |
                <prefix1 of cache-key>
                <prefix2 of cache-key>
                ...
    
          - name: Record whether any cache was restored
            id: my-step-cache-status
            env:
              MATCHED_KEY: ${{ steps.my-step-cache.outputs.cache-matched-key }}
            run: |
              if [ -n "$MATCHED_KEY" ]; then
                echo "restored=true" >> "$GITHUB_OUTPUT"
              else
                echo "restored=false" >> "$GITHUB_OUTPUT"
              fi
    
          - name: Build
            if: steps.my-step-cache-status.outputs.restored != 'true'
            run: <build command>
    
          # Compared to actions/cache, restore doesn't automatically write to the cache
          - uses: actions/cache/save@v6
            if: steps.my-step-cache-status.outputs.restored != 'true'
            with:
              path: <path-to-cache>
              key: <cache-key>
    

    The restore step sets the restored variable to true for both exact and partial matches. However, you need to explicitly enumerate the prefixes of the key.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions