Set stores a new cache entry, preserving any already-cached inputs when the SHA is unchanged. If the SHA changes (e.g. a moving tag points to a new commit), cached inputs are cleared to stay consistent with the newly-pinned commit.
(repo, version, sha string)
| 430 | // is unchanged. If the SHA changes (e.g. a moving tag points to a new commit), |
| 431 | // cached inputs are cleared to stay consistent with the newly-pinned commit. |
| 432 | func (c *ActionCache) Set(repo, version, sha string) { |
| 433 | key := formatActionCacheKey(repo, version) |
| 434 | |
| 435 | // Check if there are existing entries with the same repo+SHA but different version |
| 436 | for existingKey, entry := range c.Entries { |
| 437 | if entry.Repo == repo && entry.SHA == sha && entry.Version != version { |
| 438 | // Truncate SHA for logging (handle short SHAs in tests) |
| 439 | shortSHA := sha |
| 440 | if len(sha) > 8 { |
| 441 | shortSHA = sha[:8] |
| 442 | } |
| 443 | actionCacheLog.Printf("WARNING: Adding cache entry %s with SHA %s that already exists as %s", |
| 444 | key, shortSHA, existingKey) |
| 445 | actionCacheLog.Printf("This may cause version comment flipping in lock files. Consider using consistent version tags.") |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | actionCacheLog.Printf("Setting cache entry: key=%s, sha=%s", key, sha) |
| 450 | |
| 451 | // Preserve previously-cached inputs only when the SHA is unchanged. If the SHA |
| 452 | // changes (e.g. for a moving tag that now points to a new commit), drop any |
| 453 | // existing inputs so they stay consistent with the pinned commit. |
| 454 | existing := c.Entries[key] |
| 455 | var inputs map[string]*ActionYAMLInput |
| 456 | var description string |
| 457 | var releasedAt *time.Time |
| 458 | if existing.SHA == sha { |
| 459 | inputs = existing.Inputs |
| 460 | description = existing.ActionDescription |
| 461 | releasedAt = existing.ReleasedAt |
| 462 | } else if existing.SHA != "" { |
| 463 | // Log when an existing entry's SHA is being changed (covers both the case |
| 464 | // where cached inputs exist and where they don't, for consistent observability). |
| 465 | actionCacheLog.Printf("Clearing cached inputs for key=%s due to SHA change (%s -> %s)", key, existing.SHA, sha) |
| 466 | } |
| 467 | c.Entries[key] = ActionCacheEntry{ |
| 468 | Repo: repo, |
| 469 | Version: version, |
| 470 | SHA: sha, |
| 471 | ReleasedAt: releasedAt, |
| 472 | Inputs: inputs, |
| 473 | ActionDescription: description, |
| 474 | } |
| 475 | c.dirty = true // Mark cache as modified |
| 476 | } |
| 477 | |
| 478 | // GetInputs retrieves the cached action inputs for the given repo and version. |
| 479 | // Returns the inputs map and true if cached inputs exist, otherwise nil and false. |