Merge pull request #541 from bradrydzewski/master

ability to inject private params into the `drone build` cli command
This commit is contained in:
Brad Rydzewski 2014-10-12 14:13:38 -07:00
commit f45ef96f7e
2 changed files with 46 additions and 17 deletions

View file

@ -29,11 +29,18 @@ func NewBuildCommand() cli.Command {
Value: "", Value: "",
Usage: "identify file injected in the container", Usage: "identify file injected in the container",
}, },
cli.StringFlag{ cli.BoolFlag{
Name: "p", Name: "p",
Value: "false",
Usage: "runs drone build in a privileged container", Usage: "runs drone build in a privileged container",
}, },
cli.BoolFlag{
Name: "deploy",
Usage: "runs drone build with deployments enabled",
},
cli.BoolFlag{
Name: "publish",
Usage: "runs drone build with publishing enabled",
},
}, },
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
buildCommandFunc(c) buildCommandFunc(c)
@ -45,6 +52,8 @@ func NewBuildCommand() cli.Command {
func buildCommandFunc(c *cli.Context) { func buildCommandFunc(c *cli.Context) {
var privileged = c.Bool("p") var privileged = c.Bool("p")
var identity = c.String("i") var identity = c.String("i")
var deploy = c.Bool("deploy")
var publish = c.Bool("publish")
var path string var path string
// the path is provided as an optional argument that // the path is provided as an optional argument that
@ -71,24 +80,34 @@ func buildCommandFunc(c *cli.Context) {
log.SetPriority(log.LOG_DEBUG) //LOG_NOTICE log.SetPriority(log.LOG_DEBUG) //LOG_NOTICE
docker.Logging = false docker.Logging = false
var exit, _ = run(path, identity, privileged) var exit, _ = run(path, identity, publish, deploy, privileged)
os.Exit(exit) 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() dockerClient := docker.New()
// parse the private environment variables
envs := getParamMap("DRONE_ENV_")
// parse the Drone yml file // parse the Drone yml file
s, err := script.ParseBuildFile(path) s, err := script.ParseBuildFile(script.Inject(path, envs))
if err != nil { if err != nil {
log.Err(err.Error()) log.Err(err.Error())
return EXIT_STATUS, err return EXIT_STATUS, err
} }
// remove deploy & publish sections // inject private environment variables into build script
// for now, until I fix bug for key, val := range envs {
s.Publish = nil s.Env = append(s.Env, key+"="+val)
s.Deploy = nil }
if deploy == false {
s.Publish = nil
}
if publish == false {
s.Deploy = nil
}
// get the repository root directory // get the repository root directory
dir := filepath.Dir(path) dir := filepath.Dir(path)
@ -136,7 +155,6 @@ func run(path, identity string, privileged bool) (int, error) {
builder.Repo = &code builder.Repo = &code
builder.Key = key builder.Key = key
builder.Stdout = os.Stdout builder.Stdout = os.Stdout
// TODO ADD THIS BACK
builder.Timeout = 300 * time.Minute builder.Timeout = 300 * time.Minute
builder.Privileged = privileged builder.Privileged = privileged

View file

@ -67,13 +67,24 @@ func getRepoPath(dir string) (path string, ok bool) {
return dir[index+5:], true return dir[index+5:], true
} }
// getGitOrigin checks the .git origin in an attempt // GetRepoMap returns a map of enivronment variables that
// to correctly determine the code's package path. This // should be injected into the .drone.yml
// is Go-specific, since Go code must exist in func getParamMap(prefix string) map[string]string {
// $GOPATH/src/github.com/{owner}/{name} envs := map[string]string{}
func getGitOrigin(dir string) (path string, ok bool) {
// TODO for _, item := range os.Environ() {
return 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 // prints the time as a human readable string