From 7065a7e228523bbc71d5406e0e44a34149ddafb9 Mon Sep 17 00:00:00 2001 From: ChristopherHX Date: Mon, 12 Feb 2024 13:51:37 +0100 Subject: [PATCH] feat: offline mode for new action cache (#2173) * Try fetch update of the action, otherwise use cached version Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- act/runner/action_cache_offline_mode.go | 41 +++++++++++++++++++++++++ cmd/root.go | 12 ++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 act/runner/action_cache_offline_mode.go diff --git a/act/runner/action_cache_offline_mode.go b/act/runner/action_cache_offline_mode.go new file mode 100644 index 00000000..c4a572a4 --- /dev/null +++ b/act/runner/action_cache_offline_mode.go @@ -0,0 +1,41 @@ +package runner + +import ( + "context" + "io" + "path" + + git "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" +) + +type GoGitActionCacheOfflineMode struct { + Parent GoGitActionCache +} + +func (c GoGitActionCacheOfflineMode) Fetch(ctx context.Context, cacheDir, url, ref, token string) (string, error) { + sha, fetchErr := c.Parent.Fetch(ctx, cacheDir, url, ref, token) + gitPath := path.Join(c.Parent.Path, safeFilename(cacheDir)+".git") + gogitrepo, err := git.PlainOpen(gitPath) + if err != nil { + return "", fetchErr + } + refName := plumbing.ReferenceName("refs/action-cache-offline/" + ref) + r, err := gogitrepo.Reference(refName, true) + if fetchErr == nil { + if err != nil || sha != r.Hash().String() { + if err == nil { + refName = r.Name() + } + ref := plumbing.NewHashReference(refName, plumbing.NewHash(sha)) + _ = gogitrepo.Storer.SetReference(ref) + } + } else if err == nil { + return r.Hash().String(), nil + } + return sha, fetchErr +} + +func (c GoGitActionCacheOfflineMode) GetTarArchive(ctx context.Context, cacheDir, sha, includePrefix string) (io.ReadCloser, error) { + return c.Parent.GetTarArchive(ctx, cacheDir, sha, includePrefix) +} diff --git a/cmd/root.go b/cmd/root.go index c5d4ce0a..04e593aa 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -617,8 +617,16 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str ContainerNetworkMode: docker_container.NetworkMode(input.networkName), } if input.useNewActionCache { - config.ActionCache = &runner.GoGitActionCache{ - Path: config.ActionCacheDir, + if input.actionOfflineMode { + config.ActionCache = &runner.GoGitActionCacheOfflineMode{ + Parent: runner.GoGitActionCache{ + Path: config.ActionCacheDir, + }, + } + } else { + config.ActionCache = &runner.GoGitActionCache{ + Path: config.ActionCacheDir, + } } } r, err := runner.New(config)