-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
27eb79b
commit 852959e
Showing
2 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |