From 6772a96397ff5ec5b22f5025dc0e3bbd46bb8210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliv=C3=A9r=20Falvai?= Date: Thu, 12 Jan 2023 11:40:12 +0100 Subject: [PATCH] Expose cache hit as output (#5) --- README.md | 5 +++- go.mod | 2 +- go.sum | 4 +-- step.yml | 10 +++++++ .../go-steputils/v2/cache/common.go | 4 ++- .../go-steputils/v2/cache/restore.go | 28 +++++++++++++++---- .../go-steputils/v2/cache/save_skip.go | 6 ++-- .../go-steputils/v2/cache/tracker.go | 2 +- vendor/modules.txt | 2 +- 9 files changed, 47 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index a5b81a5..b234ec5 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,10 @@ steps:
Outputs -There are no outputs defined in this step + +| Environment Variable | Description | +| --- | --- | +| `BITRISE_CACHE_HIT` | Indicates if a cache entry was restored. Possible values: - `exact`: Exact cache hit for the first requested cache key - `partial`: Cache hit for a key other than the first - `false` No cache hit, nothing was restored |
## 🙋 Contributing diff --git a/go.mod b/go.mod index 0501b60..6479374 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/bitrise-steplib/steps-restore-gradle-cache go 1.17 require ( - github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.15 + github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.16 github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.13 ) diff --git a/go.sum b/go.sum index bca1842..2bf3d8a 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.15 h1:wG037NV+pS8cEwtalE5K58bmKLyUkU0+4m4IuXjTzmo= -github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.15/go.mod h1:M09BbxYoh6B7KJnXk/yvtuU5nZPh7RQBJGKc1dp+0hQ= +github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.16 h1:WA0QxrFG6ZeBTFsjBsqnCYwlfW0WRDuxGX74a2ffWRw= +github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.16/go.mod h1:M09BbxYoh6B7KJnXk/yvtuU5nZPh7RQBJGKc1dp+0hQ= github.com/bitrise-io/go-utils v1.0.1 h1:e7mepVBkVN1DXRPESNXb0djEw6bxB6B93p/Q74zzcvk= github.com/bitrise-io/go-utils v1.0.1/go.mod h1:ZY1DI+fEpZuFpO9szgDeICM4QbqoWVt0RSY3tRI1heY= github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.13 h1:QtAAfm/FpMDv/PnDxgzylVbbSx21pyl7+5T/ToJnWAQ= diff --git a/step.yml b/step.yml index fe47fcc..5708bf6 100644 --- a/step.yml +++ b/step.yml @@ -47,3 +47,13 @@ inputs: value_options: - "true" - "false" + +outputs: +- BITRISE_CACHE_HIT: + opts: + title: Cache hit + description: |- + Indicates if a cache entry was restored. Possible values: + - `exact`: Exact cache hit for the first requested cache key + - `partial`: Cache hit for a key other than the first + - `false` No cache hit, nothing was restored diff --git a/vendor/github.com/bitrise-io/go-steputils/v2/cache/common.go b/vendor/github.com/bitrise-io/go-steputils/v2/cache/common.go index 398306d..cb29bf6 100644 --- a/vendor/github.com/bitrise-io/go-steputils/v2/cache/common.go +++ b/vendor/github.com/bitrise-io/go-steputils/v2/cache/common.go @@ -7,8 +7,10 @@ import ( "os" ) +const cacheHitEnvVar = "BITRISE_CACHE_HIT" + // We need this prefix because there could be multiple restore steps in one workflow with multiple cache keys -const cacheHitEnvVarPrefix = "BITRISE_CACHE_HIT__" +const cacheHitUniqueEnvVarPrefix = "BITRISE_CACHE_HIT__" func checksumOfFile(path string) (string, error) { hash := sha256.New() diff --git a/vendor/github.com/bitrise-io/go-steputils/v2/cache/restore.go b/vendor/github.com/bitrise-io/go-steputils/v2/cache/restore.go index e5860c8..c82230e 100644 --- a/vendor/github.com/bitrise-io/go-steputils/v2/cache/restore.go +++ b/vendor/github.com/bitrise-io/go-steputils/v2/cache/restore.go @@ -72,7 +72,8 @@ func (r *restorer) Restore(input RestoreCacheInput) error { if errors.Is(err, network.ErrCacheNotFound) { r.logger.Donef("No cache entry found for the provided key") tracker.logRestoreResult(false, "", config.Keys) - return nil + exporter := export.NewExporter(r.cmdFactory) + return exporter.ExportOutput(cacheHitEnvVar, "false") } return fmt.Errorf("download failed: %w", err) } @@ -101,7 +102,7 @@ func (r *restorer) Restore(input RestoreCacheInput) error { r.logger.Donef("Restored archive in %s", extractionTime) tracker.logArchiveExtracted(extractionTime, len(config.Keys)) - err = r.exposeCacheHit(result) + err = r.exposeCacheHit(result, config.Keys) if err != nil { return err } @@ -179,11 +180,27 @@ func (r *restorer) download(config restoreCacheConfig) (downloadResult, error) { return downloadResult{filePath: downloadPath, matchedKey: matchedKey}, nil } -func (r *restorer) exposeCacheHit(result downloadResult) error { - if result.filePath == "" || result.matchedKey == "" { +func (r *restorer) exposeCacheHit(result downloadResult, evaluatedKeys []string) error { + if result.filePath == "" || result.matchedKey == "" || len(evaluatedKeys) == 0 { return nil } + exporter := export.NewExporter(r.cmdFactory) + var cacheHitValue string + if result.matchedKey == evaluatedKeys[0] { + cacheHitValue = "exact" + } else { + cacheHitValue = "partial" + } + err := exporter.ExportOutput(cacheHitEnvVar, cacheHitValue) + if err != nil { + return err + } + err = r.envRepo.Set(cacheHitEnvVar, cacheHitValue) + if err != nil { + return err + } + checksum, err := checksumOfFile(result.filePath) if err != nil { return err @@ -193,8 +210,7 @@ func (r *restorer) exposeCacheHit(result downloadResult) error { r.logger.Debugf("Matched key: %s", result.matchedKey) r.logger.Debugf("Archive checksum: %s", checksum) - envKey := cacheHitEnvVarPrefix + result.matchedKey - exporter := export.NewExporter(r.cmdFactory) + envKey := cacheHitUniqueEnvVarPrefix + result.matchedKey err = exporter.ExportOutput(envKey, checksum) if err != nil { return err diff --git a/vendor/github.com/bitrise-io/go-steputils/v2/cache/save_skip.go b/vendor/github.com/bitrise-io/go-steputils/v2/cache/save_skip.go index 8300040..96babd9 100644 --- a/vendor/github.com/bitrise-io/go-steputils/v2/cache/save_skip.go +++ b/vendor/github.com/bitrise-io/go-steputils/v2/cache/save_skip.go @@ -52,7 +52,7 @@ func (r skipReason) description() string { case reasonNewArchiveChecksumMatch: return "new cache archive is the same as the restored one" case reasonNewArchiveChecksumMismatch: - return "new cache archive doesn't match the restored one" + return "new cache archive contains changed files" default: return "unrecognized skipReason" } @@ -109,8 +109,8 @@ func (s *saver) getCacheHits() map[string]string { envKey := envParts[0] envValue := envParts[1] - if strings.HasPrefix(envKey, cacheHitEnvVarPrefix) { - cacheKey := strings.TrimPrefix(envKey, cacheHitEnvVarPrefix) + if strings.HasPrefix(envKey, cacheHitUniqueEnvVarPrefix) { + cacheKey := strings.TrimPrefix(envKey, cacheHitUniqueEnvVarPrefix) cacheHits[cacheKey] = envValue } } diff --git a/vendor/github.com/bitrise-io/go-steputils/v2/cache/tracker.go b/vendor/github.com/bitrise-io/go-steputils/v2/cache/tracker.go index 83504aa..bdf680d 100644 --- a/vendor/github.com/bitrise-io/go-steputils/v2/cache/tracker.go +++ b/vendor/github.com/bitrise-io/go-steputils/v2/cache/tracker.go @@ -20,7 +20,7 @@ func newStepTracker(stepId string, envRepo env.Repository, logger log.Logger) st "build_slug": envRepo.Get("BITRISE_BUILD_SLUG"), "app_slug": envRepo.Get("BITRISE_APP_SLUG"), "workflow": envRepo.Get("BITRISE_TRIGGERED_WORKFLOW_ID"), - "is_pr_build": envRepo.Get("IS_PR") == "true", + "is_pr_build": envRepo.Get("PR") == "true", } return stepTracker{ tracker: analytics.NewDefaultTracker(logger, p), diff --git a/vendor/modules.txt b/vendor/modules.txt index f774153..8468ffa 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,4 +1,4 @@ -# github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.15 +# github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.16 ## explicit; go 1.17 github.com/bitrise-io/go-steputils/v2/cache github.com/bitrise-io/go-steputils/v2/cache/compression