Supporting multiple yamls in procBuilder

This commit is contained in:
Laszlo Fogas 2019-06-01 10:41:51 +02:00
parent 8199fe4a21
commit c9f2346c2c
4 changed files with 113 additions and 124 deletions

View File

@ -323,7 +323,7 @@ func PostApproval(c *gin.Context) {
Secs: secs, Secs: secs,
Regs: regs, Regs: regs,
Link: httputil.GetURL(c.Request), Link: httputil.GetURL(c.Request),
Yaml: conf.Data, Yamls: []string{conf.Data},
Envs: envs, Envs: envs,
} }
buildItems, err := b.Build() buildItems, err := b.Build()
@ -512,7 +512,7 @@ func PostBuild(c *gin.Context) {
Secs: secs, Secs: secs,
Regs: regs, Regs: regs,
Link: httputil.GetURL(c.Request), Link: httputil.GetURL(c.Request),
Yaml: conf.Data, Yamls: []string{conf.Data},
Envs: buildParams, Envs: buildParams,
} }
buildItems, err := b.Build() buildItems, err := b.Build()

View File

@ -235,7 +235,7 @@ func PostHook(c *gin.Context) {
Regs: regs, Regs: regs,
Envs: envs, Envs: envs,
Link: httputil.GetURL(c.Request), Link: httputil.GetURL(c.Request),
Yaml: conf.Data, Yamls: []string{conf.Data},
} }
buildItems, err := b.Build() buildItems, err := b.Build()
if err != nil { if err != nil {

View File

@ -32,11 +32,12 @@ bbb`,
Secs: []*model.Secret{}, Secs: []*model.Secret{},
Regs: []*model.Registry{}, Regs: []*model.Registry{},
Link: "", Link: "",
Yaml: `pipeline: Yamls: []string{`pipeline:
xxx: xxx:
image: scratch image: scratch
yyy: ${DRONE_COMMIT_MESSAGE} yyy: ${DRONE_COMMIT_MESSAGE}
`, `,
},
} }
if _, err := b.Build(); err != nil { if _, err := b.Build(); err != nil {

View File

@ -39,7 +39,7 @@ type procBuilder struct {
Secs []*model.Secret Secs []*model.Secret
Regs []*model.Registry Regs []*model.Registry
Link string Link string
Yaml string Yamls []string
Envs map[string]string Envs map[string]string
} }
@ -51,139 +51,127 @@ type buildItem struct {
} }
func (b *procBuilder) Build() ([]*buildItem, error) { func (b *procBuilder) Build() ([]*buildItem, error) {
axes, err := matrix.ParseString(b.Yaml)
if err != nil {
return nil, err
}
if len(axes) == 0 {
axes = append(axes, matrix.Axis{})
}
var items []*buildItem var items []*buildItem
for i, axis := range axes {
proc := &model.Proc{
BuildID: b.Curr.ID,
PID: i + 1,
PGID: i + 1,
State: model.StatusPending,
Environ: axis,
}
metadata := metadataFromStruct(b.Repo, b.Curr, b.Last, proc, b.Link) for _, y := range b.Yamls {
environ := metadata.Environ() axes, err := matrix.ParseString(y)
for k, v := range metadata.EnvironDrone() {
environ[k] = v
}
for k, v := range axis {
environ[k] = v
}
var secrets []compiler.Secret
for _, sec := range b.Secs {
if !sec.Match(b.Curr.Event) {
continue
}
secrets = append(secrets, compiler.Secret{
Name: sec.Name,
Value: sec.Value,
Match: sec.Images,
})
}
y := b.Yaml
s, err := envsubst.Eval(y, func(name string) string {
env := environ[name]
if strings.Contains(env, "\n") {
env = fmt.Sprintf("%q", env)
}
return env
})
if err != nil { if err != nil {
return nil, err return nil, err
} }
y = s if len(axes) == 0 {
axes = append(axes, matrix.Axis{})
parsed, err := yaml.ParseString(y)
if err != nil {
return nil, err
}
metadata.Sys.Arch = parsed.Platform
if metadata.Sys.Arch == "" {
metadata.Sys.Arch = "linux/amd64"
} }
lerr := linter.New( for i, axis := range axes {
linter.WithTrusted(b.Repo.IsTrusted), proc := &model.Proc{
).Lint(parsed) BuildID: b.Curr.ID,
if lerr != nil { PID: i + 1,
return nil, lerr PGID: i + 1,
} State: model.StatusPending,
Environ: axis,
}
var registries []compiler.Registry metadata := metadataFromStruct(b.Repo, b.Curr, b.Last, proc, b.Link)
for _, reg := range b.Regs { environ := metadata.Environ()
registries = append(registries, compiler.Registry{ for k, v := range metadata.EnvironDrone() {
Hostname: reg.Address, environ[k] = v
Username: reg.Username, }
Password: reg.Password, for k, v := range axis {
Email: reg.Email, environ[k] = v
}
var secrets []compiler.Secret
for _, sec := range b.Secs {
if !sec.Match(b.Curr.Event) {
continue
}
secrets = append(secrets, compiler.Secret{
Name: sec.Name,
Value: sec.Value,
Match: sec.Images,
})
}
s, err := envsubst.Eval(y, func(name string) string {
env := environ[name]
if strings.Contains(env, "\n") {
env = fmt.Sprintf("%q", env)
}
return env
}) })
} if err != nil {
return nil, err
}
y = s
ir := compiler.New( parsed, err := yaml.ParseString(y)
compiler.WithEnviron(environ), if err != nil {
compiler.WithEnviron(b.Envs), return nil, err
compiler.WithEscalated(Config.Pipeline.Privileged...), }
compiler.WithResourceLimit(Config.Pipeline.Limits.MemSwapLimit, Config.Pipeline.Limits.MemLimit, Config.Pipeline.Limits.ShmSize, Config.Pipeline.Limits.CPUQuota, Config.Pipeline.Limits.CPUShares, Config.Pipeline.Limits.CPUSet), metadata.Sys.Arch = parsed.Platform
compiler.WithVolumes(Config.Pipeline.Volumes...), if metadata.Sys.Arch == "" {
compiler.WithNetworks(Config.Pipeline.Networks...), metadata.Sys.Arch = "linux/amd64"
compiler.WithLocal(false), }
compiler.WithOption(
compiler.WithNetrc( lerr := linter.New(
b.Netrc.Login, linter.WithTrusted(b.Repo.IsTrusted),
b.Netrc.Password, ).Lint(parsed)
b.Netrc.Machine, if lerr != nil {
return nil, lerr
}
var registries []compiler.Registry
for _, reg := range b.Regs {
registries = append(registries, compiler.Registry{
Hostname: reg.Address,
Username: reg.Username,
Password: reg.Password,
Email: reg.Email,
})
}
ir := compiler.New(
compiler.WithEnviron(environ),
compiler.WithEnviron(b.Envs),
compiler.WithEscalated(Config.Pipeline.Privileged...),
compiler.WithResourceLimit(Config.Pipeline.Limits.MemSwapLimit, Config.Pipeline.Limits.MemLimit, Config.Pipeline.Limits.ShmSize, Config.Pipeline.Limits.CPUQuota, Config.Pipeline.Limits.CPUShares, Config.Pipeline.Limits.CPUSet),
compiler.WithVolumes(Config.Pipeline.Volumes...),
compiler.WithNetworks(Config.Pipeline.Networks...),
compiler.WithLocal(false),
compiler.WithOption(
compiler.WithNetrc(
b.Netrc.Login,
b.Netrc.Password,
b.Netrc.Machine,
),
b.Repo.IsPrivate,
), ),
b.Repo.IsPrivate, compiler.WithRegistry(registries...),
), compiler.WithSecret(secrets...),
compiler.WithRegistry(registries...), compiler.WithPrefix(
compiler.WithSecret(secrets...), fmt.Sprintf(
compiler.WithPrefix( "%d_%d",
fmt.Sprintf( proc.ID,
"%d_%d", rand.Int(),
proc.ID, ),
rand.Int(),
), ),
), compiler.WithEnviron(proc.Environ),
compiler.WithEnviron(proc.Environ), compiler.WithProxy(),
compiler.WithProxy(), compiler.WithWorkspaceFromURL("/drone", b.Repo.Link),
compiler.WithWorkspaceFromURL("/drone", b.Repo.Link), compiler.WithMetadata(metadata),
compiler.WithMetadata(metadata), ).Compile(parsed)
).Compile(parsed)
// for _, sec := range b.Secs { item := &buildItem{
// if !sec.MatchEvent(b.Curr.Event) { Proc: proc,
// continue Config: ir,
// } Labels: parsed.Labels,
// if b.Curr.Verified || sec.SkipVerify { Platform: metadata.Sys.Arch,
// ir.Secrets = append(ir.Secrets, &backend.Secret{ }
// Mask: sec.Conceal, if item.Labels == nil {
// Name: sec.Name, item.Labels = map[string]string{}
// Value: sec.Value, }
// }) items = append(items, item)
// }
// }
item := &buildItem{
Proc: proc,
Config: ir,
Labels: parsed.Labels,
Platform: metadata.Sys.Arch,
} }
if item.Labels == nil {
item.Labels = map[string]string{}
}
items = append(items, item)
} }
return items, nil return items, nil