Skip to content

Commit 9142ed9

Browse files
feat: improve new action cache logging (#2474)
* feat: improve new action cache logging * Test logging failure cases --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 5ffec84 commit 9142ed9

4 files changed

+108
-11
lines changed

pkg/runner/action_cache.go

+22-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/go-git/go-git/v5/plumbing/object"
2020
"github.com/go-git/go-git/v5/plumbing/transport"
2121
"github.com/go-git/go-git/v5/plumbing/transport/http"
22+
"github.com/nektos/act/pkg/common"
2223
)
2324

2425
type ActionCache interface {
@@ -31,17 +32,23 @@ type GoGitActionCache struct {
3132
}
3233

3334
func (c GoGitActionCache) Fetch(ctx context.Context, cacheDir, url, ref, token string) (string, error) {
35+
logger := common.Logger(ctx)
36+
3437
gitPath := path.Join(c.Path, safeFilename(cacheDir)+".git")
38+
39+
logger.Infof("GoGitActionCache fetch %s with ref %s at %s", url, ref, gitPath)
40+
3541
gogitrepo, err := git.PlainInit(gitPath, true)
3642
if errors.Is(err, git.ErrRepositoryAlreadyExists) {
43+
logger.Debugf("GoGitActionCache cache hit %s with ref %s at %s", url, ref, gitPath)
3744
gogitrepo, err = git.PlainOpen(gitPath)
3845
}
3946
if err != nil {
40-
return "", err
47+
return "", fmt.Errorf("GoGitActionCache failed to open bare git %s with ref %s at %s: %w", url, ref, gitPath, err)
4148
}
4249
tmpBranch := make([]byte, 12)
4350
if _, err := rand.Read(tmpBranch); err != nil {
44-
return "", err
51+
return "", fmt.Errorf("GoGitActionCache failed to generate random tmp branch %s with ref %s at %s: %w", url, ref, gitPath, err)
4552
}
4653
branchName := hex.EncodeToString(tmpBranch)
4754

@@ -59,7 +66,7 @@ func (c GoGitActionCache) Fetch(ctx context.Context, cacheDir, url, ref, token s
5966
},
6067
})
6168
if err != nil {
62-
return "", err
69+
return "", fmt.Errorf("GoGitActionCache failed to create remote %s with ref %s at %s: %w", url, ref, gitPath, err)
6370
}
6471
defer func() {
6572
_ = gogitrepo.DeleteBranch(branchName)
@@ -71,12 +78,13 @@ func (c GoGitActionCache) Fetch(ctx context.Context, cacheDir, url, ref, token s
7178
Auth: auth,
7279
Force: true,
7380
}); err != nil {
74-
return "", err
81+
return "", fmt.Errorf("GoGitActionCache failed to fetch %s with ref %s at %s: %w", url, ref, gitPath, err)
7582
}
7683
hash, err := gogitrepo.ResolveRevision(plumbing.Revision(branchName))
7784
if err != nil {
78-
return "", err
85+
return "", fmt.Errorf("GoGitActionCache failed to resolve sha %s with ref %s at %s: %w", url, ref, gitPath, err)
7986
}
87+
logger.Infof("GoGitActionCache fetch %s with ref %s at %s resolved to %s", url, ref, gitPath, hash.String())
8088
return hash.String(), nil
8189
}
8290

@@ -119,22 +127,27 @@ func (g *GitFileInfo) Sys() any {
119127
}
120128

121129
func (c GoGitActionCache) GetTarArchive(ctx context.Context, cacheDir, sha, includePrefix string) (io.ReadCloser, error) {
130+
logger := common.Logger(ctx)
131+
122132
gitPath := path.Join(c.Path, safeFilename(cacheDir)+".git")
133+
134+
logger.Infof("GoGitActionCache get content %s with sha %s subpath %s at %s", cacheDir, sha, includePrefix, gitPath)
135+
123136
gogitrepo, err := git.PlainOpen(gitPath)
124137
if err != nil {
125-
return nil, err
138+
return nil, fmt.Errorf("GoGitActionCache failed to open bare git %s with sha %s subpath %s at %s: %w", cacheDir, sha, includePrefix, gitPath, err)
126139
}
127140
commit, err := gogitrepo.CommitObject(plumbing.NewHash(sha))
128141
if err != nil {
129-
return nil, err
142+
return nil, fmt.Errorf("GoGitActionCache failed to get commit %s with sha %s subpath %s at %s: %w", cacheDir, sha, includePrefix, gitPath, err)
130143
}
131144
t, err := commit.Tree()
132145
if err != nil {
133-
return nil, err
146+
return nil, fmt.Errorf("GoGitActionCache failed to open git tree %s with sha %s subpath %s at %s: %w", cacheDir, sha, includePrefix, gitPath, err)
134147
}
135148
files, err := commit.Files()
136149
if err != nil {
137-
return nil, err
150+
return nil, fmt.Errorf("GoGitActionCache failed to list files %s with sha %s subpath %s at %s: %w", cacheDir, sha, includePrefix, gitPath, err)
138151
}
139152
rpipe, wpipe := io.Pipe()
140153
// Interrupt io.Copy using ctx

pkg/runner/action_cache_offline_mode.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@ import (
77

88
git "github.com/go-git/go-git/v5"
99
"github.com/go-git/go-git/v5/plumbing"
10+
"github.com/nektos/act/pkg/common"
1011
)
1112

1213
type GoGitActionCacheOfflineMode struct {
1314
Parent GoGitActionCache
1415
}
1516

1617
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+
logger := common.Logger(ctx)
19+
1820
gitPath := path.Join(c.Parent.Path, safeFilename(cacheDir)+".git")
21+
22+
logger.Infof("GoGitActionCacheOfflineMode fetch content %s with ref %s at %s", url, ref, gitPath)
23+
24+
sha, fetchErr := c.Parent.Fetch(ctx, cacheDir, url, ref, token)
1925
gogitrepo, err := git.PlainOpen(gitPath)
2026
if err != nil {
2127
return "", fetchErr

pkg/runner/action_cache_test.go

+70-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestActionCache(t *testing.T) {
5252
},
5353
}
5454
for _, c := range refs {
55-
t.Run(c.Name, func(t *testing.T) {
55+
t.Run(c.Name, func(_ *testing.T) {
5656
sha, err := cache.Fetch(ctx, c.CacheDir, c.Repo, c.Ref, "")
5757
if !a.NoError(err) || !a.NotEmpty(sha) {
5858
return
@@ -75,3 +75,72 @@ func TestActionCache(t *testing.T) {
7575
})
7676
}
7777
}
78+
79+
func TestActionCacheFailures(t *testing.T) {
80+
a := assert.New(t)
81+
cache := &GoGitActionCache{
82+
Path: os.TempDir(),
83+
}
84+
ctx := context.Background()
85+
cacheDir := "nektos/act-test-actions"
86+
repo := "https://github.com/nektos/act-test-actions-not-exist"
87+
repoExist := "https://github.com/nektos/act-test-actions"
88+
refs := []struct {
89+
Name string
90+
CacheDir string
91+
Repo string
92+
Ref string
93+
}{
94+
{
95+
Name: "Fetch Branch Name",
96+
CacheDir: cacheDir,
97+
Repo: repo,
98+
Ref: "main",
99+
},
100+
{
101+
Name: "Fetch Branch Name Absolutely",
102+
CacheDir: cacheDir,
103+
Repo: repo,
104+
Ref: "refs/heads/main",
105+
},
106+
{
107+
Name: "Fetch HEAD",
108+
CacheDir: cacheDir,
109+
Repo: repo,
110+
Ref: "HEAD",
111+
},
112+
{
113+
Name: "Fetch Sha",
114+
CacheDir: cacheDir,
115+
Repo: repo,
116+
Ref: "de984ca37e4df4cb9fd9256435a3b82c4a2662b1",
117+
},
118+
{
119+
Name: "Fetch Branch Name no existing",
120+
CacheDir: cacheDir,
121+
Repo: repoExist,
122+
Ref: "main2",
123+
},
124+
{
125+
Name: "Fetch Branch Name Absolutely no existing",
126+
CacheDir: cacheDir,
127+
Repo: repoExist,
128+
Ref: "refs/heads/main2",
129+
},
130+
{
131+
Name: "Fetch Sha no existing",
132+
CacheDir: cacheDir,
133+
Repo: repoExist,
134+
Ref: "de984ca37e4df4cb9fd9256435a3b82c4a2662b2",
135+
},
136+
}
137+
for _, c := range refs {
138+
t.Run(c.Name, func(t *testing.T) {
139+
_, err := cache.Fetch(ctx, c.CacheDir, c.Repo, c.Ref, "")
140+
t.Logf("%s\n", err)
141+
if !a.Error(err) {
142+
return
143+
}
144+
})
145+
}
146+
}

pkg/runner/local_repository_cache.go

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path/filepath"
1313
"strings"
1414

15+
"github.com/nektos/act/pkg/common"
1516
"github.com/nektos/act/pkg/filecollector"
1617
)
1718

@@ -22,22 +23,29 @@ type LocalRepositoryCache struct {
2223
}
2324

2425
func (l *LocalRepositoryCache) Fetch(ctx context.Context, cacheDir, url, ref, token string) (string, error) {
26+
logger := common.Logger(ctx)
27+
logger.Debugf("LocalRepositoryCache fetch %s with ref %s", url, ref)
2528
if dest, ok := l.LocalRepositories[fmt.Sprintf("%s@%s", url, ref)]; ok {
29+
logger.Infof("LocalRepositoryCache matched %s with ref %s to %s", url, ref, dest)
2630
l.CacheDirCache[fmt.Sprintf("%s@%s", cacheDir, ref)] = dest
2731
return ref, nil
2832
}
2933
if purl, err := goURL.Parse(url); err == nil {
3034
if dest, ok := l.LocalRepositories[fmt.Sprintf("%s@%s", strings.TrimPrefix(purl.Path, "/"), ref)]; ok {
35+
logger.Infof("LocalRepositoryCache matched %s with ref %s to %s", url, ref, dest)
3136
l.CacheDirCache[fmt.Sprintf("%s@%s", cacheDir, ref)] = dest
3237
return ref, nil
3338
}
3439
}
40+
logger.Infof("LocalRepositoryCache not matched %s with Ref %s", url, ref)
3541
return l.Parent.Fetch(ctx, cacheDir, url, ref, token)
3642
}
3743

3844
func (l *LocalRepositoryCache) GetTarArchive(ctx context.Context, cacheDir, sha, includePrefix string) (io.ReadCloser, error) {
45+
logger := common.Logger(ctx)
3946
// sha is mapped to ref in fetch if there is a local override
4047
if dest, ok := l.CacheDirCache[fmt.Sprintf("%s@%s", cacheDir, sha)]; ok {
48+
logger.Infof("LocalRepositoryCache read cachedir %s with ref %s and subpath %s from %s", cacheDir, sha, includePrefix, dest)
4149
srcPath := filepath.Join(dest, includePrefix)
4250
buf := &bytes.Buffer{}
4351
tw := tar.NewWriter(buf)
@@ -87,5 +95,6 @@ func (l *LocalRepositoryCache) GetTarArchive(ctx context.Context, cacheDir, sha,
8795
}
8896
return io.NopCloser(buf), nil
8997
}
98+
logger.Infof("LocalRepositoryCache not matched cachedir %s with Ref %s and subpath %s", cacheDir, sha, includePrefix)
9099
return l.Parent.GetTarArchive(ctx, cacheDir, sha, includePrefix)
91100
}

0 commit comments

Comments
 (0)