harness-drone/cmd/drone-build/util.go

97 lines
2.4 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
"strconv"
"strings"
2015-05-22 18:37:40 +00:00
"github.com/drone/drone/Godeps/_workspace/src/github.com/samalba/dockerclient"
2015-07-13 07:11:33 +00:00
"github.com/drone/drone/pkg/types"
)
// helper function that converts the build step to
// a containerConfig for use with the dockerclient
2015-07-13 07:11:33 +00:00
func toContainerConfig(step *types.Step) *dockerclient.ContainerConfig {
config := &dockerclient.ContainerConfig{
Image: step.Image,
Env: step.Environment,
Cmd: step.Command,
Entrypoint: step.Entrypoint,
WorkingDir: step.WorkingDir,
HostConfig: dockerclient.HostConfig{
Privileged: step.Privileged,
NetworkMode: step.NetworkMode,
},
}
if len(config.Entrypoint) == 0 {
config.Entrypoint = nil
}
config.Volumes = map[string]struct{}{}
for _, path := range step.Volumes {
if strings.Index(path, ":") == -1 {
continue
}
parts := strings.Split(path, ":")
config.Volumes[parts[1]] = struct{}{}
config.HostConfig.Binds = append(config.HostConfig.Binds, path)
}
return config
}
// helper function to inject drone-specific environment
// variables into the container.
func toEnv(c *Context) map[string]string {
return map[string]string{
"CI": "true",
"BUILD_DIR": c.Clone.Dir,
2015-07-13 07:11:33 +00:00
"BUILD_ID": strconv.Itoa(c.Build.Number),
"BUILD_NUMBER": strconv.Itoa(c.Build.Number),
"JOB_NAME": c.Repo.FullName,
"WORKSPACE": c.Clone.Dir,
"GIT_BRANCH": c.Clone.Branch,
"GIT_COMMIT": c.Clone.Sha,
"DRONE": "true",
"DRONE_REPO": c.Repo.FullName,
2015-07-13 07:11:33 +00:00
"DRONE_BUILD": strconv.Itoa(c.Build.Number),
"DRONE_BRANCH": c.Clone.Branch,
"DRONE_COMMIT": c.Clone.Sha,
"DRONE_DIR": c.Clone.Dir,
}
}
// helper function to encode the build step to
// a json string. Primarily used for plugins, which
// expect a json encoded string in stdin or arg[1].
2015-07-13 07:11:33 +00:00
func toCommand(c *Context, step *types.Step) []string {
p := payload{
c.Repo,
2015-07-13 07:11:33 +00:00
c.Build,
2015-06-19 00:36:52 +00:00
c.Job,
c.Clone,
step.Config,
}
return []string{p.Encode()}
}
// payload represents the payload of a plugin
// that is serialized and sent to the plugin in JSON
// format via stdin or arg[1].
type payload struct {
2015-07-13 07:11:33 +00:00
Repo *types.Repo `json:"repo"`
Build *types.Build `json:"build"`
Job *types.Job `json:"job"`
Clone *types.Clone `json:"clone"`
Config map[string]interface{} `json:"vargs"`
}
// Encode encodes the payload in JSON format.
func (p *payload) Encode() string {
out, _ := json.Marshal(p)
return string(out)
}