From 22e62f3fee17dac3feb14365c5eaa6781fce75e5 Mon Sep 17 00:00:00 2001 From: Ben McNicholl Date: Mon, 7 Oct 2024 16:57:53 +1100 Subject: [PATCH 1/2] Add support for passing environment --- pkg/cmd/build/new.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/build/new.go b/pkg/cmd/build/new.go index 566832b0..ba5f0887 100644 --- a/pkg/cmd/build/new.go +++ b/pkg/cmd/build/new.go @@ -20,6 +20,8 @@ func NewCmdBuildNew(f *factory.Factory) *cobra.Command { var pipeline string var confirmed bool var web bool + var env []string + var envMap = make(map[string]string) cmd := cobra.Command{ DisableFlagsInUseLine: true, @@ -51,7 +53,13 @@ func NewCmdBuildNew(f *factory.Factory) *cobra.Command { } if confirmed { - return newBuild(pipeline.Org, pipeline.Name, f, message, commit, branch, web) + for _, e := range env { + parts := strings.Split(e, "=") + if len(parts) == 2 { + envMap[parts[0]] = parts[1] + } + } + return newBuild(pipeline.Org, pipeline.Name, f, message, commit, branch, web, envMap) } else { return nil } @@ -65,12 +73,13 @@ func NewCmdBuildNew(f *factory.Factory) *cobra.Command { cmd.Flags().StringVarP(&pipeline, "pipeline", "p", "", "The pipeline to build. This can be a {pipeline slug} or in the format {org slug}/{pipeline slug}.\n"+ "If omitted, it will be resolved using the current directory.", ) + cmd.Flags().StringArrayVarP(&env, "env", "e", []string{}, "Set environment variables for the build") cmd.Flags().BoolVarP(&confirmed, "yes", "y", false, "Skip the confirmation prompt. Useful if being used in automation/CI.") cmd.Flags().SortFlags = false return &cmd } -func newBuild(org string, pipeline string, f *factory.Factory, message string, commit string, branch string, web bool) error { +func newBuild(org string, pipeline string, f *factory.Factory, message string, commit string, branch string, web bool, env map[string]string) error { var err error var build *buildkite.Build spinErr := spinner.New(). @@ -89,6 +98,7 @@ func newBuild(org string, pipeline string, f *factory.Factory, message string, c Message: message, Commit: commit, Branch: branch, + Env: env, } build, _, err = f.RestAPIClient.Builds.Create(org, pipeline, &newBuild) From a6703f92b9cfc5f5343452ec875d4a6d2a6f8eef Mon Sep 17 00:00:00 2001 From: Ben McNicholl Date: Mon, 7 Oct 2024 17:00:02 +1100 Subject: [PATCH 2/2] Add a helper for -e flags --- pkg/cmd/build/new.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/cmd/build/new.go b/pkg/cmd/build/new.go index ba5f0887..77d9ea01 100644 --- a/pkg/cmd/build/new.go +++ b/pkg/cmd/build/new.go @@ -31,6 +31,13 @@ func NewCmdBuildNew(f *factory.Factory) *cobra.Command { Long: heredoc.Doc(` Create a new build on a pipeline. The web URL to the build will be printed to stdout. + + ## To create a new build + $ bk build new + + ## To create a new build with environment variables set + $ bk build new -e "FOO=BAR" -e "BAR=BAZ" + `), RunE: func(cmd *cobra.Command, args []string) error { resolvers := resolver.NewAggregateResolver(