From 04f1e344b10fb41219466ea6146306a22533f531 Mon Sep 17 00:00:00 2001 From: Dimitris Sotirakis Date: Wed, 3 May 2023 14:52:10 +0300 Subject: [PATCH] CI: Fix broken nightly builds (#67705) Sort objects in a bucket based on the generation timestamp --- pkg/build/gcloud/storage/gsutil.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/pkg/build/gcloud/storage/gsutil.go b/pkg/build/gcloud/storage/gsutil.go index 38a040f6017..91ead018b42 100644 --- a/pkg/build/gcloud/storage/gsutil.go +++ b/pkg/build/gcloud/storage/gsutil.go @@ -366,11 +366,17 @@ func GetLatestMainBuild(ctx context.Context, bucket *storage.BucketHandle, path return "", ErrorNilBucket } - it := bucket.Objects(ctx, &storage.Query{ + query := &storage.Query{ Prefix: path, - }) + } + err := query.SetAttrSelection([]string{"Name", "Generation"}) + if err != nil { + return "", fmt.Errorf("failed to set attribute selector, err: %q", err) + } + it := bucket.Objects(ctx, query) var files []string + var oldGeneration int64 for { attrs, err := it.Next() if errors.Is(err, iterator.Done) { @@ -379,12 +385,16 @@ func GetLatestMainBuild(ctx context.Context, bucket *storage.BucketHandle, path if err != nil { return "", fmt.Errorf("failed to iterate through bucket, err: %w", err) } - - files = append(files, attrs.Name) + if attrs.Generation >= oldGeneration { + files = append([]string{attrs.Name}, files...) + oldGeneration = attrs.Generation + } else { + files = append(files, attrs.Name) + } } var latestVersion string - for i := len(files) - 1; i >= 0; i-- { + for i := 0; i < len(files); i++ { captureVersion := regexp.MustCompile(`(\d+\.\d+\.\d+-\d+pre)`) if captureVersion.MatchString(files[i]) { latestVersion = captureVersion.FindString(files[i])