From 643f811f05a205721b9b15d7af1646fb8c36bec4 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sun, 12 Oct 2014 13:57:29 -0700 Subject: [PATCH 1/3] ability to inject private params into the `drone build` cli command --- cli/build.go | 10 +++++++++- cli/util.go | 25 ++++++++++++++++++------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cli/build.go b/cli/build.go index 1e69359ce..cceb954ce 100644 --- a/cli/build.go +++ b/cli/build.go @@ -78,13 +78,21 @@ func buildCommandFunc(c *cli.Context) { func run(path, identity string, privileged bool) (int, error) { dockerClient := docker.New() + // parse the private environment variables + envs := getParamMap("DRONE_ENV_") + // parse the Drone yml file - s, err := script.ParseBuildFile(path) + s, err := script.ParseBuildFile(script.Inject(path, envs)) if err != nil { log.Err(err.Error()) return EXIT_STATUS, err } + // inject private environment variables into build script + for key, val := range envs { + s.Env = append(s.Env, key+"="+val) + } + // remove deploy & publish sections // for now, until I fix bug s.Publish = nil diff --git a/cli/util.go b/cli/util.go index f98a88c3d..ec4f2de7d 100644 --- a/cli/util.go +++ b/cli/util.go @@ -67,13 +67,24 @@ func getRepoPath(dir string) (path string, ok bool) { return dir[index+5:], true } -// getGitOrigin checks the .git origin in an attempt -// to correctly determine the code's package path. This -// is Go-specific, since Go code must exist in -// $GOPATH/src/github.com/{owner}/{name} -func getGitOrigin(dir string) (path string, ok bool) { - // TODO - return +// GetRepoMap returns a map of enivronment variables that +// should be injected into the .drone.yml +func getParamMap(prefix string) map[string]string { + envs := map[string]string{} + + for _, item := range os.Environ() { + env := strings.SplitN(item, "=", 2) + if len(env) != 2 { + continue + } + + key := env[0] + val := env[1] + if strings.HasPrefix(key, prefix) { + envs[strings.TrimPrefix(key, prefix)] = val + } + } + return envs } // prints the time as a human readable string From afc3030087260b51f901b9f674c0d6a9d7b01bb7 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sun, 12 Oct 2014 14:07:28 -0700 Subject: [PATCH 2/3] ability to pass --deploy and --publish flags to `drone build` --- cli/build.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/cli/build.go b/cli/build.go index cceb954ce..134e91040 100644 --- a/cli/build.go +++ b/cli/build.go @@ -34,6 +34,16 @@ func NewBuildCommand() cli.Command { Value: "false", Usage: "runs drone build in a privileged container", }, + cli.StringFlag{ + Name: "deploy", + Value: "false", + Usage: "runs drone build with deployments enabled", + }, + cli.StringFlag{ + Name: "publish", + Value: "false", + Usage: "runs drone build with publishing enabled", + }, }, Action: func(c *cli.Context) { buildCommandFunc(c) @@ -45,6 +55,8 @@ func NewBuildCommand() cli.Command { func buildCommandFunc(c *cli.Context) { var privileged = c.Bool("p") var identity = c.String("i") + var deploy = c.Bool("deploy") + var publish = c.Bool("publish") var path string // the path is provided as an optional argument that @@ -71,11 +83,11 @@ func buildCommandFunc(c *cli.Context) { log.SetPriority(log.LOG_DEBUG) //LOG_NOTICE docker.Logging = false - var exit, _ = run(path, identity, privileged) + var exit, _ = run(path, identity, publish, deploy, privileged) os.Exit(exit) } -func run(path, identity string, privileged bool) (int, error) { +func run(path, identity string, publish, deploy, privileged bool) (int, error) { dockerClient := docker.New() // parse the private environment variables @@ -93,10 +105,12 @@ func run(path, identity string, privileged bool) (int, error) { s.Env = append(s.Env, key+"="+val) } - // remove deploy & publish sections - // for now, until I fix bug - s.Publish = nil - s.Deploy = nil + if deploy == false { + s.Publish = nil + } + if publish == false { + s.Deploy = nil + } // get the repository root directory dir := filepath.Dir(path) @@ -144,7 +158,6 @@ func run(path, identity string, privileged bool) (int, error) { builder.Repo = &code builder.Key = key builder.Stdout = os.Stdout - // TODO ADD THIS BACK builder.Timeout = 300 * time.Minute builder.Privileged = privileged From 617303b1550eb4ddb36c56e0b2e5b1e047b8fd01 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sun, 12 Oct 2014 14:09:55 -0700 Subject: [PATCH 3/3] changed `drone build` flags from string to bool --- cli/build.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/cli/build.go b/cli/build.go index 134e91040..e50be1aa3 100644 --- a/cli/build.go +++ b/cli/build.go @@ -29,19 +29,16 @@ func NewBuildCommand() cli.Command { Value: "", Usage: "identify file injected in the container", }, - cli.StringFlag{ + cli.BoolFlag{ Name: "p", - Value: "false", Usage: "runs drone build in a privileged container", }, - cli.StringFlag{ + cli.BoolFlag{ Name: "deploy", - Value: "false", Usage: "runs drone build with deployments enabled", }, - cli.StringFlag{ + cli.BoolFlag{ Name: "publish", - Value: "false", Usage: "runs drone build with publishing enabled", }, },