Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting env with -e #375

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions pkg/cmd/build/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,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(
Expand All @@ -51,7 +60,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
}
Expand All @@ -65,12 +80,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().
Expand All @@ -89,6 +105,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)
Expand Down