Adding caching into the build. Fixing style issues

This commit is contained in:
Don Olmstead 2015-08-17 16:24:47 -07:00 committed by Olmstead
parent d9b59dfa6f
commit d27788e4b4
4 changed files with 87 additions and 51 deletions

View File

@ -45,7 +45,7 @@ func setup(c *Context) error {
// inject the matrix parameters into the yaml // inject the matrix parameters into the yaml
injected := inject.Inject(string(c.Yaml), c.Job.Environment) injected := inject.Inject(string(c.Yaml), c.Job.Environment)
c.Conf, err = parser.ParseSingle(injected, &opts) c.Conf, err = parser.ParseSingle(injected, &opts, c.Repo)
if err != nil { if err != nil {
return err return err
} }

View File

@ -28,14 +28,14 @@ var DefaultOpts = &Opts{
// Parse parses a build matrix and returns // Parse parses a build matrix and returns
// a list of build configurations for each axis // a list of build configurations for each axis
// using the default parsing options. // using the default parsing options.
func Parse(raw string) ([]*common.Config, error) { func Parse(raw string, r *common.Repo) ([]*common.Config, error) {
return ParseOpts(raw, DefaultOpts) return ParseOpts(raw, DefaultOpts, r)
} }
// ParseOpts parses a build matrix and returns // ParseOpts parses a build matrix and returns
// a list of build configurations for each axis // a list of build configurations for each axis
// using the provided parsing options. // using the provided parsing options.
func ParseOpts(raw string, opts *Opts) ([]*common.Config, error) { func ParseOpts(raw string, opts *Opts, r *common.Repo) ([]*common.Config, error) {
axis, err := matrix.Parse(raw) axis, err := matrix.Parse(raw)
if err != nil { if err != nil {
return nil, err return nil, err
@ -45,7 +45,7 @@ func ParseOpts(raw string, opts *Opts) ([]*common.Config, error) {
// when no matrix values exist we should return // when no matrix values exist we should return
// a single config value with an empty label. // a single config value with an empty label.
if len(axis) == 0 { if len(axis) == 0 {
conf, err := ParseSingle(raw, opts) conf, err := ParseSingle(raw, opts, r)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -55,7 +55,7 @@ func ParseOpts(raw string, opts *Opts) ([]*common.Config, error) {
for _, ax := range axis { for _, ax := range axis {
// inject the matrix values into the raw script // inject the matrix values into the raw script
injected := inject.Inject(raw, ax) injected := inject.Inject(raw, ax)
conf, err := ParseSingle(injected, opts) conf, err := ParseSingle(injected, opts, r)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -67,7 +67,7 @@ func ParseOpts(raw string, opts *Opts) ([]*common.Config, error) {
} }
// helper funtion to parse a yaml configuration file. // helper funtion to parse a yaml configuration file.
func ParseSingle(raw string, opts *Opts) (*common.Config, error) { func ParseSingle(raw string, opts *Opts, r *common.Repo) (*common.Config, error) {
conf := &common.Config{} conf := &common.Config{}
err := yaml.Unmarshal([]byte(raw), conf) err := yaml.Unmarshal([]byte(raw), conf)
if err != nil { if err != nil {
@ -79,16 +79,17 @@ func ParseSingle(raw string, opts *Opts) (*common.Config, error) {
return nil, err return nil, err
} }
// apply rules / transforms // apply rules / transforms
transform.Transform(conf) transform.Defaults(conf)
if !opts.Network { if !opts.Network {
transform.TransformRemoveNetwork(conf) transform.RemoveNetwork(conf)
} }
if !opts.Volumes { if !opts.Volumes {
transform.TransformRemoveVolumes(conf) transform.RemoveVolumes(conf)
} }
if !opts.Privileged { if !opts.Privileged {
transform.TransformRemovePrivileged(conf) transform.RemovePrivileged(conf)
} }
transform.Repo(conf, r)
err = LintPlugins(conf, opts) err = LintPlugins(conf, opts)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -13,7 +13,7 @@ import (
// to the build configuration. // to the build configuration.
type transformRule func(*common.Config) type transformRule func(*common.Config)
var transformRules = [...]transformRule{ var transformRules = []transformRule{
transformSetup, transformSetup,
transformClone, transformClone,
transformBuild, transformBuild,
@ -21,48 +21,50 @@ var transformRules = [...]transformRule{
transformDockerPlugin, transformDockerPlugin,
} }
var rmPrivilegedRules = [...]transformRule{ var rmPrivilegedRules = []transformRule{
rmPrivileged, rmPrivileged,
rmVolumes, rmVolumes,
rmNetwork, rmNetwork,
} }
// Transform executes the default transformers that // Default executes the default transformers that
// ensure the minimal Yaml configuration is in place // ensure the minimal Yaml configuration is in place
// and correctly configured. // and correctly configured.
func Transform(c *common.Config) { func Defaults(c *common.Config) {
for _, rule := range transformRules { for _, rule := range transformRules {
rule(c) rule(c)
} }
} }
// TransformSafe executes all transformers that remove // Safe executes all transformers that remove privileged
// privileged options from the Yaml. // options from the Yaml.
func TransformSafe(c *common.Config) { func Safe(c *common.Config) {
for _, rule := range rmPrivilegedRules { for _, rule := range rmPrivilegedRules {
rule(c) rule(c)
} }
} }
// TransformRemoveNetwork executes all transformers that // RemoveNetwork executes all transformers that remove
// remove network options from the Yaml. // network options from the Yaml.
func TransformRemoveNetwork(c *common.Config) { func RemoveNetwork(c *common.Config) {
rmNetwork(c) rmNetwork(c)
} }
// TransformRemoveVolumes executes all transformers that // TransformRemoveVolumes executes all transformers that
// remove volume options from the Yaml. // remove volume options from the Yaml.
func TransformRemoveVolumes(c *common.Config) { func RemoveVolumes(c *common.Config) {
rmVolumes(c) rmVolumes(c)
} }
// TransformRemovePrivileged executes all transformers that // RemovePrivileged executes all transformers that remove
// remove privileged options from the Yaml. // privileged options from the Yaml.
func TransformRemovePrivileged(c *common.Config) { func RemovePrivileged(c *common.Config) {
rmPrivileged(c) rmPrivileged(c)
} }
func TransformRepo(c *common.Config, r *common.Repo) { // Repo executes all transformers that rely on repository
// information.
func Repo(c *common.Config, r *common.Repo) {
transformWorkspace(c, r) transformWorkspace(c, r)
transformCache(c, r) transformCache(c, r)
} }
@ -209,7 +211,10 @@ func transformWorkspace(c *common.Config, r *common.Repo) {
func transformCache(c *common.Config, r *common.Repo) { func transformCache(c *common.Config, r *common.Repo) {
cacheCount := len(c.Build.Cache) cacheCount := len(c.Build.Cache)
if cacheCount != 0 { if cacheCount == 0 {
return
}
volumes := make([]string, cacheCount) volumes := make([]string, cacheCount)
cache := cacheRoot(r) cache := cacheRoot(r)
@ -220,11 +225,13 @@ func transformCache(c *common.Config, r *common.Repo) {
workspaceDir := filepath.Join(workspace, dir) workspaceDir := filepath.Join(workspace, dir)
volumes[i] = fmt.Sprintf("%s:%s", cacheDir, workspaceDir) volumes[i] = fmt.Sprintf("%s:%s", cacheDir, workspaceDir)
fmt.Printf("Volume %s", volumes[i])
} }
c.Setup.Volumes = append(c.Setup.Volumes, volumes...) c.Setup.Volumes = append(c.Setup.Volumes, volumes...)
c.Clone.Volumes = append(c.Clone.Volumes, volumes...) c.Clone.Volumes = append(c.Clone.Volumes, volumes...)
c.Build.Volumes = append(c.Build.Volumes, volumes...) c.Build.Volumes = append(c.Build.Volumes, volumes...)
for _, step := range c.Publish { for _, step := range c.Publish {
step.Volumes = append(step.Volumes, volumes...) step.Volumes = append(step.Volumes, volumes...)
} }
@ -238,7 +245,6 @@ func transformCache(c *common.Config, r *common.Repo) {
step.Volumes = append(step.Volumes, volumes...) step.Volumes = append(step.Volumes, volumes...)
} }
} }
}
// imageName is a helper function that resolves the // imageName is a helper function that resolves the
// image name. When using official drone plugins it // image name. When using official drone plugins it
@ -263,14 +269,20 @@ func imageNameDefault(name, defaultName string) string {
return imageName(name) return imageName(name)
} }
// workspaceRoot is a helper function that determines the
// default workspace the build runs in.
func workspaceRoot(r *common.Repo) string { func workspaceRoot(r *common.Repo) string {
return filepath.Join("/drone/src", repoPath(r)) return filepath.Join("/drone/src", repoPath(r))
} }
// cacheRoot is a helper function that deteremines the
// default caching root.
func cacheRoot(r *common.Repo) string { func cacheRoot(r *common.Repo) string {
return filepath.Join("/tmp/drone/cache", repoPath(r)) return filepath.Join("/tmp/drone/cache", repoPath(r))
} }
// repoPath is a helper function that creates a path based
// on the host and repository name.
func repoPath(r *common.Repo) string { func repoPath(r *common.Repo) string {
parsed, _ := url.Parse(r.Link) parsed, _ := url.Parse(r.Link)
return filepath.Join(parsed.Host, r.FullName) return filepath.Join(parsed.Host, r.FullName)

View File

@ -145,9 +145,14 @@ func Test_Transform(t *testing.T) {
g.It("Should have cached volumes", func() { g.It("Should have cached volumes", func() {
c := &common.Config{ c := &common.Config{
Setup: &common.Step{},
Clone: &common.Step{},
Build: &common.Step{ Build: &common.Step{
Cache: []string{".git","foo","bar"}, Cache: []string{".git","foo","bar"},
}, },
Notify: map[string]*common.Step{},
Deploy: map[string]*common.Step{},
Publish: map[string]*common.Step{},
} }
r := &common.Repo{ r := &common.Repo{
Link: "https://github.com/drone/drone", Link: "https://github.com/drone/drone",
@ -155,7 +160,25 @@ func Test_Transform(t *testing.T) {
} }
transformCache(c, r) transformCache(c, r)
g.Assert(len(c.Build.Volumes)).Equal(3) cacheCount := len(c.Build.Cache)
test := func(s *common.Step) {
g.Assert(len(s.Volumes)).Equal(cacheCount)
}
testRange := func(s map[string]*common.Step) {
for _, step := range s {
test(step)
}
}
test(c.Setup)
test(c.Clone)
test(c.Build)
testRange(c.Publish)
testRange(c.Deploy)
testRange(c.Notify)
testRange(c.Compose)
}) })
}) })
} }