Skip to content

Commit

Permalink
Add support for setting env with -e (#375)
Browse files Browse the repository at this point in the history
* Add support for passing environment

* Add a helper for -e flags
  • Loading branch information
mcncl authored Oct 7, 2024
1 parent ca55d11 commit ab7f133
Showing 1 changed file with 19 additions and 2 deletions.
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

0 comments on commit ab7f133

Please sign in to comment.