From bf965bf4ce0f27105bf932cc0620a2c25c74742c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Valais?= Date: Wed, 25 Aug 2021 14:17:27 +0200 Subject: [PATCH] running cmrel does not require to be in the release repo folder anymore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The flag --cloudbuild was thus removed. Signed-off-by: Maƫl Valais --- cmd/cmrel/cmd/publish.go | 16 +++++++--------- .../cmrel/cmd/publish_cloudbuild.yaml | 0 cmd/cmrel/cmd/stage.go | 16 +++++++--------- .../cmrel/cmd/stage_cloudbuild.yaml | 0 pkg/gcb/gcb.go | 17 +++++------------ 5 files changed, 19 insertions(+), 30 deletions(-) rename gcb/publish/cloudbuild.yaml => cmd/cmrel/cmd/publish_cloudbuild.yaml (100%) rename gcb/stage/cloudbuild.yaml => cmd/cmrel/cmd/stage_cloudbuild.yaml (100%) diff --git a/cmd/cmrel/cmd/publish.go b/cmd/cmrel/cmd/publish.go index 55e05ce..698e1f7 100644 --- a/cmd/cmrel/cmd/publish.go +++ b/cmd/cmrel/cmd/publish.go @@ -28,6 +28,8 @@ import ( "github.com/cert-manager/release/pkg/gcb" "github.com/cert-manager/release/pkg/release" + + _ "embed" ) const ( @@ -53,9 +55,6 @@ type publishOptions struct { // Name of the staged release to publish ReleaseName string - // The path to the cloudbuild.yaml file used to perform the cert-manager crossbuild - CloudBuildFile string - // Project to run the GCB job in Project string @@ -98,8 +97,6 @@ type publishOptions struct { func (o *publishOptions) AddFlags(fs *flag.FlagSet, markRequired func(string)) { fs.StringVar(&o.Bucket, "bucket", release.DefaultBucketName, "The name of the GCS bucket to publish the release to.") fs.StringVar(&o.ReleaseName, "release-name", "", "Name of the staged release to publish.") - fs.StringVar(&o.CloudBuildFile, "cloudbuild", "./gcb/publish/cloudbuild.yaml", "The path to the cloudbuild.yaml file used to publish the release. "+ - "The default value assumes that this tool is run from the root of the release repository.") fs.StringVar(&o.Project, "project", release.DefaultReleaseProject, "The GCP project to run the GCB build jobs in.") fs.BoolVar(&o.NoMock, "nomock", false, "Whether to actually publish the release. If false, the command will exit after preparing the release for pushing.") fs.StringVar(&o.PublishedImageRepository, "published-image-repo", release.DefaultImageRepository, "The docker image repository to push the release images & manifest lists to.") @@ -114,7 +111,6 @@ func (o *publishOptions) print() { log.Printf("Publish options:") log.Printf(" Bucket: %q", o.Bucket) log.Printf(" ReleaseName: %q", o.ReleaseName) - log.Printf(" CloudBuildFile: %q", o.CloudBuildFile) log.Printf(" Project: %q", o.Project) log.Printf(" NoMock: %t", o.NoMock) log.Printf(" PublishedImageRepo: %q", o.PublishedImageRepository) @@ -145,7 +141,10 @@ func publishCmd(rootOpts *rootOptions) *cobra.Command { return cmd } -func runPublish(rootOpts *rootOptions, o *publishOptions) error { +//go:embed publish_cloudbuild.yaml +var cloudbuildPublish []byte + +func runPublish(_ *rootOptions, o *publishOptions) error { ctx := context.Background() gcs, err := storage.NewClient(ctx) if err != nil { @@ -159,8 +158,7 @@ func runPublish(rootOpts *rootOptions, o *publishOptions) error { } log.Printf("Release with version %q (%s) will be published", rel.Metadata().ReleaseVersion, rel.Metadata().GitCommitRef) - log.Printf("DEBUG: Loading cloudbuild.yaml file from %q", o.CloudBuildFile) - build, err := gcb.LoadBuild(o.CloudBuildFile) + build, err := gcb.LoadCloudBuild(cloudbuildPublish) if err != nil { return fmt.Errorf("error loading cloudbuild.yaml file: %w", err) } diff --git a/gcb/publish/cloudbuild.yaml b/cmd/cmrel/cmd/publish_cloudbuild.yaml similarity index 100% rename from gcb/publish/cloudbuild.yaml rename to cmd/cmrel/cmd/publish_cloudbuild.yaml diff --git a/cmd/cmrel/cmd/stage.go b/cmd/cmrel/cmd/stage.go index c1d1966..62d9e04 100644 --- a/cmd/cmrel/cmd/stage.go +++ b/cmd/cmrel/cmd/stage.go @@ -27,6 +27,8 @@ import ( "github.com/cert-manager/release/pkg/gcb" "github.com/cert-manager/release/pkg/release" + + _ "embed" ) const ( @@ -67,9 +69,6 @@ type stageOptions struct { // Optional commit ref of cert-manager that should be staged GitRef string - // The path to the cloudbuild.yaml file used to perform the cert-manager crossbuild - CloudBuildFile string - // Project is the name of the GCP project to run the GCB job in Project string @@ -92,8 +91,6 @@ func (o *stageOptions) AddFlags(fs *flag.FlagSet, markRequired func(string)) { fs.StringVar(&o.Repo, "repo", "cert-manager", "Name of the GitHub repo to fetch cert-manager sources from.") fs.StringVar(&o.Branch, "branch", "master", "The git branch to build the release from. If --git-ref is not specified, the HEAD of this branch will be looked up on GitHub.") fs.StringVar(&o.GitRef, "git-ref", "", "The git commit ref of cert-manager that should be staged.") - fs.StringVar(&o.CloudBuildFile, "cloudbuild", "./gcb/stage/cloudbuild.yaml", "The path to the cloudbuild.yaml file used to perform the cert-manager crossbuild. "+ - "The default value assumes that this tool is run from the root of the release repository.") fs.StringVar(&o.Project, "project", release.DefaultReleaseProject, "The GCP project to run the GCB build jobs in.") fs.StringVar(&o.ReleaseVersion, "release-version", "", "Optional release version override used to force the version strings used during the release to a specific value. If not set, build is treated as development build and artifacts staged to 'devel' path.") fs.StringVar(&o.PublishedImageRepository, "published-image-repo", release.DefaultImageRepository, "The docker image repository set when building the release.") @@ -107,7 +104,6 @@ func (o *stageOptions) print() { log.Printf(" Repo: %q", o.Repo) log.Printf(" Branch: %q", o.Branch) log.Printf(" GitRef: %q", o.GitRef) - log.Printf(" CloudBuildFile: %q", o.CloudBuildFile) log.Printf(" Project: %q", o.Project) log.Printf(" ReleaseVersion: %q", o.ReleaseVersion) log.Printf(" PublishedImageRepo: %q", o.PublishedImageRepository) @@ -133,7 +129,10 @@ func stageCmd(rootOpts *rootOptions) *cobra.Command { return cmd } -func runStage(rootOpts *rootOptions, o *stageOptions) error { +//go:embed stage_cloudbuild.yaml +var cloudbuildStage []byte + +func runStage(_ *rootOptions, o *stageOptions) error { if o.GitRef == "" { log.Printf("git-ref flag not specified, looking up git commit ref for %s/%s@%s", o.Org, o.Repo, o.Branch) ref, err := release.LookupBranchRef(o.Org, o.Repo, o.Branch) @@ -144,8 +143,7 @@ func runStage(rootOpts *rootOptions, o *stageOptions) error { } log.Printf("Staging build for %s/%s@%s", o.Org, o.Repo, o.GitRef) - log.Printf("DEBUG: Loading cloudbuild.yaml file from %q", o.CloudBuildFile) - build, err := gcb.LoadBuild(o.CloudBuildFile) + build, err := gcb.LoadCloudBuild(cloudbuildStage) if err != nil { return fmt.Errorf("error loading cloudbuild.yaml file: %w", err) } diff --git a/gcb/stage/cloudbuild.yaml b/cmd/cmrel/cmd/stage_cloudbuild.yaml similarity index 100% rename from gcb/stage/cloudbuild.yaml rename to cmd/cmrel/cmd/stage_cloudbuild.yaml diff --git a/pkg/gcb/gcb.go b/pkg/gcb/gcb.go index 27b3e56..baf5eb3 100644 --- a/pkg/gcb/gcb.go +++ b/pkg/gcb/gcb.go @@ -21,7 +21,6 @@ import ( "encoding/json" "fmt" "log" - "os" "time" "google.golang.org/api/cloudbuild/v1" @@ -34,16 +33,11 @@ const ( Failure = "FAILURE" ) -// LoadBuild will decode a cloudbuild.yaml file into a cloudbuild.Build -// structure and return it. -func LoadBuild(filename string) (*cloudbuild.Build, error) { - f, err := os.ReadFile(filename) - if err != nil { - return nil, err - } - +// LoadCloudBuild will decode the content of a cloudbuild.yaml file into a +// cloudbuild.Build structure and return it. +func LoadCloudBuild(cloudbuildRaw []byte) (*cloudbuild.Build, error) { cb := cloudbuild.Build{} - if err := yaml.UnmarshalStrict(f, &cb); err != nil { + if err := yaml.UnmarshalStrict(cloudbuildRaw, &cb); err != nil { return nil, err } @@ -59,10 +53,9 @@ func SubmitBuild(svc *cloudbuild.Service, projectID string, build *cloudbuild.Bu return nil, err } - log.Printf("DEBUG: decoding build operation metadata") metadata := &cloudbuild.BuildOperationMetadata{} if err := json.Unmarshal(op.Metadata, metadata); err != nil { - return nil, err + return nil, fmt.Errorf("while decoding build operation metadata: %v", err) } return metadata.Build, nil