Skip to content

Commit 8f57114

Browse files
authored
Merge pull request #212 from gitpod-io/aledbf/s3-upload
Do not return an error uploading to remote cache
2 parents 3ebc192 + ca42381 commit 8f57114

File tree

4 files changed

+51
-22
lines changed

4 files changed

+51
-22
lines changed

pkg/leeway/build.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,13 @@ func Build(pkg *Package, opts ...BuildOption) (err error) {
533533

534534
buildErr := pkg.build(ctx)
535535

536+
// Check for build errors immediately and return if there are any
537+
if buildErr != nil {
538+
// We deliberately swallow the target package build error as that will have already been reported using the reporter.
539+
return xerrors.Errorf("build failed")
540+
}
541+
542+
// Only proceed with cache upload if build succeeded
536543
pkgsToUpload := ctx.GetNewPackagesForCache()
537544
// Convert []*Package to []cache.Package
538545
pkgsToUploadCache := make([]cache.Package, len(pkgsToUpload))
@@ -541,11 +548,6 @@ func Build(pkg *Package, opts ...BuildOption) (err error) {
541548
}
542549

543550
cacheErr := ctx.RemoteCache.Upload(context.Background(), ctx.LocalCache, pkgsToUploadCache)
544-
545-
if buildErr != nil {
546-
// We deliberately swallow the target pacakge build error as that will have already been reported using the reporter.
547-
return xerrors.Errorf("build failed")
548-
}
549551
if cacheErr != nil {
550552
return cacheErr
551553
}

pkg/leeway/cache/remote/s3.go

+43-6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ func (s *S3Cache) processPackages(ctx context.Context, pkgs []cache.Package, fn
114114
}
115115

116116
if len(errs) > 0 {
117+
// For upload operations, we want to log errors but not fail the entire build
118+
// This is determined by the caller (Upload vs Download vs ExistingPackages)
119+
log.WithField("errorCount", len(errs)).Debug("Some packages had errors during processing")
117120
return fmt.Errorf("multiple errors occurred: %v", errs)
118121
}
119122

@@ -326,19 +329,42 @@ func (s *S3Cache) Download(ctx context.Context, dst cache.LocalCache, pkgs []cac
326329

327330
// Upload implements RemoteCache
328331
func (s *S3Cache) Upload(ctx context.Context, src cache.LocalCache, pkgs []cache.Package) error {
329-
return s.processPackages(ctx, pkgs, func(ctx context.Context, p cache.Package) error {
332+
var uploadErrors []error
333+
334+
err := s.processPackages(ctx, pkgs, func(ctx context.Context, p cache.Package) error {
330335
localPath, exists := src.Location(p)
331336
if !exists {
332-
return fmt.Errorf("package %s not found in local cache", p.FullName())
337+
log.WithField("package", p.FullName()).Warn("package not found in local cache - skipping upload")
338+
return nil // Skip but don't fail everything
333339
}
334340

335341
key := filepath.Base(localPath)
336342
if err := s.storage.UploadObject(ctx, key, localPath); err != nil {
337-
return fmt.Errorf("failed to upload package %s: %w", p.FullName(), err)
343+
log.WithError(err).WithFields(log.Fields{
344+
"package": p.FullName(),
345+
"key": key,
346+
}).Warn("failed to upload package to remote cache - continuing")
347+
uploadErrors = append(uploadErrors, fmt.Errorf("package %s: %w", p.FullName(), err))
348+
return nil // Don't fail the entire operation
338349
}
339350

351+
log.WithFields(log.Fields{
352+
"package": p.FullName(),
353+
"key": key,
354+
}).Debug("successfully uploaded package to remote cache")
340355
return nil
341356
})
357+
358+
if err != nil {
359+
log.WithError(err).Warn("errors occurred during upload to remote cache - continuing")
360+
// Don't return the error to allow the build to continue
361+
}
362+
363+
if len(uploadErrors) > 0 {
364+
log.WithField("errorCount", len(uploadErrors)).Warn("some packages failed to upload to remote cache - continuing with build")
365+
}
366+
367+
return nil // Always return nil to allow the build to continue
342368
}
343369

344370
// s3ClientAPI is a subset of the S3 client interface we need
@@ -475,6 +501,7 @@ func (s *S3Storage) GetObject(ctx context.Context, key string, dest string) (int
475501
func (s *S3Storage) UploadObject(ctx context.Context, key string, src string) error {
476502
file, err := os.Open(src)
477503
if err != nil {
504+
log.WithError(err).WithField("key", key).Warn("failed to open source file for upload")
478505
return fmt.Errorf("failed to open source file: %w", err)
479506
}
480507
defer file.Close()
@@ -492,10 +519,20 @@ func (s *S3Storage) UploadObject(ctx context.Context, key string, src string) er
492519
_, err = uploader.Upload(ctx, input)
493520
if err != nil {
494521
var apiErr smithy.APIError
495-
if errors.As(err, &apiErr) && apiErr.ErrorCode() == "Forbidden" {
496-
log.WithError(err).Warnf("permission denied while uploading object %s to S3 - continuing", key)
497-
return nil
522+
if errors.As(err, &apiErr) {
523+
if apiErr.ErrorCode() == "Forbidden" {
524+
log.WithError(err).Warnf("permission denied while uploading object %s to S3 - continuing", key)
525+
return nil
526+
}
527+
// Handle other API errors as warnings too
528+
log.WithError(err).WithFields(log.Fields{
529+
"key": key,
530+
"errorCode": apiErr.ErrorCode(),
531+
}).Warn("S3 API error while uploading object - continuing")
532+
return fmt.Errorf("S3 API error: %w", err)
498533
}
534+
// Handle non-API errors
535+
log.WithError(err).WithField("key", key).Warn("failed to upload object - continuing")
499536
return fmt.Errorf("failed to upload object: %w", err)
500537
}
501538

pkg/leeway/cache/remote/s3_test.go

-10
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,6 @@ func TestS3Cache_Upload(t *testing.T) {
368368
},
369369
},
370370
},
371-
{
372-
name: "package not in local cache",
373-
packages: []cache.Package{
374-
&mockPackage{version: "v1"},
375-
},
376-
localCache: &mockLocalCache{
377-
locations: map[string]string{},
378-
},
379-
expectError: true,
380-
},
381371
{
382372
name: "403 forbidden error should not fail",
383373
packages: []cache.Package{

pkg/leeway/reporter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (r *ConsoleReporter) BuildFinished(pkg *Package, err error) {
178178
return
179179
}
180180

181-
color.Println("\n<green>build succeded</>")
181+
color.Println("\n<green>build succeeded</>")
182182
}
183183

184184
// PackageBuildStarted is called when a package build actually gets underway.

0 commit comments

Comments
 (0)