Skip to content

Commit 852959e

Browse files
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>
1 parent 27eb79b commit 852959e

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

cmd/root.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,16 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
562562
ContainerNetworkMode: docker_container.NetworkMode(input.networkName),
563563
}
564564
if input.useNewActionCache {
565-
config.ActionCache = &runner.GoGitActionCache{
566-
Path: config.ActionCacheDir,
565+
if input.actionOfflineMode {
566+
config.ActionCache = &runner.GoGitActionCacheOfflineMode{
567+
Parent: runner.GoGitActionCache{
568+
Path: config.ActionCacheDir,
569+
},
570+
}
571+
} else {
572+
config.ActionCache = &runner.GoGitActionCache{
573+
Path: config.ActionCacheDir,
574+
}
567575
}
568576
}
569577
r, err := runner.New(config)
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package runner
2+
3+
import (
4+
"context"
5+
"io"
6+
"path"
7+
8+
git "github.com/go-git/go-git/v5"
9+
"github.com/go-git/go-git/v5/plumbing"
10+
)
11+
12+
type GoGitActionCacheOfflineMode struct {
13+
Parent GoGitActionCache
14+
}
15+
16+
func (c GoGitActionCacheOfflineMode) Fetch(ctx context.Context, cacheDir, url, ref, token string) (string, error) {
17+
sha, fetchErr := c.Parent.Fetch(ctx, cacheDir, url, ref, token)
18+
gitPath := path.Join(c.Parent.Path, safeFilename(cacheDir)+".git")
19+
gogitrepo, err := git.PlainOpen(gitPath)
20+
if err != nil {
21+
return "", fetchErr
22+
}
23+
refName := plumbing.ReferenceName("refs/action-cache-offline/" + ref)
24+
r, err := gogitrepo.Reference(refName, true)
25+
if fetchErr == nil {
26+
if err != nil || sha != r.Hash().String() {
27+
if err == nil {
28+
refName = r.Name()
29+
}
30+
ref := plumbing.NewHashReference(refName, plumbing.NewHash(sha))
31+
_ = gogitrepo.Storer.SetReference(ref)
32+
}
33+
} else if err == nil {
34+
return r.Hash().String(), nil
35+
}
36+
return sha, fetchErr
37+
}
38+
39+
func (c GoGitActionCacheOfflineMode) GetTarArchive(ctx context.Context, cacheDir, sha, includePrefix string) (io.ReadCloser, error) {
40+
return c.Parent.GetTarArchive(ctx, cacheDir, sha, includePrefix)
41+
}

0 commit comments

Comments
 (0)